Repository navigation
Fix #2935: Copy current session object in copy_current_request_context - #2936
Conversation
|
Looks good, but please shorten your commit messages a bit. General recommendation is max 51 chars in the first line, and yours are even beyond the limit where GitHub truncates them. Also, I wonder if we should do a deep copy on the session... otherwise people might get a bad surprise in case of mutable objects stored there. |
b041818 to
732d77b
Compare
|
Fixed the commit messages @ThiefMaster. Re: deep copying, you're right - though it's worth noting that the same issue is already true for |
|
@ThiefMaster is there a chance users would store things in the session object that would fail a deepcopy? I don't want to break people's code. Perhaps a more conservative approach would be: try:
reqctx.session = deepcopy(session)
except TypeError:
reqctx.session = session.copy() |
|
By default only the object types handled in there can be stored in a session |
|
Ah, no worries then. Alright, switched to deepcopy. |
01ef320 to
83b258a
Compare
|
Anything further needed to review this @ThiefMaster ? |
| def index(): | ||
| flask.session['fizz'] = 'buzz' | ||
| reqctx = flask._request_ctx_stack.top.copy() | ||
| reqctx.session = deepcopy(flask.session) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
Perhaps we should expose a method on the session interface for copying, so the dev can override deepcopy vs copy? |
|
@davidism do you mean along the lines of, Do you think there are cases to want both behaviours? deepcopy seems safer, since the intent of the method isn't to provide references back to the original objects. I can imagine a case where a user would prefer deepcopy, but not one where they'd prefer a shallow copy. |
|
Maybe an attribute on the Flask class or the session interface (pointing to the copy function to use)? I'd rather subclass once instead of having to specify this again and again. |
|
I can do that. First, though, what's the benefit here? What does offering shallow vs deep copy achieve? Adding API surface increases cost (testability, complexity, documentation, etc.) and given the purpose of this helper function is for the fairly narrow use case of creating a context copy for use in threads or greenlets I'm not sure what shallow copies buy us. Happy to be overruled but I'd like to make sure before I touch any other code. |
|
Just checking in with @davidism to see if there's value / benefit in adding new API's to allow multiple copy behaviours - I'm still erring on the side of not, since this is already pretty edge-casey and it seems like an edge case of an edge case. Still, open to other opinions. |
|
A separate problem is that I would like to use |
|
@okeuday To confirm, that's talking about deepcopying the request, not the session, right? (Of course, it would make sense to go all or nothing) Can you deepcopy a request? I mean, you can't do it directly - you'll get What are your thoughts on reverting this change back to the original shallow copy implementation, then opening a separate issue for a true deepcopy for both |
|
@dchevell I would agree that a deep copy is best that handles the It should be better to modify |
|
@okeuday not sure I agree - the scope of this PR was simply, copies of the current request contain the previous session object. Shallow copy / deep copy covers the same area of code, but it's an entirely separate set of concerns. Other than touching the same code, I don't see a relationship between them. I agree that a deepcopy has value, just not that it fits within the scope of #2935 |
|
If #2960 covers the need for offering an API for both shallow and deepcopying, I think we should revert the deepcopy change here back to the original shallow copy implementation, then a fix for #2960 should implement a deepcopy option which implements that behaviour for the request and session objects |
|
Looking at the docs for
It's clear copying was never meant to use deepcopy, it requires the user to lock around multiple threads modifying. So let's go back to |
83b258a to
1942f11
Compare
1942f11 to
b43366d
Compare
|
Thanks for the pointers @davidism - would you mind checking the latest diff to confirm if it matches what you were thinking? |
b43366d to
6378a9d
Compare
Updates
flask.copy_current_request_contextto add a copy of the current session object to the request context copy. This preventsflask.sessionpointing to an out-of-date object.pytestversionaddedorversionchangeddirectives to relevant docstringsPlease refer to #2935 for background on this