René's URL Explorer Experiment


Title: asyncio: socket leak and silent error when transport creation fails for an accepted connection · Issue #155934 · python/cpython · GitHub

Open Graph Title: asyncio: socket leak and silent error when transport creation fails for an accepted connection · Issue #155934 · python/cpython

X Title: asyncio: socket leak and silent error when transport creation fails for an accepted connection · Issue #155934 · python/cpython

Description: Bug report Bug description When an incoming connection has been accepted and protocol_factory() (or transport creation) then raises inside BaseSelectorEventLoop._accept_connection2() (Lib/asyncio/selector_events.py), two things go wrong....

Open Graph Description: Bug report Bug description When an incoming connection has been accepted and protocol_factory() (or transport creation) then raises inside BaseSelectorEventLoop._accept_connection2() (Lib/asyncio/s...

X Description: Bug report Bug description When an incoming connection has been accepted and protocol_factory() (or transport creation) then raises inside BaseSelectorEventLoop._accept_connection2() (Lib/asyncio/s...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"asyncio: socket leak and silent error when transport creation fails for an accepted connection","articleBody":"# Bug report\n\n## Bug description\n\nWhen an incoming connection has been accepted and `protocol_factory()` (or transport creation) then raises inside `BaseSelectorEventLoop._accept_connection2()` (`Lib/asyncio/selector_events.py`), two things go wrong.\n\nFirst, **The accepted socket is never closed.** Ownership of `conn` only passes to the transport once it is created; if creation fails, nothing closes the socket, so the `fd` leaks until garbage collection\n(Log: `ResourceWarning: unclosed \u003csocket.socket ...>` with the server's address as `laddr`).\n\nSecond, **The error is reported only in debug mode.** The `except BaseException` block calls `call_exception_handler()` behind `if self._debug:`, so with the default `debug=False` the failure is not reported anywhere. The client sees an established connection that never answers (the TCP handshake and ACKs come from the kernel), while the server keeps no record that the connection ever existed.\n\nThis is the server-side counterpart of gh-153133, which fixed the same socket leak in `create_connection()` (gh-153134).\nIt is also inconsistent with `_accept_connection()` itself, which reports accept-stage errors (e.g. `EMFILE`, `ENFILE`, ...) via `call_exception_handler()` unconditionally.\n\n## Reproducer\n\n```python\nimport asyncio\nimport sys\n\nasync def main(debug):\n    loop = asyncio.get_running_loop()\n    loop.set_debug(debug)\n    handler_calls = []\n    loop.set_exception_handler(lambda l, ctx: handler_calls.append(ctx.get(\"message\")))\n\n    def bad_factory():\n        raise RuntimeError(\"boom in protocol_factory\")\n\n    server = await loop.create_server(bad_factory, \"127.0.0.1\", 0)\n    port = server.sockets[0].getsockname()[1]\n\n    reader, writer = await asyncio.open_connection(\"127.0.0.1\", port)\n    await asyncio.sleep(0.2)\n\n    writer.close()\n    server.close()\n    await server.wait_closed()\n    print(f\"debug={debug}  exception_handler calls={len(handler_calls)}  {handler_calls}\")\n\nasyncio.run(main(debug=(sys.argv[1:] == [\"1\"])))\n```\n\nOutput on current main:\n\n```\n$ ./python repro.py\ndebug=False  exception_handler calls=\nResourceWarning: unclosed \u003csocket.socket fd=8, ..., laddr=('127.0.0.1', \u003cserver port>), ...>\n\n$ ./python repro.py 1\ndebug=True  exception_handler calls=1ation for incoming connection']\n```\n\nThe leaked socket's `laddr` is the server port, it is the accepted server-side socket.\n\n## Suggested fix\n\nMirror gh-153134 on the server side. When the transport was never created, close `conn` and report the exception unconditionally. Reporting should stay limited to that case because the same `except` block also sees `await waiter` failures such as SSL handshake errors from port scanners, and reporting those unconditionally would be noisy.\n\nI'd like to work on this as part of the PyCon KR sprint. @hugovk @corona10 \n\nFound while verifying entries from [devdanzin's stdlib audit catalog] n.22 (https://gist.github.com/devdanzin/3198710e3c0128fda5e0a7b4e0768e5f).\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-155936\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/danielKim614","@type":"Person","name":"danielKim614"},"datePublished":"2026-08-17T06:01:53Z","url":"https://github.com/python/cpython/issues/155934"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:25be892a-6da0-5092-925e-97bac25a7206
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD2B2:A99C2:4824528:5B75545:6B022F64
html-safe-nonce92773515a313399fd6b1e8b57be7ce37ad0285353e378f08e645cda372bd2bb3
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMkIyOkE5OUMyOjQ4MjQ1Mjg6NUI3NTU0NTo2QjAyMkY2NCIsInZpc2l0b3JfaWQiOiI4OTM2ODk2NDMxNTE2NjkyMzI0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmace4cd2de92d0f4edd8ac63aa6d6a49b2316117055356f04315cdc7ea81c02d82e
hovercard-subject-tagissue:5168791868
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/155934/issue_layout
twitter:imagehttps://opengraph.githubassets.com/4e12a281180f8cf0b747b59f6936a89ad8f6c03b03d4aa5dce1cdbb85085aa73/python/cpython/issues/155934
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/4e12a281180f8cf0b747b59f6936a89ad8f6c03b03d4aa5dce1cdbb85085aa73/python/cpython/issues/155934
og:image:altBug report Bug description When an incoming connection has been accepted and protocol_factory() (or transport creation) then raises inside BaseSelectorEventLoop._accept_connection2() (Lib/asyncio/s...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamedanielKim614
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
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
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetcanary-1
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/155934#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F155934
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
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
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://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
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F155934
Sign uphttps://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/155934
Reloadhttps://github.com/python/cpython/issues/155934
Reloadhttps://github.com/python/cpython/issues/155934
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/155934
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/155934
asyncio: socket leak and silent error when transport creation fails for an accepted connectionhttps://github.com/python/cpython/issues/155934#top
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/danielKim614
danielKim614https://github.com/danielKim614
on Aug 17, 2026https://github.com/python/cpython/issues/155934#issue-5168791868
gh-153133https://github.com/python/cpython/issues/153133
gh-153134https://github.com/python/cpython/pull/153134
gh-153134https://github.com/python/cpython/pull/153134
@hugovkhttps://github.com/hugovk
@corona10https://github.com/corona10
https://gist.github.com/devdanzin/3198710e3c0128fda5e0a7b4e0768e5fhttps://gist.github.com/devdanzin/3198710e3c0128fda5e0a7b4e0768e5f
gh-155934: Fix socket leak and silent error for asyncio accepted connections #155936https://github.com/python/cpython/pull/155936
danielKim614https://github.com/danielKim614
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on Aug 17, 2026https://github.com/python/cpython/issues/155934#event-29545174450
StanFromIrelandhttps://github.com/StanFromIreland
topic-asynciohttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-asyncio%22
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
on Aug 17, 2026https://github.com/python/cpython/issues/155934#event-29553431917
github-project-automationhttps://github.com/apps/github-project-automation
asynciohttps://github.com/orgs/python/projects/29
on Aug 17, 2026https://github.com/python/cpython/issues/155934#event-29553436570
github-project-automationhttps://github.com/apps/github-project-automation
asynciohttps://github.com/orgs/python/projects/29
on Aug 17, 2026https://github.com/python/cpython/issues/155934
danielKim614https://github.com/danielKim614
on Aug 22, 2026https://github.com/python/cpython/issues/155934#event-29840954259
Merge branch 'main' into pythongh-155934-accept-errorhttps://github.com/danielKim614/cpython/commit/9ad4b31ac8874dcd4e50d224b36e6bb8fc249653
pythongh-155934https://github.com/python/cpython/issues/155934
9ad4b31https://github.com/danielKim614/cpython/commit/9ad4b31ac8874dcd4e50d224b36e6bb8fc249653
Merge branch 'main' into pythongh-155934-accept-errorhttps://github.com/danielKim614/cpython/commit/9f6dbe58d096842ad3dcd17bcebef2bbc534f9d0
pythongh-155934https://github.com/python/cpython/issues/155934
9f6dbe5https://github.com/danielKim614/cpython/commit/9f6dbe58d096842ad3dcd17bcebef2bbc534f9d0
kumaraditya303https://github.com/kumaraditya303
on Aug 23, 2026https://github.com/python/cpython/issues/155934#event-29868934216
gh-155934: Fix socket leak and silent error for asyncio accepted conn…https://github.com/python/cpython/commit/1461f0475b55154c0c4e033caae83e29b75f3e4d
gh-155934https://github.com/python/cpython/issues/155934
1461f04https://github.com/python/cpython/commit/1461f0475b55154c0c4e033caae83e29b75f3e4d
https://github.com/hugovk
hugovkhttps://github.com/hugovk
on Aug 27, 2026https://github.com/python/cpython/issues/155934#issuecomment-5438511514
hugovkhttps://github.com/hugovk
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Aug 27, 2026https://github.com/python/cpython/issues/155934#event-30101003497
github-project-automationhttps://github.com/apps/github-project-automation
asynciohttps://github.com/orgs/python/projects/29
on Aug 27, 2026https://github.com/python/cpython/issues/155934
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/155934
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/155934
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
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.