Replace shared global _empty_stream with separate instances#1340
Merged
davidism merged 1 commit intopallets:masterfrom Aug 4, 2018
Merged
Replace shared global _empty_stream with separate instances#1340davidism merged 1 commit intopallets:masterfrom
davidism merged 1 commit intopallets:masterfrom
Conversation
Member
|
Makes sense. Would you add a changelog entry? |
6e3e3e4 to
0d23117
Compare
If a test closed the _empty_stream, subsequent requests could reuse this closed stream, causing request.get_data() and other methods to fail.
0d23117 to
baf2051
Compare
Contributor
Author
|
Thanks @davidism. I've amended the commit with a changelog entry and the commit is now passing lint. |
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The use of a shared
_empty_streaminstance can cause subtle failures when a test or process closes the shared stream.For example, if a test initializes a request with a file upload
FileStoragewith a falsystreamargument, theFileStoragewill use the shared_empty_stream:werkzeug/werkzeug/datastructures.py
Line 2642 in ac230a2
When the request is torn down, it will close the shared
_empty_stream:werkzeug/werkzeug/wrappers.py
Line 416 in ac230a2
werkzeug/werkzeug/datastructures.py
Line 2736 in ac230a2
Subsequently, if a test environment is created with no specified
input_stream, it will use the closed_empty_stream:werkzeug/werkzeug/test.py
Line 599 in 816a1e9
Now calls to
request.get_data()and other operations on the stream will fail.In the case where we discovered this behavior at Patreon, two unrelated tests were causing a failure: one which initialized an
FileStoragewith_empty_stream, and one which calledrequest.get_data().I believe that replacing this shared instance with individually created
BytesIObuffers would reduce the chances of such spooky shared global bugs without creating much overhead. What do you think?