Rewind file streams before rate-limit retries - #2179
Open
CR29-22-2805 wants to merge 1 commit into
Open
Conversation
CR29-22-2805
force-pushed
the
fix-2178-rewind-file-streams
branch
3 times, most recently
from
July 20, 2026 04:10
9a8cc7e to
e9b0358
Compare
bboe
reviewed
Jul 26, 2026
bboe
left a comment
Member
There was a problem hiding this comment.
Thanks, this is a well-scoped fix and the regression test starting the stream at a non-zero offset is a nice touch. Two things:
- Please add a
CHANGES.rstentry underUnreleased— this is a user-visible bug fix. - Silently skipping non-seekable streams leaves the original bug in the case that's hardest to diagnose. I'd note in the
post()docstring that automatic retries only preserve seekable file objects.
Comment on lines
+824
to
+832
| max_attempts = 3 | ||
| attempts = max_attempts | ||
| last_exception: RedditAPIException | None = None | ||
| while attempts > 0: | ||
| attempts -= 1 | ||
| if attempts < max_attempts - 1 and file_positions: | ||
| assert files is not None | ||
| for name, position in file_positions.items(): | ||
| files[name].seek(position) |
Member
There was a problem hiding this comment.
Equivalent behavior, but the loop counter reads more directly than the attempts < max_attempts - 1 arithmetic.
Suggested change
| max_attempts = 3 | |
| attempts = max_attempts | |
| last_exception: RedditAPIException | None = None | |
| while attempts > 0: | |
| attempts -= 1 | |
| if attempts < max_attempts - 1 and file_positions: | |
| assert files is not None | |
| for name, position in file_positions.items(): | |
| files[name].seek(position) | |
| last_exception: RedditAPIException | None = None | |
| for attempt in range(3): | |
| if attempt and file_positions: | |
| assert files is not None | |
| for name, position in file_positions.items(): | |
| files[name].seek(position) |
Member
|
One more thing: if you'd like, please add yourself to - Your Name `@CR29-22-2805 <https://github.com/CR29-22-2805>`_Entirely optional, but the contribution is worth the credit. |
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.
Summary
Rewind seekable multipart file streams before retrying a POST request after a retryable RATELIMIT response.
Without rewinding, the first request consumes the stream and a retry may upload an empty or truncated file.
Changes
Fixes #2178
Tests