gh-156980: Fix asyncio call graph truncation at async generators by deadlovelll · Pull Request #156984 · python/cpython · GitHub
Skip to content
Open
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
8 changes: 8 additions & 0 deletions Doc/library/stdtypes.rst
3 changes: 3 additions & 0 deletions Lib/asyncio/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def _build_graph_for_future(
# A native async generator or duck-type compatible iterator
st.append(FrameCallGraphEntry(coro.ag_frame))
coro = coro.ag_await
elif hasattr(coro, 'ag_gen'):
# gh-156980: An asend()/athrow() awaitable: step over it to its generator
coro = coro.ag_gen
else:
break

Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,23 @@ async def gen():
r"cannot reuse already awaited __anext__\(\)/asend\(\)"):
an.send(None)

def test_async_gen_asend_athrow_ag_gen(self):
# gh-156980: asend()/athrow() awaitables expose the generator they drive
async def gen():
yield 1

g = gen()
asend = g.asend(None)
athrow = g.athrow(ValueError)
try:
self.assertIs(asend.ag_gen, g)
self.assertIs(athrow.ag_gen, g)
with self.assertRaises(AttributeError):
asend.ag_gen = None
finally:
asend.close()
athrow.close()

def test_async_gen_asend_throw_concurrent_with_send(self):
import types

Expand Down
71 changes: 71 additions & 0 deletions Lib/test/test_asyncio/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,77 @@ class FakeCoro:

self.assertEqual(len(result.call_stack), 2)

async def test_stack_async_gen_asend(self):
# gh-156980: an async_generator_asend must not truncate the stack
fut = asyncio.Future()
stack_for_consumer = None

async def deep():
await fut

async def gen():
await deep()
yield 1

async def consumer():
async for _ in gen():
pass

async def main():
nonlocal stack_for_consumer

async with asyncio.TaskGroup() as g:
t = g.create_task(consumer(), name='consumer')
for _ in range(5):
await asyncio.sleep(0)

stack_for_consumer = capture_test_stack(fut=t)
fut.set_result(None)

await main()

self.assertEqual(stack_for_consumer[0][:2], [
'T<consumer>',
['a deep', 'ag gen', 'a consumer'],
])

async def test_stack_async_gen_aclose(self):
# gh-156980: an async_generator_athrow must not truncate the stack
fut = asyncio.Future()
stack_for_consumer = None

async def deep():
await fut

async def gen():
try:
yield 1
finally:
await deep()

async def consumer():
agen = gen()
await anext(agen)
await agen.aclose()

async def main():
nonlocal stack_for_consumer

async with asyncio.TaskGroup() as g:
t = g.create_task(consumer(), name='consumer')
for _ in range(5):
await asyncio.sleep(0)

stack_for_consumer = capture_test_stack(fut=t)
fut.set_result(None)

await main()

self.assertEqual(stack_for_consumer[0][:2], [
'T<consumer>',
['a deep', 'ag gen', 'a consumer'],
])

async def test_stack_gather(self):

stack_for_deep = None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`asyncio.print_call_graph` no longer truncates the call stack of a
task suspended inside an asynchronous generator.
14 changes: 12 additions & 2 deletions Objects/genobject.c
Loading