René's URL Explorer Experiment


Title: asyncio: cancelling a task awaiting subprocess `stdin.wait_closed()` cancels the shared close future, then `pipe_connection_lost()` raises `InvalidStateError` · Issue #162217 · python/cpython · GitHub

Open Graph Title: asyncio: cancelling a task awaiting subprocess `stdin.wait_closed()` cancels the shared close future, then `pipe_connection_lost()` raises `InvalidStateError` · Issue #162217 · python/cpython

X Title: asyncio: cancelling a task awaiting subprocess `stdin.wait_closed()` cancels the shared close future, then `pipe_connection_lost()` raises `InvalidStateError` · Issue #162217 · python/cpython

Description: Bug report Bug description For a subprocess created with asyncio.create_subprocess_exec(..., stdin=PIPE), proc.stdin.wait_closed() awaits the protocol's shared _stdin_closed future directly. SubprocessStreamProtocol._get_close_waiter() r...

Open Graph Description: Bug report Bug description For a subprocess created with asyncio.create_subprocess_exec(..., stdin=PIPE), proc.stdin.wait_closed() awaits the protocol's shared _stdin_closed future directly. Subpro...

X Description: Bug report Bug description For a subprocess created with asyncio.create_subprocess_exec(..., stdin=PIPE), proc.stdin.wait_closed() awaits the protocol's shared _stdin_closed future directly. Su...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"asyncio: cancelling a task awaiting subprocess `stdin.wait_closed()` cancels the shared close future, then `pipe_connection_lost()` raises `InvalidStateError`","articleBody":"# Bug report\n\n### Bug description\n\nFor a subprocess created with `asyncio.create_subprocess_exec(..., stdin=PIPE)`, `proc.stdin.wait_closed()` awaits the protocol's shared `_stdin_closed` future directly. `SubprocessStreamProtocol._get_close_waiter()` returns it, and `StreamWriter.wait_closed()` awaits it without `shield()`. If the task awaiting `wait_closed()` is cancelled after `proc.stdin.close()` but before the pipe's `connection_lost` callback runs, the cancellation cancels that shared future.\n\nWhen `SubprocessStreamProtocol.pipe_connection_lost(0, None)` runs later, it calls `self._stdin_closed.set_result(None)` on the already-cancelled future. The event loop then logs:\n\n```\nException in callback SubprocessStreamProtocol.pipe_connection_lost()\n...\nasyncio.exceptions.InvalidStateError: invalid state\n```\n\n`StreamReaderProtocol` guards the equivalent future for sockets (`if not self._closed.done(): ...`); `SubprocessStreamProtocol.pipe_connection_lost()` has no such check.\n\nMinimal, deterministic reproducer:\n\n```python\nimport asyncio\nimport sys\n\n\nasync def main(mode: str) -> int:\n    loop = asyncio.get_running_loop()\n    errors = []\n    loop.set_exception_handler(lambda _loop, ctx: errors.append(ctx))\n    proc = await asyncio.create_subprocess_exec(\n        \"cat\", stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,\n        stderr=asyncio.subprocess.PIPE,\n    )\n    proc.stdin.close()\n\n    async def closer():\n        if mode == \"shield\":\n            await asyncio.shield(proc.stdin.wait_closed())\n        else:\n            await proc.stdin.wait_closed()\n\n    task = asyncio.create_task(closer())\n    await asyncio.sleep(0)  # let closer() reach the await on the close future\n    task.cancel()           # cancel between close() and pipe_connection_lost()\n    try:\n        await task\n    except asyncio.CancelledError:\n        pass\n    print(\"stdin close future:\", proc._protocol._stdin_closed)\n    await proc.communicate()\n    await proc.wait()\n    for _ in range(5):\n        await asyncio.sleep(0)\n    for ctx in errors:\n        print(\"loop exception:\", repr(ctx.get(\"exception\")), \"|\", ctx.get(\"message\"))\n    return 1 if errors else 0\n\n\nif __name__ == \"__main__\":\n    sys.exit(asyncio.run(main(sys.argv[1] if len(sys.argv) > 1 else \"plain\")))\n```\n\nOutput on 3.14.4 (Linux):\n\n```\n$ python3 repro.py\nstdin close future: \u003cFuture cancelled>\nloop exception: InvalidStateError('invalid state') | Exception in callback SubprocessStreamProtocol.pipe_connection_lost()\n\n$ python3 repro.py shield\nstdin close future: \u003cFuture finished result=None>\n```\n\nThe visible impact is an error logged from a loop callback (for example during application shutdown, when a task blocked in `wait_closed()` gets cancelled). Process I/O is otherwise unaffected. In our case it appeared intermittently in a test suite (about 5 in 2,000 runs under load) until we stopped awaiting `wait_closed()` on that path.\n\nPossible fixes, either of which avoids the error:\n- In `SubprocessStreamProtocol.pipe_connection_lost()`, only call `set_result()` / `set_exception()` on `_stdin_closed` when it is not already done, mirroring `StreamReaderProtocol`.\n- Or have `wait_closed()` (or `_get_close_waiter()`) shield the shared future, so cancelling one waiter does not cancel it for everyone.\n\nPossibly related older report: bpo-23140.\n\n### CPython versions tested on\n\n3.14\n\n### Operating systems tested on\n\nLinux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-162284\n* gh-162317\n* gh-162320\n* gh-162321\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/AugustusHsu","@type":"Person","name":"AugustusHsu"},"datePublished":"2026-11-18T15:46:30Z","url":"https://github.com/python/cpython/issues/162217"}

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:3ab31b7a-d2d1-7065-505c-609e4b8ae8cd
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD5CC:1CCECA:1425186:1ACD523:6B02038B
html-safe-nonce4c33615e1bb8d437887428e0de6abe984236f64b0c49f190bba0e815bcb7396f
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENUNDOjFDQ0VDQToxNDI1MTg2OjFBQ0Q1MjM6NkIwMjAzOEIiLCJ2aXNpdG9yX2lkIjoiMzY2Mjg4MTY4MTAxMjAzMjM5NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacc1a3e8c3415cd24de1a65c104e8b29fa0423f2ce6aa6ac6ec5e085861665d010
hovercard-subject-tagissue:6470947088
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/162217/issue_layout
twitter:imagehttps://opengraph.githubassets.com/6de1b71849e5f6bdf960109dc99ef821573082d6508186277ed9999f57f85ae4/python/cpython/issues/162217
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/6de1b71849e5f6bdf960109dc99ef821573082d6508186277ed9999f57f85ae4/python/cpython/issues/162217
og:image:altBug report Bug description For a subprocess created with asyncio.create_subprocess_exec(..., stdin=PIPE), proc.stdin.wait_closed() awaits the protocol's shared _stdin_closed future directly. Subpro...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameAugustusHsu
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-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/162217#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F162217
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%2F162217
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/162217
Reloadhttps://github.com/python/cpython/issues/162217
Reloadhttps://github.com/python/cpython/issues/162217
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/162217
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/162217
asyncio: cancelling a task awaiting subprocess stdin.wait_closed() cancels the shared close future, then pipe_connection_lost() raises InvalidStateErrorhttps://github.com/python/cpython/issues/162217#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/AugustusHsu
AugustusHsuhttps://github.com/AugustusHsu
on Nov 18, 2026https://github.com/python/cpython/issues/162217#issue-6470947088
bpo-23140https://bugs.python.org/issue?@action=redirect&bpo=23140
gh-162217: Shield shared stdin close future to avoid InvalidStateError #162284https://github.com/python/cpython/pull/162284
gh-162217: Fix InvalidStateError when subprocess stdin is closed after a cancelled wait_closed() #162317https://github.com/python/cpython/pull/162317
[3.15] gh-162217: Fix InvalidStateError when subprocess stdin is closed after a cancelled wait_closed() (GH-162317) #162320https://github.com/python/cpython/pull/162320
[3.14] gh-162217: Fix InvalidStateError when subprocess stdin is closed after a cancelled wait_closed() (GH-162317) #162321https://github.com/python/cpython/pull/162321
https://github.com/deadlovelll
deadlovelllhttps://github.com/deadlovelll
on Nov 18, 2026https://github.com/python/cpython/issues/162217#issuecomment-6984328101
NekoAsakurahttps://github.com/NekoAsakura
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
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 Nov 18, 2026https://github.com/python/cpython/issues/162217#event-36566382249
github-project-automationhttps://github.com/apps/github-project-automation
asynciohttps://github.com/orgs/python/projects/29
on Nov 18, 2026https://github.com/python/cpython/issues/162217#event-36566385758
github-project-automationhttps://github.com/apps/github-project-automation
asynciohttps://github.com/orgs/python/projects/29
on Nov 18, 2026https://github.com/python/cpython/issues/162217
mathisbusanabussei359-webhttps://github.com/mathisbusanabussei359-web
on Nov 20, 2026https://github.com/python/cpython/issues/162217#event-36742443440
Merge branch 'main' into pythongh-162217-fix-stdin-wait-closedhttps://github.com/mathisbusanabussei359-web/cpython/commit/0df293a9882947692828780849f82336d1194aa9
pythongh-162217https://github.com/python/cpython/issues/162217
0df293ahttps://github.com/mathisbusanabussei359-web/cpython/commit/0df293a9882947692828780849f82336d1194aa9
kumaraditya303https://github.com/kumaraditya303
on Nov 20, 2026https://github.com/python/cpython/issues/162217#event-36768311962
gh-162217: Fix InvalidStateError when subprocess stdin is closed afte…https://github.com/python/cpython/commit/a3ea5d29f5b0285f7ac1b0053f15b30f2c8daec9
gh-162217https://github.com/python/cpython/issues/162217
a3ea5d2https://github.com/python/cpython/commit/a3ea5d29f5b0285f7ac1b0053f15b30f2c8daec9
kumaraditya303https://github.com/kumaraditya303
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Nov 20, 2026https://github.com/python/cpython/issues/162217#event-36768603109
github-project-automationhttps://github.com/apps/github-project-automation
asynciohttps://github.com/orgs/python/projects/29
on Nov 20, 2026https://github.com/python/cpython/issues/162217
kumaraditya303https://github.com/kumaraditya303
on Nov 20, 2026https://github.com/python/cpython/issues/162217#event-36772926213
[3.14] gh-162217: Fix InvalidStateError when subprocess stdin is clos…https://github.com/python/cpython/commit/8607e1d42101be553bbe0f81dda2368325d8ac85
gh-162217https://github.com/python/cpython/issues/162217
8607e1dhttps://github.com/python/cpython/commit/8607e1d42101be553bbe0f81dda2368325d8ac85
[3.15] gh-162217: Fix InvalidStateError when subprocess stdin is clos…https://github.com/python/cpython/commit/0ff53c82c7e89bd37d9b8737c970d310eb44eea1
gh-162217https://github.com/python/cpython/issues/162217
0ff53c8https://github.com/python/cpython/commit/0ff53c82c7e89bd37d9b8737c970d310eb44eea1
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/162217
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/162217
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.