Title: test_taskgroup_cancel_keeps_outer_cancellation is timing-dependent and fails on slow (TSan) CI · Issue #157157 · python/cpython · GitHub
Open Graph Title: test_taskgroup_cancel_keeps_outer_cancellation is timing-dependent and fails on slow (TSan) CI · Issue #157157 · python/cpython
X Title: test_taskgroup_cancel_keeps_outer_cancellation is timing-dependent and fails on slow (TSan) CI · Issue #157157 · python/cpython
Description: Bug report Bug description: test_taskgroup_cancel_keeps_outer_cancellation (added by gh-155433 in ee23046) failed on the "Sanitizers / TSan (free-threading)" CI job of an unrelated PR: https://github.com/python/cpython/actions/runs/34184...
Open Graph Description: Bug report Bug description: test_taskgroup_cancel_keeps_outer_cancellation (added by gh-155433 in ee23046) failed on the "Sanitizers / TSan (free-threading)" CI job of an unrelated PR: https://gith...
X Description: Bug report Bug description: test_taskgroup_cancel_keeps_outer_cancellation (added by gh-155433 in ee23046) failed on the "Sanitizers / TSan (free-threading)" CI job of an unrelated PR: ht...
Opengraph URL: https://github.com/python/cpython/issues/157157
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"test_taskgroup_cancel_keeps_outer_cancellation is timing-dependent and fails on slow (TSan) CI","articleBody":"# Bug report\n\n### Bug description:\n\n`test_taskgroup_cancel_keeps_outer_cancellation` (added by gh-155433 in ee23046d64) failed on the \"Sanitizers / TSan (free-threading)\" CI job of an unrelated PR: https://github.com/python/cpython/actions/runs/34184752621/job/101930777575\n\n```\nERROR: test_taskgroup_cancel_keeps_outer_cancellation (test.test_asyncio.test_taskgroups.TestTaskGroup.test_taskgroup_cancel_keeps_outer_cancellation)\nTraceback (most recent call last):\n File \".../Lib/test/test_asyncio/test_taskgroups.py\", line 1220, in test_taskgroup_cancel_keeps_outer_cancellation\n self.assertEqual('message', cm.exception.args[0])\nIndexError: tuple index out of range\n```\n\nThe test relies on timing. It starts the body task, does `await asyncio.sleep(0.01)`, then calls `task.cancel('message')`, expecting the outer cancellation to arrive while the group is still waiting for its child (whose `finally` sleeps for 0.1 s). The `CancelledError` that comes out carries no message when the loop stalls for more than 10 ms between arming that timer and running the body's first step (for example a GC pause, or the general slowness of a sanitizer build: the same job log reports single callbacks taking more than 0.1 s).\n\nWhat happens in that ordering:\n\n1. The timer expires before the iteration that runs the body's first step, so the timer callback is queued right behind it and the outer coroutine resumes in the next iteration, ahead of the group's own wakeup.\n2. Meanwhile the body has called `tg.cancel()`, which cancels the parent task; when `TaskGroup._aexit()` awaits `_on_completed_fut`, `Task.__step()` cancels that future immediately (no message) and schedules the wakeup.\n3. The outer `task.cancel('message')` now runs while `_fut_waiter` is that already-cancelled future, so `Task.cancel()` cannot cancel it and falls back to `_must_cancel = True` with the message.\n4. The wakeup then calls `__step(exc)` with the message-less `CancelledError` from the future, and since `exc` is already a `CancelledError`, `_must_cancel` is cleared without using `_cancel_message`. The group re-raises that message-less error at the end of `_aexit()`.\n\nReproducer, simulating the stall with `time.sleep()` after the timer is armed (the eager-task variant is not affected, because there the body's first step has already run when the timer is armed):\n\n```python\nimport asyncio, time\n\nasync def main():\n loop = asyncio.get_running_loop()\n\n async def child():\n try:\n await asyncio.sleep(10)\n finally:\n await asyncio.sleep(0.1)\n\n async def body():\n async with asyncio.TaskGroup() as tg:\n tg.create_task(child())\n await asyncio.sleep(0)\n tg.cancel()\n\n task = asyncio.create_task(body())\n fut = loop.create_future()\n loop.call_later(0.01, fut.set_result, None) # what asyncio.sleep(0.01) does\n time.sleep(0.02) # the loop stalls\n await fut\n task.cancel('message')\n try:\n await task\n except asyncio.CancelledError as exc:\n print(exc.args) # () instead of ('message',)\n\nasyncio.run(main())\n```\n\nThe test can be made deterministic by synchronizing on events instead of sleeping: have the child signal from its `finally` block that the group is cancelling and wait there for the test to release it, and only then cancel the parent task from outside. I have a PR for that.\n\nWhether `Task.cancel(msg)` should keep the message when it is called between the cancellation of the awaited future and the task's next step (step 3 and 4 above) is a separate question; I have not changed that.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux (CI), macOS (reproducer)\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-157158\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/iamsharduld","@type":"Person","name":"iamsharduld"},"datePublished":"2026-09-08T04:16:33Z","url":"https://github.com/python/cpython/issues/157157"}
| react-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ebe2a1a9-8120-c9f7-4e73-28de3dc823b0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AEC0:842EC:32F45B4:43EF18D:6B01F9F1 |
| html-safe-nonce | 0f646c34111f27870b0083bea2219e935b6932ca70d562c4fd9cc70b810fcb9d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRUMwOjg0MkVDOjMyRjQ1QjQ6NDNFRjE4RDo2QjAxRjlGMSIsInZpc2l0b3JfaWQiOiI0ODI1NjYxNjEzMDMxOTQ2NzM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | faf9a1f186910bb1ae77c16502c52ae2098dad0018a3801cde5b93ec859d7ee2 |
| hovercard-subject-tag | issue:5381954627 |
| 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/157157/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fa257bb4ed090f00c91bf36d057d6c1bc53581d3bfff88fd871ffdc9d7eaa186/python/cpython/issues/157157 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fa257bb4ed090f00c91bf36d057d6c1bc53581d3bfff88fd871ffdc9d7eaa186/python/cpython/issues/157157 |
| og:image:alt | Bug report Bug description: test_taskgroup_cancel_keeps_outer_cancellation (added by gh-155433 in ee23046) failed on the "Sanitizers / TSan (free-threading)" CI job of an unrelated PR: https://gith... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iamsharduld |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width