Skip to content

GH-95704: Don't suppress errors from tasks when TG is cancelled #95761

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

Merged
merged 6 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 3 additions & 4 deletions Lib/asyncio/taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ async def __aexit__(self, et, exc, tb):
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:
Expand Down
54 changes: 30 additions & 24 deletions Lib/test/test_asyncio/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,29 +229,29 @@ async def runner():

self.assertEqual(NUM, 15)

async def test_cancellation_in_body(self):
async def test_taskgroup_08(self):

async def foo():
await asyncio.sleep(0.1)
1 / 0
try:
await asyncio.sleep(10)
finally:
1 / 0

async def runner():
async with taskgroups.TaskGroup() as g:
for _ in range(5):
g.create_task(foo())

try:
await asyncio.sleep(10)
except asyncio.CancelledError:
raise
await asyncio.sleep(10)

r = asyncio.create_task(runner())
await asyncio.sleep(0.1)

self.assertFalse(r.done())
r.cancel()
with self.assertRaises(asyncio.CancelledError) as cm:
with self.assertRaises(ExceptionGroup) as cm:
await r
self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})

async def test_taskgroup_09(self):

Expand Down Expand Up @@ -315,33 +315,37 @@ async def runner():
async def test_taskgroup_11(self):

async def foo():
await asyncio.sleep(0.1)
1 / 0
try:
await asyncio.sleep(10)
finally:
1 / 0

async def runner():
async with taskgroups.TaskGroup():
async with taskgroups.TaskGroup() as g2:
for _ in range(5):
g2.create_task(foo())

try:
await asyncio.sleep(10)
except asyncio.CancelledError:
raise
await asyncio.sleep(10)

r = asyncio.create_task(runner())
await asyncio.sleep(0.1)

self.assertFalse(r.done())
r.cancel()
with self.assertRaises(asyncio.CancelledError):
with self.assertRaises(ExceptionGroup) as cm:
await r

self.assertEqual(get_error_types(cm.exception), {ExceptionGroup})
self.assertEqual(get_error_types(cm.exception.exceptions[0]), {ZeroDivisionError})

async def test_taskgroup_12(self):

async def foo():
await asyncio.sleep(0.1)
1 / 0
try:
await asyncio.sleep(10)
finally:
1 / 0

async def runner():
async with taskgroups.TaskGroup() as g1:
Expand All @@ -351,19 +355,19 @@ async def runner():
for _ in range(5):
g2.create_task(foo())

try:
await asyncio.sleep(10)
except asyncio.CancelledError:
raise
await asyncio.sleep(10)

r = asyncio.create_task(runner())
await asyncio.sleep(0.1)

self.assertFalse(r.done())
r.cancel()
with self.assertRaises(asyncio.CancelledError):
with self.assertRaises(ExceptionGroup) as cm:
await r

self.assertEqual(get_error_types(cm.exception), {ExceptionGroup})
self.assertEqual(get_error_types(cm.exception.exceptions[0]), {ZeroDivisionError})

async def test_taskgroup_13(self):

async def crash_after(t):
Expand Down Expand Up @@ -423,8 +427,9 @@ async def runner():

self.assertFalse(r.done())
r.cancel()
with self.assertRaises(asyncio.CancelledError):
with self.assertRaises(ExceptionGroup) as cm:
await r
self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})

async def test_taskgroup_16(self):

Expand All @@ -450,8 +455,9 @@ async def runner():

self.assertFalse(r.done())
r.cancel()
with self.assertRaises(asyncio.CancelledError):
with self.assertRaises(ExceptionGroup) as cm:
await r
self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})

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