Skip to content

Commit 36c114a

Browse files
GH-95704: Don't suppress errors from tasks when TG is cancelled (GH-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 <[email protected]> (cherry picked from commit f51f54f) Co-authored-by: Guido van Rossum <[email protected]>
1 parent 2bb363c commit 36c114a

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

Lib/asyncio/taskgroups.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,9 @@ async def __aexit__(self, et, exc, tb):
116116
if self._base_error is not None:
117117
raise self._base_error
118118

119-
if propagate_cancellation_error is not None:
120-
# The wrapping task was cancelled; since we're done with
121-
# closing all child tasks, just propagate the cancellation
122-
# request now.
119+
# Propagate CancelledError if there is one, except if there
120+
# are other errors -- those have priority.
121+
if propagate_cancellation_error and not self._errors:
123122
raise propagate_cancellation_error
124123

125124
if et is not None and et is not exceptions.CancelledError:

Lib/test/test_asyncio/test_taskgroups.py

+30-24
Original file line numberDiff line numberDiff line change
@@ -230,29 +230,29 @@ async def runner():
230230

231231
self.assertEqual(NUM, 15)
232232

233-
async def test_cancellation_in_body(self):
233+
async def test_taskgroup_08(self):
234234

235235
async def foo():
236-
await asyncio.sleep(0.1)
237-
1 / 0
236+
try:
237+
await asyncio.sleep(10)
238+
finally:
239+
1 / 0
238240

239241
async def runner():
240242
async with taskgroups.TaskGroup() as g:
241243
for _ in range(5):
242244
g.create_task(foo())
243245

244-
try:
245-
await asyncio.sleep(10)
246-
except asyncio.CancelledError:
247-
raise
246+
await asyncio.sleep(10)
248247

249248
r = asyncio.create_task(runner())
250249
await asyncio.sleep(0.1)
251250

252251
self.assertFalse(r.done())
253252
r.cancel()
254-
with self.assertRaises(asyncio.CancelledError) as cm:
253+
with self.assertRaises(ExceptionGroup) as cm:
255254
await r
255+
self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
256256

257257
async def test_taskgroup_09(self):
258258

@@ -316,33 +316,37 @@ async def runner():
316316
async def test_taskgroup_11(self):
317317

318318
async def foo():
319-
await asyncio.sleep(0.1)
320-
1 / 0
319+
try:
320+
await asyncio.sleep(10)
321+
finally:
322+
1 / 0
321323

322324
async def runner():
323325
async with taskgroups.TaskGroup():
324326
async with taskgroups.TaskGroup() as g2:
325327
for _ in range(5):
326328
g2.create_task(foo())
327329

328-
try:
329-
await asyncio.sleep(10)
330-
except asyncio.CancelledError:
331-
raise
330+
await asyncio.sleep(10)
332331

333332
r = asyncio.create_task(runner())
334333
await asyncio.sleep(0.1)
335334

336335
self.assertFalse(r.done())
337336
r.cancel()
338-
with self.assertRaises(asyncio.CancelledError):
337+
with self.assertRaises(ExceptionGroup) as cm:
339338
await r
340339

340+
self.assertEqual(get_error_types(cm.exception), {ExceptionGroup})
341+
self.assertEqual(get_error_types(cm.exception.exceptions[0]), {ZeroDivisionError})
342+
341343
async def test_taskgroup_12(self):
342344

343345
async def foo():
344-
await asyncio.sleep(0.1)
345-
1 / 0
346+
try:
347+
await asyncio.sleep(10)
348+
finally:
349+
1 / 0
346350

347351
async def runner():
348352
async with taskgroups.TaskGroup() as g1:
@@ -352,19 +356,19 @@ async def runner():
352356
for _ in range(5):
353357
g2.create_task(foo())
354358

355-
try:
356-
await asyncio.sleep(10)
357-
except asyncio.CancelledError:
358-
raise
359+
await asyncio.sleep(10)
359360

360361
r = asyncio.create_task(runner())
361362
await asyncio.sleep(0.1)
362363

363364
self.assertFalse(r.done())
364365
r.cancel()
365-
with self.assertRaises(asyncio.CancelledError):
366+
with self.assertRaises(ExceptionGroup) as cm:
366367
await r
367368

369+
self.assertEqual(get_error_types(cm.exception), {ExceptionGroup})
370+
self.assertEqual(get_error_types(cm.exception.exceptions[0]), {ZeroDivisionError})
371+
368372
async def test_taskgroup_13(self):
369373

370374
async def crash_after(t):
@@ -424,8 +428,9 @@ async def runner():
424428

425429
self.assertFalse(r.done())
426430
r.cancel()
427-
with self.assertRaises(asyncio.CancelledError):
431+
with self.assertRaises(ExceptionGroup) as cm:
428432
await r
433+
self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
429434

430435
async def test_taskgroup_16(self):
431436

@@ -451,8 +456,9 @@ async def runner():
451456

452457
self.assertFalse(r.done())
453458
r.cancel()
454-
with self.assertRaises(asyncio.CancelledError):
459+
with self.assertRaises(ExceptionGroup) as cm:
455460
await r
461+
self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
456462

457463
async def test_taskgroup_17(self):
458464
NUM = 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When a task catches :exc:`asyncio.CancelledError` and raises some other error,
2+
the other error should generally not silently be suppressed.

0 commit comments

Comments
 (0)