René's URL Explorer Experiment


Title: gh-150107: Fix asyncio sendfile fallback ignoring non-zero offset by grantlouisherman · Pull Request #150270 · python/cpython · GitHub

Open Graph Title: gh-150107: Fix asyncio sendfile fallback ignoring non-zero offset by grantlouisherman · Pull Request #150270 · python/cpython

X Title: gh-150107: Fix asyncio sendfile fallback ignoring non-zero offset by grantlouisherman · Pull Request #150270 · python/cpython

Description: 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. if offset: file.seek(offset) This check in the function doesn't call file.seek if 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 when sendfile is 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. import asyncio import os import ssl import subprocess import sys import tempfile from pathlib import Path CONTENT = b"X" * 1000 def make_throwaway_cert(tmp: Path) -> tuple[Path, Path]: cert = tmp / "cert.pem" key = tmp / "key.pem" subprocess.run( [ "openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-keyout", str(key), "-out", str(cert), "-days", "1", "-subj", "/CN=localhost", ], check=True, capture_output=True, ) return cert, key async def main() -> int: with tempfile.TemporaryDirectory() as tmp_: tmp = Path(tmp_) cert, key = make_throwaway_cert(tmp) data_path = tmp / "data" data_path.write_bytes(CONTENT) # SSL server that drains whatever it receives. server_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) server_ctx.load_cert_chain(cert, key) received = 0 done = asyncio.Event() async def handle(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: nonlocal received try: while chunk := await reader.read(4096): received += len(chunk) finally: writer.close() done.set() server = await asyncio.start_server(handle, "127.0.0.1", 0, ssl=server_ctx) port = server.sockets[0].getsockname()[1] # SSL client → transport's `_sendfile_compatible == UNSUPPORTED` → fallback path. client_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) client_ctx.check_hostname = False client_ctx.verify_mode = ssl.CERT_NONE _, writer = await asyncio.open_connection( "127.0.0.1", port, ssl=client_ctx, server_hostname="localhost" ) # Open the file and pre-advance its position. In the real-world bug # this happens because the calling code previously read through the # same fh (e.g. to parse an HTTP header). f = open(data_path, "rb", buffering=0) f.seek(len(CONTENT)) # file position is at EOF # Send from offset=0. Expectation: 1000 bytes transferred. loop = asyncio.get_running_loop() sent = await loop.sendfile(writer.transport, f, offset=0) writer.close() await writer.wait_closed() await done.wait() server.close() await server.wait_closed() f.close() print(f"Python: {sys.version}") print(f"file size: {len(CONTENT)}") print(f"file.tell() before sendfile: {len(CONTENT)} (EOF)") print(f"loop.sendfile(..., offset=0) returned: {sent}") print(f"server received: {received} bytes") print() if sent == 0 and received == 0: print("BUG REPRODUCED: offset=0 was ignored; the fallback read from " "file.tell() instead of from the requested offset.") return 1 else: print("Bug NOT reproduced on this Python build.") return 0 sys.exit(asyncio.run(main())) The relevant lines are the line f.seek(len(CONTENT)) and the line sent = 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 0 is 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. Issue: gh-150107

Open Graph Description: 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 whe...

X Description: 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...

