Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure error while opening session pops context #2254

Merged
merged 1 commit into from
Apr 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ Major release, unreleased
adding OPTIONS method when the ``view_func`` argument is not a class.
(`#1489`_).
- ``MethodView`` can inherit method handlers from base classes. (`#1936`_)
- Errors caused while opening the session at the beginning of the request are
handled by the app's error handlers. (`#2254`_)

.. _#1489: https://github.com/pallets/flask/pull/1489
.. _#1936: https://github.com/pallets/flask/pull/1936
.. _#2017: https://github.com/pallets/flask/pull/2017
.. _#2223: https://github.com/pallets/flask/pull/2223
.. _#2254: https://github.com/pallets/flask/pull/2254

Version 0.12.1
--------------
Expand Down
2 changes: 1 addition & 1 deletion flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2002,10 +2002,10 @@ def wsgi_app(self, environ, start_response):
exception context to start the response
"""
ctx = self.request_context(environ)
ctx.push()
error = None
try:
try:
ctx.push()
response = self.full_dispatch_request()
except Exception as e:
error = e
Expand Down
25 changes: 25 additions & 0 deletions tests/test_reqctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pytest

import flask
from flask.sessions import SessionInterface

try:
from greenlet import greenlet
Expand Down Expand Up @@ -193,3 +194,27 @@ def g():

result = greenlets[0].run()
assert result == 42


def test_session_error_pops_context():
class SessionError(Exception):
pass

class FailingSessionInterface(SessionInterface):
def open_session(self, app, request):
raise SessionError()

class CustomFlask(flask.Flask):
session_interface = FailingSessionInterface()

app = CustomFlask(__name__)

@app.route('/')
def index():
# shouldn't get here
assert False

response = app.test_client().get('/')
assert response.status_code == 500
assert not flask.request
assert not flask.current_app