Skip to content

Commit 1c641ff

Browse files
authored
gh-157157: Make test_taskgroup_cancel_keeps_outer_cancellation deterministic (#157158)
1 parent 59cba59 commit 1c641ff

1 file changed

Lines changed: 9 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])

0 commit comments

Comments
 (0)