Fix #2935: Copy current session object in copy_current_request_context (#2936)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add session to RequestContext.copy()
authored by Dave Chevell · committed by David Lord · parent 7e714bd · Browse files · 3 files·+22 added−6 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+22 added−6 removed
- tests:+4 added−0 removed
- other:+18 added−6 removed
Provenance
- Commit
e08bcf9- Tree
241ee4c- Author
Dave Chevell <chevell@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
7e714bd
3 files changed+22 added−6 removed
modified:flask/ctx.py+12 added-6 removed
120 unmodified lines121 def copy_current_request_context(f):122 """A helper function that decorates a function to retain the current123 request context. This is useful when working with greenlets. The moment124 the function is decorated a copy of the request context is created and125- then pushed when the function is called.125+ then pushed when the function is called. The current session is also126+ included in the copied request context.127 128 Example::129 130 import gevent2 unmodified lines133 @app.route('/')134 def index():135 @copy_current_request_context136 def do_some_work():136- # do some work here, it can access flask.request like you137- # would otherwise in the view function.137+ # do some work here, it can access flask.request or138+ # flask.session like you would otherwise in the view function.139 ...140 gevent.spawn(do_some_work)141 return 'Regular response'142 133 unmodified lines276 sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in277 that situation, otherwise your unittests will leak memory.278 """279 279- def __init__(self, app, environ, request=None):280+ def __init__(self, app, environ, request=None, session=None):281 self.app = app282 if request is None:283 request = app.request_class(environ)284 self.request = request285 self.url_adapter = app.create_url_adapter(self.request)286 self.flashes = None286- self.session = None287+ self.session = session288 289 # Request contexts can be pushed multiple times and interleaved with290 # other request contexts. Now only if the last level is popped we291 # get rid of them. Additionally if an application context is missing29 unmodified lines321 move a request context to a different thread unless access to the322 request object is locked.323 324 .. versionadded:: 0.10325+326+ .. versionchanged:: 1.1327+ The current session object is used instead of reloading the original328+ data. This prevents `flask.session` pointing to an out-of-date object.329 """330 return self.__class__(self.app,331 environ=self.request.environ,327- request=self.request332+ request=self.request,333+ session=self.session334 )335 336 def match_request(self):337 """Can be overridden by a subclass to hook into the matching126 unmodified linesmodified:tests/test_reqctx.py+4 added
154 unmodified lines155 greenlets = []156 157 @app.route('/')158 def index():159+ flask.session['fizz'] = 'buzz'160 reqctx = flask._request_ctx_stack.top.copy()161 162 def g():163 assert not flask.request2 unmodified lines166 assert flask.request167 assert flask.current_app == app168 assert flask.request.path == '/'169 assert flask.request.args['foo'] == 'bar'170+ assert flask.session.get('fizz') == 'buzz'171 assert not flask.request172 return 42173 174 greenlets.append(greenlet(g))9 unmodified lines184 greenlets = []185 186 @app.route('/')187 def index():188+ flask.session['fizz'] = 'buzz'189 reqctx = flask._request_ctx_stack.top.copy()190 191 @flask.copy_current_request_context192 def g():193 assert flask.request194 assert flask.current_app == app195 assert flask.request.path == '/'196 assert flask.request.args['foo'] == 'bar'197+ assert flask.session.get('fizz') == 'buzz'198 return 42199 200 greenlets.append(greenlet(g))201 return 'Hello World!'30 unmodified linesmodified:CHANGES.rst+6 added
7 unmodified lines8 -----------9 10 Unreleased11 12+- :meth:`flask.RequestContext.copy` includes the current session13+ object in the request context copy. This prevents ``flask.session`` 14+ pointing to an out-of-date object. (`#2935`)15 16+.. _#2935: https://github.com/pallets/flask/issues/293517+18+19 Version 1.0.320 -------------21 22 Unreleased924 unmodified lines