-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
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
copy_current_request_context copies old session object #2935
Comments
I've found a solution for this - but I don't trust it because I don't have a strong enough knowledge of the Werkzeug internals to know if there are unintended side effects. After making a copy of the request context, you can simply give it a copy of the current session object. Here's a modified version of def copy_current_request_context(f):
top = _request_ctx_stack.top
if top is None:
raise RuntimeError('This decorator can only be used at local scopes '
'when a request context is on the stack. For instance within '
'view functions.')
reqctx = top.copy()
reqctx.session = session.copy()
def wrapper(*args, **kwargs):
with reqctx:
return f(*args, **kwargs)
return update_wrapper(wrapper, f)
With this change, my example produces the expected behaviour. The Flask test suite passes, so there's no obvious side effects I can see. I would love to have the built in |
Expected Behavior
When using
copy_current_request_context
in the middle of a request, I'm expecting it to copy the current state offlask.request
andflask.session
at the time it's called.Actual Behavior
The current state of
flask.request
is copied, butflask.session
is from before the current request and doesn't contain changes made during the current request, beforecopy_current_request_context
was called. To me this seems like unexpected behaviour. If it's not I'd like to understand a bit more about why, and also to find out whether there's any workaround or alternative approach I can take.Example
Here's a fully functional example you can save and run. It adds a test value to
flask.request
andflask.session
during the request, then usescopy_current_request_context
to decorate a method that will retrieve those same values in another thread. Two requests are made; on the first request the copied context can find the new value added toflask.request
but cannot find the value added toflask.session
. On the second request, the copied context can find the value added toflask.request
, and now finds the value that was previously added toflask.session
in the first request.Example output:
Environment
Also, for whatever it's worth: I'm investigating this as part of improving this project: https://github.com/dchevell/flask-executor
The text was updated successfully, but these errors were encountered: