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

Replace shared global _empty_stream with separate instances #1340

Merged
merged 1 commit into from Aug 4, 2018

Conversation

Projects
None yet
2 participants
@chromakode
Copy link
Contributor

commented Aug 3, 2018

The use of a shared _empty_stream instance 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 FileStorage with a falsy stream argument, the FileStorage will use the shared _empty_stream:

self.stream = stream or _empty_stream

When the request is torn down, it will close the shared _empty_stream:

value.close()

self.stream.close()

Subsequently, if a test environment is created with no specified input_stream, it will use the closed _empty_stream:

input_stream = _empty_stream

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 FileStorage with _empty_stream, and one which called request.get_data().

I believe that replacing this shared instance with individually created BytesIO buffers would reduce the chances of such spooky shared global bugs without creating much overhead. What do you think?

@davidism

This comment has been minimized.

Copy link
Member

commented Aug 3, 2018

Makes sense. Would you add a changelog entry?

@chromakode chromakode force-pushed the chromakode:remove-shared-empty-stream branch 2 times, most recently from 6e3e3e4 to 0d23117 Aug 3, 2018

Replace shared global _empty_stream with separate instances
If a test closed the _empty_stream, subsequent requests could reuse this
closed stream, causing request.get_data() and other methods to fail.

@chromakode chromakode force-pushed the chromakode:remove-shared-empty-stream branch from 0d23117 to baf2051 Aug 3, 2018

@chromakode

This comment has been minimized.

Copy link
Contributor Author

commented Aug 3, 2018

Thanks @davidism. I've amended the commit with a changelog entry and the commit is now passing lint.

@davidism davidism merged commit 8c4e6b5 into pallets:master Aug 4, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@davidism

This comment has been minimized.

Copy link
Member

commented Aug 4, 2018

Thanks!

@chromakode chromakode deleted the chromakode:remove-shared-empty-stream branch Aug 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.