Skip to content

Commit 25bdf39

Browse files
deadlovelllmiss-islington
authored andcommitted
gh-158239: Fix asyncio.gather() performance regression (GH-158240)
(cherry picked from commit b06dd22) Co-authored-by: Timofei Ivankov <128279579+deadlovelll@users.noreply.github.com>
1 parent 81c50e9 commit 25bdf39

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎Lib/asyncio/tasks.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ def cancel(self, msg=None):
784784
return ret
785785

786786

787-
def _discard_awaited_by(children, waiter, outer):
787+
def _discard_awaited_by(children, waiter):
788788
for fut in children:
789789
futures.future_discard_from_awaited_by(fut, waiter)
790790

@@ -850,11 +850,14 @@ def _done_callback(fut, cur_task=cur_task):
850850
# 'fut.exception()' will *raise* a CancelledError
851851
# instead of returning it.
852852
exc = fut._make_cancelled_error()
853+
# gh-157213: children outliving gather() must lose the edge
854+
_discard_awaited_by(children, cur_task)
853855
outer.set_exception(exc)
854856
return
855857
else:
856858
exc = fut.exception()
857859
if exc is not None:
860+
_discard_awaited_by(children, cur_task)
858861
outer.set_exception(exc)
859862
return
860863

@@ -922,10 +925,6 @@ def _done_callback(fut, cur_task=cur_task):
922925
children.append(fut)
923926

924927
outer = _GatheringFuture(children, loop=loop)
925-
if cur_task is not None:
926-
# gh-157213: a child outliving gather() must lose the awaited-by edge
927-
outer.add_done_callback(
928-
functools.partial(_discard_awaited_by, children, cur_task))
929928
# Run done callbacks after GatheringFuture created so any post-processing
930929
# can be performed at this point
931930
# optimization: in the special case that *all* futures finished eagerly,

‎Lib/test/test_asyncio/test_tasks.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,37 @@ async def coro():
12501250

12511251
self.loop.run_until_complete(self.new_task(self.loop, coro()))
12521252

1253+
def test_gather_discards_awaited_by_for_cancelled_sibling(self):
1254+
# gh-157213: same, when gather() is ended by a cancelled child
1255+
async def survivor():
1256+
await asyncio.Future()
1257+
1258+
async def coro():
1259+
t = self.new_task(self.loop, survivor())
1260+
victim = self.new_task(self.loop, asyncio.sleep(10))
1261+
victim.cancel()
1262+
with self.assertRaises(asyncio.CancelledError):
1263+
await asyncio.gather(t, victim)
1264+
self.assertFalse(t._asyncio_awaited_by)
1265+
t.cancel()
1266+
with self.assertRaises(asyncio.CancelledError):
1267+
await t
1268+
1269+
self.loop.run_until_complete(self.new_task(self.loop, coro()))
1270+
1271+
def test_gather_does_not_add_callback_to_outer(self):
1272+
# gh-158239: gather() must not add an internal done callback to
1273+
# the outer future just to maintain the await graph.
1274+
async def child():
1275+
await asyncio.sleep(0)
1276+
1277+
async def coro():
1278+
outer = asyncio.gather(child(), child())
1279+
self.assertFalse(outer._callbacks)
1280+
await outer
1281+
1282+
self.loop.run_until_complete(self.new_task(self.loop, coro()))
1283+
12531284
def test_wait_really_done(self):
12541285
# there is possibility that some tasks in the pending list
12551286
# became done but their callbacks haven't all been called yet

‎Misc/ACKS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ Dean Inwood
881881
Bob Ippolito
882882
Roger Irwin
883883
Atsuo Ishimoto
884+
Timofei Ivankov
884885
Alexey Izbyshev
885886
Kasia Jachim
886887
Adam Jackson

0 commit comments

Comments
 (0)