summaryrefslogtreecommitdiff
path: root/Lib/asyncio/taskgroups.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2022-08-16 18:23:06 -0700
committerGitHub <noreply@github.com>2022-08-16 18:23:06 -0700
commitf51f54f39d384da63be622bcdc9cf4cfb43bad3d (patch)
treebdedc65a5f961e9aa330f7d8b929d8df0c5a4bb6 /Lib/asyncio/taskgroups.py
parent9b30b965f0c1da216397b495faef4d93ff7a5328 (diff)
downloadcpython-git-f51f54f39d384da63be622bcdc9cf4cfb43bad3d.tar.gz
GH-95704: Don't suppress errors from tasks when TG is cancelled (#95761)
When a task catches CancelledError and raises some other error, the other error should not silently be suppressed. Any scenario where a task crashes in cleanup upon cancellation will now result in an ExceptionGroup wrapping the crash(es) instead of propagating CancelledError and ignoring the side errors. NOTE: This represents a change in behavior (hence the need to change several tests). But it is only an edge case. Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Diffstat (limited to 'Lib/asyncio/taskgroups.py')
-rw-r--r--Lib/asyncio/taskgroups.py7
1 files changed, 3 insertions, 4 deletions
diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py
index 9be4838e3c..5d5e2a8a85 100644
--- a/Lib/asyncio/taskgroups.py
+++ b/Lib/asyncio/taskgroups.py
@@ -116,10 +116,9 @@ class TaskGroup:
if self._base_error is not None:
raise self._base_error
- if propagate_cancellation_error is not None:
- # The wrapping task was cancelled; since we're done with
- # closing all child tasks, just propagate the cancellation
- # request now.
+ # Propagate CancelledError if there is one, except if there
+ # are other errors -- those have priority.
+ if propagate_cancellation_error and not self._errors:
raise propagate_cancellation_error
if et is not None and et is not exceptions.CancelledError: