René's URL Explorer Experiment


Title: Unable to cancel Server.serve_forever() if a reader is blocked while reading (3.12 regression) · Issue #123720 · python/cpython · GitHub

Open Graph Title: Unable to cancel Server.serve_forever() if a reader is blocked while reading (3.12 regression) · Issue #123720 · python/cpython

X Title: Unable to cancel Server.serve_forever() if a reader is blocked while reading (3.12 regression) · Issue #123720 · python/cpython

Description: Bug report Bug description: Consider this code, slightly simplifying the documentation's TCPServer example code: import asyncio async def handle_echo(reader, writer): print("Reading") data = await reader.read(100) message = data.decode()...

Open Graph Description: Bug report Bug description: Consider this code, slightly simplifying the documentation's TCPServer example code: import asyncio async def handle_echo(reader, writer): print("Reading") data = await ...

X Description: Bug report Bug description: Consider this code, slightly simplifying the documentation's TCPServer example code: import asyncio async def handle_echo(reader, writer): print("Reading")...

Opengraph URL: https://github.com/python/cpython/issues/123720

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unable to cancel Server.serve_forever() if a reader is blocked while reading (3.12 regression)","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nConsider this code, slightly simplifying the documentation's TCPServer example code:\r\n```python\r\nimport asyncio\r\n\r\nasync def handle_echo(reader, writer):\r\n    print(\"Reading\")\r\n    data = await reader.read(100)\r\n    message = data.decode()\r\n    print(f\"Received '{message!r}'\")\r\n\r\n    print(\"Closing the connection\")\r\n    writer.close()\r\n    await writer.wait_closed()\r\n\r\nasync def main():\r\n    server = await asyncio.start_server(handle_echo, \"127.0.0.1\", 8888)\r\n    print(\"Serving forever...\")\r\n    async with server:\r\n        try:\r\n            await server.serve_forever()  \r\n        except asyncio.CancelledError:\r\n            print(\"Cancelled by Ctrl+C\")\r\n            server.close()\r\n\r\nasyncio.run(main())\r\n```\r\n(My code is closer to a `while True: await reader.readline(); ...`, but the above probably suffices as a demonstration)\r\n\r\nRunning this results in a _Serving forever_, and hitting Ctrl+C, results in a _Cancelled by Ctrl+C_, and a normal exit.\r\n\r\nHowever, if in another window we `nc 127.0.0.1 8888`, and leave the connection open, Ctrl+C (SIGINT) does nothing, and a second Ctrl+C is required to terminate. (This however breaks out of the asyncio loop by raising `KeyboardInterrupt()` , as documented).\r\n\r\nSo basically clients can prevent the server from cleanly exiting by just keeping their connection open.\r\n\r\nThis is a regression: this fails with 3.12.5 and 3.13.0-rc1 but works with 3.11.9.\r\n\r\nThis is because (TTBOMK) of this code in `base_events.py`:\r\n```python\r\n        try:\r\n            await self._serving_forever_fut\r\n        except exceptions.CancelledError:\r\n            try:\r\n                self.close()\r\n                await self.wait_closed()\r\n            finally:\r\n                raise\r\n        finally:\r\n            self._serving_forever_fut = None\r\n```\r\n\r\nI believe this to be related to the `wait_closed()` changes, 5d09d11aa0b89aeba187f4f520728ccaf4fc5ac1, 26553695592ad399f735d4dbaf32fd871d0bb1e1 etc. (Cc @gvanrossum). Related issues #104344 and #113538.\r\n\r\nPer @gvanrossum in https://github.com/python/cpython/issues/113538#issuecomment-1874724821:  \"_In 3.10 and before, server.wait_closed() was a no-op, unless you called it before server.close(), in a task (asyncio.create_task(server.wait_closed())). The unclosed connection was just getting abandoned._\"\r\n\r\n`CancelledError()` is caught here, which spawns `wait_closed()` before re-raising the exception. In 3.12+, `wait_closed()`... actually waits for the connection to close, as intended. However, while this prevents the reader task from being abandoned, it does not allow neither _the callers_ of `reader.read()` or `serve_forever()` to catch `CancelledError()` and clean up (such as actually closing the connection, potentially after e.g. a signaling to the client a server close through whatever protocol is implemented here).\r\n\r\nBasically no user code is executed until the client across the network drops the connection.\r\n\r\nAs far as I know, it's currently impossible to handle SIGINTs cleanly with clients blocked in a `read()` without messing with deep asyncio/selector internals, which seems like a pretty serious limitation? Have I missed something?\r\n\r\n### CPython versions tested on:\r\n\r\n3.11, 3.12, 3.13\r\n\r\n### Operating systems tested on:\r\n\r\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-124689\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/paravoid","@type":"Person","name":"paravoid"},"datePublished":"2024-09-05T00:31:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":13},"url":"https://github.com/123720/cpython/issues/123720"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:59fcd090-173a-1375-7bdb-4db9ceb5a35b
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB3F0:FC316:16AB3B0:1E0E5B1:696AE410
html-safe-noncecd2d659aed16be0640c48059acc74aae70d8d5685486e05245e372b76ed09df0
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCM0YwOkZDMzE2OjE2QUIzQjA6MUUwRTVCMTo2OTZBRTQxMCIsInZpc2l0b3JfaWQiOiI0NDk1MjUyMTQ5NzQ1OTM1Mzc2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacfbdc42cbdf21f17b4cbca738e9379e081f442b593ede37d56735ba55e078df11
hovercard-subject-tagissue:2506530852
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/123720/issue_layout
twitter:imagehttps://opengraph.githubassets.com/9d886b0c53098cef71693571e4827887f55b91e71acc7d4df80e40d7d075ba87/python/cpython/issues/123720
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/9d886b0c53098cef71693571e4827887f55b91e71acc7d4df80e40d7d075ba87/python/cpython/issues/123720
og:image:altBug report Bug description: Consider this code, slightly simplifying the documentation's TCPServer example code: import asyncio async def handle_echo(reader, writer): print("Reading") data = await ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameparavoid
hostnamegithub.com
expected-hostnamegithub.com
None5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d
turbo-cache-controlno-preview
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
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release3d84d50b3c75fa36755c3cf392edbc09e626f979
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/123720#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F123720
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
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
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
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/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%2Fissues%2F123720
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%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/123720
Reloadhttps://github.com/python/cpython/issues/123720
Reloadhttps://github.com/python/cpython/issues/123720
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/123720
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k 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.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/123720
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 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/123720
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/123720
Unable to cancel Server.serve_forever() if a reader is blocked while reading (3.12 regression)https://github.com/python/cpython/issues/123720#top
https://github.com/gvanrossum
3.12only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
3.13bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.13%22
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
sprinthttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22sprint%22
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-asynciohttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-asyncio%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/paravoid
https://github.com/paravoid
paravoidhttps://github.com/paravoid
on Sep 5, 2024https://github.com/python/cpython/issues/123720#issue-2506530852
5d09d11https://github.com/python/cpython/commit/5d09d11aa0b89aeba187f4f520728ccaf4fc5ac1
2655369https://github.com/python/cpython/commit/26553695592ad399f735d4dbaf32fd871d0bb1e1
@gvanrossumhttps://github.com/gvanrossum
#104344https://github.com/python/cpython/issues/104344
#113538https://github.com/python/cpython/issues/113538
@gvanrossumhttps://github.com/gvanrossum
#113538 (comment)https://github.com/python/cpython/issues/113538#issuecomment-1874724821
gh-123720: When closing an asyncio server, stop the handlers #124689https://github.com/python/cpython/pull/124689
gvanrossumhttps://github.com/gvanrossum
3.12only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
3.13bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.13%22
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
sprinthttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22sprint%22
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-asynciohttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-asyncio%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
asynciohttps://github.com/orgs/python/projects/29
Sprint 2024https://github.com/orgs/python/projects/32
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.