Skip to content

Commit 86ee308

Browse files
committed
gh-157157: Make test_taskgroup_cancel_keeps_outer_cancellation deterministic
The test cancelled the parent task from outside after a 10 ms sleep, expecting the group to still be waiting for its child. If the event loop stalled for longer than that before the body task first ran, the outer cancel() landed while the parent's awaited future was already cancelled by the group, and its message was dropped, failing the test on slow builds such as the TSan CI job. Synchronize on events instead: the child signals from its finally block that the group is cancelling and waits to be released, and only then is the parent task cancelled from outside.
1 parent 0b4c7da commit 86ee308

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

Lib/test/test_asyncio/test_taskgroups.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,11 +1200,17 @@ async def child(tg):
12001200

12011201
async def test_taskgroup_cancel_keeps_outer_cancellation(self):
12021202
# gh-155433: any cancellation from outside the group must propagate.
1203+
cancelling = asyncio.Event()
1204+
release = asyncio.Event()
1205+
12031206
async def child():
12041207
try:
12051208
await asyncio.sleep(10)
12061209
finally:
1207-
await asyncio.sleep(0.1)
1210+
# The group is cancelling: it has cancelled its parent task
1211+
# and is waiting for this task to finish.
1212+
cancelling.set()
1213+
await release.wait()
12081214

12091215
async def body():
12101216
async with asyncio.TaskGroup() as tg:
@@ -1213,8 +1219,9 @@ async def body():
12131219
tg.cancel()
12141220

12151221
task = asyncio.create_task(body())
1216-
await asyncio.sleep(0.01)
1222+
await cancelling.wait()
12171223
task.cancel('message')
1224+
release.set()
12181225
with self.assertRaises(asyncio.CancelledError) as cm:
12191226
await task
12201227
self.assertEqual('message', cm.exception.args[0])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a timing dependence in ``test_taskgroup_cancel_keeps_outer_cancellation``
2+
in ``test_asyncio``, which failed on slow builds.

0 commit comments

Comments
 (0)