Skip to content

Commit a5db6a3

Browse files
gh-113848: Handle CancelledError subclasses in asyncio TaskGroup() and timeout() (GH-113850)
1 parent 5273655 commit a5db6a3

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

Lib/asyncio/taskgroups.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ async def __aexit__(self, et, exc, tb):
7373
self._base_error is None):
7474
self._base_error = exc
7575

76-
propagate_cancellation_error = \
77-
exc if et is exceptions.CancelledError else None
76+
if et is not None and issubclass(et, exceptions.CancelledError):
77+
propagate_cancellation_error = exc
78+
else:
79+
propagate_cancellation_error = None
7880
if self._parent_cancel_requested:
7981
# If this flag is set we *must* call uncancel().
8082
if self._parent_task.uncancel() == 0:
@@ -133,7 +135,7 @@ async def __aexit__(self, et, exc, tb):
133135
if propagate_cancellation_error and not self._errors:
134136
raise propagate_cancellation_error
135137

136-
if et is not None and et is not exceptions.CancelledError:
138+
if et is not None and not issubclass(et, exceptions.CancelledError):
137139
self._errors.append(exc)
138140

139141
if self._errors:

Lib/asyncio/timeouts.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ async def __aexit__(
109109
if self._state is _State.EXPIRING:
110110
self._state = _State.EXPIRED
111111

112-
if self._task.uncancel() <= self._cancelling and exc_type is exceptions.CancelledError:
113-
# Since there are no new cancel requests, we're
114-
# handling this.
115-
raise TimeoutError from exc_val
112+
if self._task.uncancel() <= self._cancelling and exc_type is not None:
113+
if issubclass(exc_type, exceptions.CancelledError):
114+
# Since there are no new cancel requests, we're
115+
# handling this.
116+
raise TimeoutError from exc_val
116117
elif self._state is _State.ENTERED:
117118
self._state = _State.EXITED
118119

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`asyncio.TaskGroup()` and :func:`asyncio.timeout()` context managers
2+
now handle :exc:`~asyncio.CancelledError` subclasses as well as exact
3+
:exc:`!CancelledError`.

0 commit comments

Comments
 (0)