Skip to content

Commit 13754b6

Browse files
committed
ensure error while opening session pops context
errors will be handled by the app error handlers closes pallets#1538, closes pallets#1528
1 parent f22da31 commit 13754b6

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ Major release, unreleased
2525
adding OPTIONS method when the ``view_func`` argument is not a class.
2626
(`#1489`_).
2727
- ``MethodView`` can inherit method handlers from base classes. (`#1936`_)
28+
- Errors caused while opening the session at the beginning of the request are
29+
handled by the app's error handlers. (`#2254`_)
2830

2931
.. _#1489: https://github.com/pallets/flask/pull/1489
3032
.. _#1936: https://github.com/pallets/flask/pull/1936
3133
.. _#2017: https://github.com/pallets/flask/pull/2017
3234
.. _#2223: https://github.com/pallets/flask/pull/2223
35+
.. _#2254: https://github.com/pallets/flask/pull/2254
3336

3437
Version 0.12.1
3538
--------------

flask/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,10 +2002,10 @@ def wsgi_app(self, environ, start_response):
20022002
exception context to start the response
20032003
"""
20042004
ctx = self.request_context(environ)
2005-
ctx.push()
20062005
error = None
20072006
try:
20082007
try:
2008+
ctx.push()
20092009
response = self.full_dispatch_request()
20102010
except Exception as e:
20112011
error = e

tests/test_reqctx.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pytest
1313

1414
import flask
15+
from flask.sessions import SessionInterface
1516

1617
try:
1718
from greenlet import greenlet
@@ -193,3 +194,27 @@ def g():
193194

194195
result = greenlets[0].run()
195196
assert result == 42
197+
198+
199+
def test_session_error_pops_context():
200+
class SessionError(Exception):
201+
pass
202+
203+
class FailingSessionInterface(SessionInterface):
204+
def open_session(self, app, request):
205+
raise SessionError()
206+
207+
class CustomFlask(flask.Flask):
208+
session_interface = FailingSessionInterface()
209+
210+
app = CustomFlask(__name__)
211+
212+
@app.route('/')
213+
def index():
214+
# shouldn't get here
215+
assert False
216+
217+
response = app.test_client().get('/')
218+
assert response.status_code == 500
219+
assert not flask.request
220+
assert not flask.current_app

0 commit comments

Comments
 (0)