gh-150107: Fix asyncio sendfile fallback ignoring non-zero offset#150259
Closed
grantlouisherman wants to merge 1 commit into
Closed
gh-150107: Fix asyncio sendfile fallback ignoring non-zero offset#150259grantlouisherman wants to merge 1 commit into
grantlouisherman wants to merge 1 commit into
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
3ef70d1 to
f7cd6b0
Compare
The fallback paths in BaseEventLoop._sock_sendfile_fallback and _sendfile_fallback only seeked the file when offset was truthy, so an offset of 0 was respected but later non-zero offsets were dropped when the file lacked seek tracking. Seek whenever the file supports seek(). Also seek the CRT file pointer on Windows TransmitFile, which ignores OVERLAPPED.Offset for handles not opened with FILE_FLAG_OVERLAPPED. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2461b09 to
f8d2bdc
Compare
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.
Original Bug Report:
Bug report
Bug description:
BaseEventLoop._sock_sendfile_fallback(https://github.com/python/cpython/blob/main/Lib/asyncio/base_events.py#L971) erroneously doesn't seek when passed the offset 0.This check in the function doesn't call
file.seekif offset is falsey. If the file in question has a file pointer that is not currently at the 0 offset then this will erroneously send from the current offset and not the start of the file as desired. This can happen whensendfileis called with the destination socket is an SSL socket.Repro code (generated by Claude), because of needing to create an SSL socket it's a bit involved - including making a self signed cert with the openssl command.
The relevant lines are the line
f.seek(len(CONTENT))and the linesent = await loop.sendfile(writer.transport, f, offset=0). Most of the rest is setup and teardown.CPython versions tested on:
3.12, 3.13
Operating systems tested on:
Linux
Fix
I think what I did was correct or understood the issue, but essentially as far as I could understand
0is always falsey so if offset was ever set to zero than the file.seek wasent happening even though 0 offset is a legitimate offset. I simply just did an instance on offset to make sure it is an int and then the f.seek goes through. I validated with the repro command and passing unit tests.