Title: Cancelling a task A awaiting another task B cancels task B · Issue #141186 · python/cpython · GitHub
Open Graph Title: Cancelling a task A awaiting another task B cancels task B · Issue #141186 · python/cpython
X Title: Cancelling a task A awaiting another task B cancels task B · Issue #141186 · python/cpython
Description: Documentation This is the issue described in #138717, which was closed as "not planned" because (I think) no acceptable resolution was proposed. I think that issue might not have been raised if it was clearly documented that it would hap...
Open Graph Description: Documentation This is the issue described in #138717, which was closed as "not planned" because (I think) no acceptable resolution was proposed. I think that issue might not have been raised if it ...
X Description: Documentation This is the issue described in #138717, which was closed as "not planned" because (I think) no acceptable resolution was proposed. I think that issue might not have been rai...
Opengraph URL: https://github.com/python/cpython/issues/141186
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cancelling a task A awaiting another task B cancels task B","articleBody":"# Documentation\n\nThis is the issue described in [#138717](https://github.com/python/cpython/issues/138717), which was closed as \"not planned\" because (I think) no acceptable resolution was proposed.\n\nI think that issue might not have been raised if it was clearly documented that it would happen.\n\nIt also had to be explicitly explained in [this comment](https://github.com/python/cpython/issues/108042#issuecomment-1681279789):\n\n\u003e Cancelling any future propagates the error down the chain of await, cancelling each future\n\nThis code is a trivial example:\n\n```python\nasync def test_sub_cancellation():\n \"\"\"Does cancelling a coro waiting on a Task also cancel that Task?\"\"\"\n async def coro1():\n await asyncio.sleep(2)\n task1 = asyncio.create_task(coro1())\n async def coro2():\n await task1\n with pytest.raises(asyncio.TimeoutError):\n await asyncio.wait_for(coro2(), 1)\n assert task1.cancelled() # implicitly when coro2 was cancelled?\n```\n\nThe [docs](https://docs.python.org/3/library/asyncio-task.html#running-in-threads) say (way, way down) that:\n\n\u003e If a coroutine is awaiting on a Future object during cancellation, the Future object will be cancelled.\n\nThis does actually apply because Tasks are a subset of Futures, but that doesn't seem to be documented (so I didn't know until I looked at the code for Task). I am using this behaviour, not objecting to it, but I'm worried that being undocumented it might change without warning.\n\nIt might be as simple as changing that text to:\n\n\u003e If a coroutine is awaiting on a Future **or Task** object during cancellation, the **awaited** object will be cancelled.\n\nAnd/or stating explicitly that Tasks are actually Futures.\n\nAlso I think that a coroutine cannot actually be cancelled (only Tasks and Futures can be), so this language is maybe not completely clear.\n\nIt might also be worth repeating that under \"Task cancellation\" higher up, in this para:\n\n\u003e Tasks can easily and safely be cancelled. When a task is cancelled, [asyncio.CancelledError](https://docs.python.org/3/library/asyncio-exceptions.html#asyncio.CancelledError) will be raised in the task at the next opportunity.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-141247\n* gh-141249\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/qris","@type":"Person","name":"qris"},"datePublished":"2025-11-07T12:37:56.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/141186/cpython/issues/141186"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7e685b71-665d-1911-c6bf-e8ad4ead148c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C17C:322E35:677052D:90FAFB9:6A5043E5 |
| html-safe-nonce | ff24fe970dd854adeb317bcaa20e45f63b65b66afd0df0384ea4ff67861e08a6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTdDOjMyMkUzNTo2NzcwNTJEOjkwRkFGQjk6NkE1MDQzRTUiLCJ2aXNpdG9yX2lkIjoiMTczMzkxMjUxNzE4MzI5MjM4OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4f22be5b0e44c018353e0b4c7786378068dd3ab4d687862e54df1b685b264ae1 |
| hovercard-subject-tag | issue:3600160459 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/141186/issue_layout |
| twitter:image | https://opengraph.githubassets.com/375928abc0023b558590d8557dcf9fa83c71805b75732bd3736bf51df01050af/python/cpython/issues/141186 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/375928abc0023b558590d8557dcf9fa83c71805b75732bd3736bf51df01050af/python/cpython/issues/141186 |
| og:image:alt | Documentation This is the issue described in #138717, which was closed as "not planned" because (I think) no acceptable resolution was proposed. I think that issue might not have been raised if it ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | qris |
| hostname | github.com |
| expected-hostname | github.com |
| None | d6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d90666eaf032b7eb8975a378efe38e37f4ebb96c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width