Opengraph URL: https://github.com/python/cpython/pull/150270

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:61d25836-fdb0-5b6a-16ec-e5230a64901a
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idA6AE:170C03:218FF:2BBFD:6A5136AF
html-safe-nonce4f3702dda9b9e80a3e3d28d64e8656e273898c4088c5862b96673d32ea093b75
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNkFFOjE3MEMwMzoyMThGRjoyQkJGRDo2QTUxMzZBRiIsInZpc2l0b3JfaWQiOiIzNjM0ODEwODk1NzQwMjUzODcxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac6f3785ab5d191e812383925d13705c85a79e11f3c7baf705d5457a2c77fb5acd
hovercard-subject-tagpull_request:3732823544
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/150270/files
twitter:imagehttps://avatars.githubusercontent.com/u/5606705?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/5606705?s=400&v=4
og:image:altOriginal 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 whe...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Nonee840f405b2718e8f2d55aafa9ff27dbce17e29d0c253011d05ea0fea3c78baff
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release4ae6027e8ba70c79dc08249bb6cdc1a09d788496
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/150270/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F150270%2Ffiles
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F150270%2Ffiles
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/150270/files
Reloadhttps://github.com/python/cpython/pull/150270/files
Reloadhttps://github.com/python/cpython/pull/150270/files
Please reload this pagehttps://github.com/python/cpython/pull/150270/files
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/pull/150270/files
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.7k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
kumaraditya303https://github.com/kumaraditya303
python:mainhttps://github.com/python/cpython/tree/main
grantlouisherman:bugfix-150107-asyncio-offsethttps://github.com/grantlouisherman/cpython/tree/bugfix-150107-asyncio-offset
Conversation 15 https://github.com/python/cpython/pull/150270
Commits 2 https://github.com/python/cpython/pull/150270/commits
Checks 62 https://github.com/python/cpython/pull/150270/checks
Files changed https://github.com/python/cpython/pull/150270/files
Please reload this pagehttps://github.com/python/cpython/pull/150270/files
gh-150107: Fix asyncio sendfile fallback ignoring non-zero offset https://github.com/python/cpython/pull/150270/files#top
Show all changes 2 commits https://github.com/python/cpython/pull/150270/files
19edc6b Fix asyncio sendfile fallback ignoring non-zero offset grantlouisherman May 22, 2026 https://github.com/python/cpython/pull/150270/commits/19edc6b3004fafd58bf3e010a150b17069c68d0b
6d03e4a Update Lib/test/test_asyncio/test_sendfile.py grantlouisherman May 28, 2026 https://github.com/python/cpython/pull/150270/commits/6d03e4a0d3b9841c8aa9a2d90229e08ff1af3c10
Clear filters https://github.com/python/cpython/pull/150270/files
Please reload this pagehttps://github.com/python/cpython/pull/150270/files
Please reload this pagehttps://github.com/python/cpython/pull/150270/files
base_events.py https://github.com/python/cpython/pull/150270/files#diff-7962e1627bc1bfbce13bef0be78d5ef7fc962f99bbc524a199ef489852ca1344
proactor_events.py https://github.com/python/cpython/pull/150270/files#diff-15176d64608861034db7168743f4f116f4cb33422cf685a2b911481c55826aa2
unix_events.py https://github.com/python/cpython/pull/150270/files#diff-59eb90ea71c294e306140c76e079c867bcd69a6faf7f96831ce7152eb6e37fc4
windows_events.py https://github.com/python/cpython/pull/150270/files#diff-4d950142ac4382096cb08d3d7f9b377ff8208085ce5d5f76ad0d0dadc7a77205
test_sendfile.py https://github.com/python/cpython/pull/150270/files#diff-711b6f68342493b9a0f3fc828d2e8383be9996bd8c2513e807d11b5bdbb84a8f
2026-05-22-17-09-28.gh-issue-150107.GD72-D.rst https://github.com/python/cpython/pull/150270/files#diff-87b0c3b6f7359c1430ff23b5766275adbe5bd7d6f32ab19466479370dab36ea2
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L379
Lib/asyncio/base_events.pyhttps://github.com/python/cpython/pull/150270/files#diff-7962e1627bc1bfbce13bef0be78d5ef7fc962f99bbc524a199ef489852ca1344
View file https://github.com/grantlouisherman/cpython/blob/6d03e4a0d3b9841c8aa9a2d90229e08ff1af3c10/Lib/asyncio/base_events.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/150270/{{ revealButtonHref }}
https://github.com/python/cpython/pull/150270/files#diff-7962e1627bc1bfbce13bef0be78d5ef7fc962f99bbc524a199ef489852ca1344
https://github.com/python/cpython/pull/150270/files#diff-7962e1627bc1bfbce13bef0be78d5ef7fc962f99bbc524a199ef489852ca1344
https://github.com/python/cpython/pull/150270/files#diff-7962e1627bc1bfbce13bef0be78d5ef7fc962f99bbc524a199ef489852ca1344
https://github.com/python/cpython/pull/150270/files#diff-7962e1627bc1bfbce13bef0be78d5ef7fc962f99bbc524a199ef489852ca1344
https://github.com/python/cpython/pull/150270/files#diff-7962e1627bc1bfbce13bef0be78d5ef7fc962f99bbc524a199ef489852ca1344
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L379
Lib/asyncio/proactor_events.pyhttps://github.com/python/cpython/pull/150270/files#diff-15176d64608861034db7168743f4f116f4cb33422cf685a2b911481c55826aa2
View file https://github.com/grantlouisherman/cpython/blob/6d03e4a0d3b9841c8aa9a2d90229e08ff1af3c10/Lib/asyncio/proactor_events.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/150270/{{ revealButtonHref }}
https://github.com/python/cpython/pull/150270/files#diff-15176d64608861034db7168743f4f116f4cb33422cf685a2b911481c55826aa2
https://github.com/python/cpython/pull/150270/files#diff-15176d64608861034db7168743f4f116f4cb33422cf685a2b911481c55826aa2
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L379
Lib/asyncio/unix_events.pyhttps://github.com/python/cpython/pull/150270/files#diff-59eb90ea71c294e306140c76e079c867bcd69a6faf7f96831ce7152eb6e37fc4
View file https://github.com/grantlouisherman/cpython/blob/6d03e4a0d3b9841c8aa9a2d90229e08ff1af3c10/Lib/asyncio/unix_events.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/150270/{{ revealButtonHref }}
https://github.com/python/cpython/pull/150270/files#diff-59eb90ea71c294e306140c76e079c867bcd69a6faf7f96831ce7152eb6e37fc4
https://github.com/python/cpython/pull/150270/files#diff-59eb90ea71c294e306140c76e079c867bcd69a6faf7f96831ce7152eb6e37fc4
https://github.com/python/cpython/pull/150270/files#diff-59eb90ea71c294e306140c76e079c867bcd69a6faf7f96831ce7152eb6e37fc4
https://github.com/python/cpython/pull/150270/files#diff-59eb90ea71c294e306140c76e079c867bcd69a6faf7f96831ce7152eb6e37fc4
https://github.com/python/cpython/pull/150270/files#diff-59eb90ea71c294e306140c76e079c867bcd69a6faf7f96831ce7152eb6e37fc4
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L379
Lib/asyncio/windows_events.pyhttps://github.com/python/cpython/pull/150270/files#diff-4d950142ac4382096cb08d3d7f9b377ff8208085ce5d5f76ad0d0dadc7a77205
View file https://github.com/grantlouisherman/cpython/blob/6d03e4a0d3b9841c8aa9a2d90229e08ff1af3c10/Lib/asyncio/windows_events.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/150270/{{ revealButtonHref }}
https://github.com/python/cpython/pull/150270/files#diff-4d950142ac4382096cb08d3d7f9b377ff8208085ce5d5f76ad0d0dadc7a77205
https://github.com/python/cpython/pull/150270/files#diff-4d950142ac4382096cb08d3d7f9b377ff8208085ce5d5f76ad0d0dadc7a77205
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L380
Lib/test/test_asyncio/test_sendfile.pyhttps://github.com/python/cpython/pull/150270/files#diff-711b6f68342493b9a0f3fc828d2e8383be9996bd8c2513e807d11b5bdbb84a8f
View file https://github.com/grantlouisherman/cpython/blob/6d03e4a0d3b9841c8aa9a2d90229e08ff1af3c10/Lib/test/test_asyncio/test_sendfile.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/150270/{{ revealButtonHref }}
https://github.com/python/cpython/pull/150270/files#diff-711b6f68342493b9a0f3fc828d2e8383be9996bd8c2513e807d11b5bdbb84a8f
Please reload this pagehttps://github.com/python/cpython/pull/150270/files
https://github.com/python/cpython/pull/150270/files#diff-711b6f68342493b9a0f3fc828d2e8383be9996bd8c2513e807d11b5bdbb84a8f
Misc/NEWS.d/next/Core_and_Builtins/2026-05-22-17-09-28.gh-issue-150107.GD72-D.rsthttps://github.com/python/cpython/pull/150270/files#diff-87b0c3b6f7359c1430ff23b5766275adbe5bd7d6f32ab19466479370dab36ea2
View file https://github.com/grantlouisherman/cpython/blob/6d03e4a0d3b9841c8aa9a2d90229e08ff1af3c10/Misc/NEWS.d/next/Core_and_Builtins/2026-05-22-17-09-28.gh-issue-150107.GD72-D.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/150270/{{ revealButtonHref }}
Please reload this pagehttps://github.com/python/cpython/pull/150270/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.