Skip to content
Myr

Break up a circular dependency on shutdown

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 11c7b1d · Browse files · 3 files+93 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+93 added−0 removed

  • other:+93 added−0 removed

3 files+93 added−0 removed

added:flask/testsuite/regression.py+83 added
1# -*- coding: utf-8 -*-2"""3 flask.testsuite.regression4 ~~~~~~~~~~~~~~~~~~~~~~~~~~56 Tests regressions.78 :copyright: (c) 2011 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112from __future__ import with_statement1314import gc15import sys16import flask17import threading18import unittest19from werkzeug.test import run_wsgi_app, create_environ20from flask.testsuite import FlaskTestCase212223_gc_lock = threading.Lock()242526class _NoLeakAsserter(object):2728 def __init__(self, testcase):29 self.testcase = testcase3031 def __enter__(self):32 gc.disable()33 _gc_lock.acquire()34 loc = flask._request_ctx_stack._local3536 # Force Python to track this dictionary at all times.37 # This is necessary since Python only starts tracking38 # dicts if they contain mutable objects. It's a horrible,39 # horrible hack but makes this kinda testable.40 loc.__storage__['FOOO'] = [1, 2, 3]4142 gc.collect()43 self.old_objects = len(gc.get_objects())4445 def __exit__(self, exc_type, exc_value, tb):46 if not hasattr(sys, 'getrefcount'):47 gc.collect()48 new_objects = len(gc.get_objects())49 if new_objects > self.old_objects:50 self.testcase.fail('Example code leaked')51 _gc_lock.release()52 gc.enable()535455class MemoryTestCase(FlaskTestCase):5657 def assert_no_leak(self):58 return _NoLeakAsserter(self)5960 def test_memory_consumption(self):61 app = flask.Flask(__name__)62 @app.route('/')63 def index():64 return flask.render_template('simple_template.html', whiskey=42)6566 def fire():67 with app.test_client() as c:68 rv = c.get('/')69 self.assert_equal(rv.status_code, 200)70 self.assert_equal(rv.data, '<h1>42</h1>')7172 # Trigger caches73 fire()7475 with self.assert_no_leak():76 for x in xrange(10):77 fire()787980def suite():81 suite = unittest.TestSuite()82 suite.addTest(unittest.makeSuite(MemoryTestCase))83 return suite
modified:flask/ctx.py+4 added
148 unmodified lines
149 rv = _request_ctx_stack.pop()150 assert rv is self, 'Popped wrong request context. (%r instead of %r)' \151 % (rv, self)152153 # get rid of circular dependencies at the end of the request154 # so that we don't require the GC to be active.155 rv.request.environ['werkzeug.request'] = None156157 def __enter__(self):158 self.push()159 return self160
19 unmodified lines
modified:CHANGES+6 added
12 unmodified lines
13- The :func:`flask.url_for` function now can also explicitly generate14 URL rules specific to a given HTTP method.15- Logger now only returns the debug log setting if it was not set16 explicitly.17- Unregister a circular dependency between the WSGI environment and18 the request object when shutting down the request. This means that19 environ ``werkzeug.request`` will be `None` after the response was20 returned to the WSGI server but has the advantage that the garbage21 collector is not needed on CPython to tear down the request unless22 the user created circular dependencies themselves.2324Version 0.8.125-------------26
294 unmodified lines