Add test for file-like read() method sending fewer bytes than requested #644
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.
We have a
while
loop logic inAVIOFileLikeContext::read
that handles cases where the read() method of the file-like object sends fewer bytes than requested:torchcodec/src/torchcodec/_core/AVIOFileLikeContext.cpp
Line 37 in 2c137e7
Until now, this logic was untested, so this PR adds a test for this.
Side note
I am genuinely surprised that this actually works. I tried hard to build a test that would fail, but I couldn't. I guess it's a good thing. The bad thing is: I don't understand how this actually works. It looks like FFmpeg is being really smart and re-requests the bytes it didn't originally get, and there's the internal buffer logic going on as well. I played a bit with this snippet, trying to take the
diff
between a "good" reader and a "bad" reader.The bad reader ends up reading fewer bytes in total, and it's impressive to see that things still work even if
read()
returns 0 bytes sometimes. And yet, results appear to be correct. So, I'm still puzzled, but I'll accept that this works until proven otherwise.Logs with
read_less=True
:logs with
read_less=False