Improve application context popping
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Exceptions during teardown handling will no longer leave application contexts lingering around. This fixes #1767
authored by Armin Ronacher · committed by Armin Ronacher · parent 87787b1 · Browse files · 3 files·+61 added−33 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+61 added−33 removed
- tests:+22 added−0 removed
- other:+39 added−33 removed
Provenance
- Commit
8482ce6- Tree
ce53875- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
87787b1
3 files changed+61 added−33 removed
modified:flask/ctx.py+37 added-33 removed
179 unmodified lines180 appcontext_pushed.send(self.app)181 182 def pop(self, exc=_sentinel):183 """Pops the app context."""184+ try:184- self._refcnt -= 1185- if self._refcnt <= 0:186- if exc is _sentinel:187- exc = sys.exc_info()[1]188- self.app.do_teardown_appcontext(exc)189- rv = _app_ctx_stack.pop()185+ self._refcnt -= 1186+ if self._refcnt <= 0:187+ if exc is _sentinel:188+ exc = sys.exc_info()[1]189+ self.app.do_teardown_appcontext(exc)190+ finally:191+ rv = _app_ctx_stack.pop()192 assert rv is self, 'Popped wrong app context. (%r instead of %r)' \193 % (rv, self)194 appcontext_popped.send(self.app)195 146 unmodified lines342 Added the `exc` argument.343 """344 app_ctx = self._implicit_app_ctx_stack.pop()345 346+ try:344- clear_request = False345- if not self._implicit_app_ctx_stack:346- self.preserved = False347- self._preserved_exc = None348- if exc is _sentinel:349- exc = sys.exc_info()[1]350- self.app.do_teardown_request(exc)347+ clear_request = False348+ if not self._implicit_app_ctx_stack:349+ self.preserved = False350+ self._preserved_exc = None351+ if exc is _sentinel:352+ exc = sys.exc_info()[1]353+ self.app.do_teardown_request(exc)354 352- # If this interpreter supports clearing the exception information353- # we do that now. This will only go into effect on Python 2.x,354- # on 3.x it disappears automatically at the end of the exception355- # stack.356- if hasattr(sys, 'exc_clear'):357- sys.exc_clear()355+ # If this interpreter supports clearing the exception information356+ # we do that now. This will only go into effect on Python 2.x,357+ # on 3.x it disappears automatically at the end of the exception358+ # stack.359+ if hasattr(sys, 'exc_clear'):360+ sys.exc_clear()361 359- request_close = getattr(self.request, 'close', None)360- if request_close is not None:361- request_close()362- clear_request = True362+ request_close = getattr(self.request, 'close', None)363+ if request_close is not None:364+ request_close()365+ clear_request = True366+ finally:367+ rv = _request_ctx_stack.pop()368 364- rv = _request_ctx_stack.pop()365- assert rv is self, 'Popped wrong request context. (%r instead of %r)' \366- % (rv, self)369+ # get rid of circular dependencies at the end of the request370+ # so that we don't require the GC to be active.371+ if clear_request:372+ rv.request.environ['werkzeug.request'] = None373 368- # get rid of circular dependencies at the end of the request369- # so that we don't require the GC to be active.370- if clear_request:371- rv.request.environ['werkzeug.request'] = None374+ # Get rid of the app as well if necessary.375+ if app_ctx is not None:376+ app_ctx.pop(exc)377 373- # Get rid of the app as well if necessary.374- if app_ctx is not None:375- app_ctx.pop(exc)378+ assert rv is self, 'Popped wrong request context. ' \379+ '(%r instead of %r)' % (rv, self)380 381 def auto_pop(self, exc):382 if self.request.environ.get('flask._preserve_context') or \383 (exc is not None and self.app.preserve_context_on_exception):27 unmodified linesmodified:tests/test_appctx.py+22 added
144 unmodified lines145 res = c.get('/')146 assert res.status_code == 200147 assert res.data == b''148 assert called == ['request', 'app']149+150+151+def test_clean_pop():152+ called = []153+ app = flask.Flask(__name__)154+155+ @app.teardown_request156+ def teardown_req(error=None):157+ 1 / 0158+159+ @app.teardown_appcontext160+ def teardown_app(error=None):161+ called.append('TEARDOWN')162+163+ try:164+ with app.test_request_context():165+ called.append(flask.current_app.name)166+ except ZeroDivisionError:167+ pass168+169+ assert called == ['test_appctx', 'TEARDOWN']170+ assert not flask.current_appmodified:CHANGES+2 added
75 unmodified lines76 - ``flask.ext`` is now deprecated (pull request ``#1484``).77 - ``send_from_directory`` now raises BadRequest if the filename is invalid on78 the server OS (pull request ``#1763``).79 - Added the ``JSONIFY_MIMETYPE`` configuration variable (pull request ``#1728``).80+- Exceptions during teardown handling will no longer leave bad application81+ contexts lingering around.82 83 Version 0.10.284 --------------85 480 unmodified lines