Skip to content

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

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

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.

Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We first need a discussion before a PR. This is premature. Please read the devguide to understand our process.

This also needs:

  • A doc change
  • What's New.
  • NEWS entries.

For now, I'm going to close it. Wait on the issue before re-opening the PR please.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 24, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@picnixz picnixz closed this May 24, 2026
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.

2 participants