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

Only open session if request does not have open session #2354

Merged
merged 2 commits into from
Jun 3, 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
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ Major release, unreleased
of the generic bad request message. (`#2348`_)
- Allow registering new tags with ``TaggedJSONSerializer`` to support
storing other types in the session cookie. (`#2352`_)
- Only open the session if the request has not been pushed onto the context
stack yet. This allows ``stream_with_context`` generators to access the same
session that the containing view uses. (`#2354`_)

.. _#1489: https://github.com/pallets/flask/pull/1489
.. _#1621: https://github.com/pallets/flask/pull/1621
Expand All @@ -87,6 +90,7 @@ Major release, unreleased
.. _#2326: https://github.com/pallets/flask/pull/2326
.. _#2348: https://github.com/pallets/flask/pull/2348
.. _#2352: https://github.com/pallets/flask/pull/2352
.. _#2354: https://github.com/pallets/flask/pull/2354

Version 0.12.2
--------------
Expand Down
19 changes: 11 additions & 8 deletions flask/ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,18 @@ def push(self):

_request_ctx_stack.push(self)

# Open the session at the moment that the request context is
# available. This allows a custom open_session method to use the
# request context (e.g. code that access database information
# stored on `g` instead of the appcontext).
session_interface = self.app.session_interface
self.session = session_interface.open_session(self.app, self.request)

# Open the session at the moment that the request context is available.
# This allows a custom open_session method to use the request context.
# Only open a new session if this is the first time the request was
# pushed, otherwise stream_with_context loses the session.
if self.session is None:
self.session = session_interface.make_null_session(self.app)
session_interface = self.app.session_interface
self.session = session_interface.open_session(
self.app, self.request
)

if self.session is None:
self.session = session_interface.make_null_session(self.app)

def pop(self, exc=_sentinel):
"""Pops the request context and unbinds it by doing that. This will
Expand Down
14 changes: 14 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,20 @@ def generate():
assert rv.data == b'Hello World!'
assert called == [42]

def test_stream_keeps_session(self, app, client):
@app.route('/')
def index():
flask.session['test'] = 'flask'

@flask.stream_with_context
def gen():
yield flask.session['test']

return flask.Response(gen())

rv = client.get('/')
assert rv.data == b'flask'


class TestSafeJoin(object):
def test_safe_join(self):
Expand Down