Skip to content

gh-150355: Allow creating tasks in cancelled TaskGroup with cooperative cancellation#150357

Open
azibom wants to merge 1 commit into
python:mainfrom
azibom:fix-taskgroup-cancel-before-create-task
Open

gh-150355: Allow creating tasks in cancelled TaskGroup with cooperative cancellation#150357
azibom wants to merge 1 commit into
python:mainfrom
azibom:fix-taskgroup-cancel-before-create-task

Conversation

@azibom
Copy link
Copy Markdown
Contributor

@azibom azibom commented May 24, 2026

gh-150355: Allow create_task after tg.cancel() – cooperative cancellation

Problem:
After tg.cancel(), create_task() raises RuntimeError: TaskGroup is shutting down. The test TODO says this is not ideal – the task should run until the first await and then be cancelled.

Solution:
Add _explicitly_cancelled flag. On explicit cancel() the flag is set. In create_task(), if _aborting and _explicitly_cancelled is true, the task is created normally and then scheduled for cancellation with call_soon. This allows the synchronous code up to the first await to run. Failure‑induced abort still raises RuntimeError.

Testing:
All 118 tests in test_asyncio.test_taskgroups pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant