Stop RebufferedBytesIO from hanging at EOF (fixes #988) - #1140
Open
binggao1230 wants to merge 1 commit into
Open
binggao1230 wants to merge 1 commit into
binggao1230 wants to merge 1 commit into
Conversation
RebufferedBytesIO.read/write polled the substream in a loop and treated both None (non-blocking would-block) and empty bytes (EOF) the same way: sleep(0) then keep looping. A stream that returned b'' at end of file therefore spun forever instead of stopping. Distinguish the two cases: keep polling on None, but break on empty bytes so read() falls through to its 'could not read enough bytes' IOError and write() stops trying to backfill. Fixes construct#988.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
RebufferedBytesIO.read(andwrite) poll the underlying substream in a loop. They treated two distinct outcomes identically:None— a non-blocking stream that has no data yetb""— the stream has reached end of fileBoth hit
if not newdata: sleep(0); continue, so a substream that signals EOF by returning empty bytes caused construct to spin forever instead of stopping. This is the hang reported in #988.Fix
Distinguish the two cases:
newdata is None→ still keep polling (sleep(0); continue), preserving non-blocking behaviour.breakout of the loop.After breaking,
readfalls through to its existingcould not read enough bytesIOError, andwritestops trying to backfill. The same fix is applied to both methods (thewriteloop had the same pattern and could also spin).Tests
Added
test_rebuffered_reading_past_eofwhich asserts an over-read now raisesIOErrorrather than hanging. Existingtest_bitstream.pytests still pass.