Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-101581 Optionally prevent child tasks from being cancelled in asyncio taskgroups #101648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
Loading
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add defer_errors flag to asyncio.TaskGroup
`asyncio.TaskGroup` cancels all child tasks when one raises. Setting `defer_errors=True` will prevent this
  • Loading branch information
DontPanicO committed Feb 7, 2023
commit 2df96d0247cbf512c24e4d463369398e602e689e
13 changes: 8 additions & 5 deletions 13 Lib/asyncio/taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class TaskGroup:

def __init__(self):
def __init__(self, defer_errors=False):
self._entered = False
self._exiting = False
self._aborting = False
Expand All @@ -22,6 +22,7 @@ def __init__(self):
self._errors = []
self._base_error = None
self._on_completed_fut = None
self._defer_errors = defer_errors

def __repr__(self):
info = ['']
Expand Down Expand Up @@ -180,7 +181,8 @@ def _on_task_done(self, task):
return

self._errors.append(exc)
if self._is_base_error(exc) and self._base_error is None:
is_base_error = self._is_base_error(exc)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @DontPanicO, I'm trying to understand why is_base_error is needed. Please help me understand why that is preferable to self._is_base_error(exc). Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

Hi @willingc . I stored self._is_base_error(exc) result in a variable since it's used twice: once on line 205 and once on line 238

if is_base_error and self._base_error is None:
self._base_error = exc

if self._parent_task.done():
Expand Down Expand Up @@ -213,6 +215,7 @@ def _on_task_done(self, task):
# pass
# await something_else # this line has to be called
# # after TaskGroup is finished.
self._abort()
self._parent_cancel_requested = True
self._parent_task.cancel()
if not self._defer_errors or is_base_error:
self._abort()
self._parent_cancel_requested = True
self._parent_task.cancel()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.