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>
This commit is contained in:
Guido van Rossum 2022-08-16 18:23:06 -07:00 committed by GitHub
parent 9b30b965f0
commit f51f54f39d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 28 deletions

View file

@ -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: