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
3 changes: 2 additions & 1 deletion Lib/asyncio/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def _build_graph_for_future(
while coro is not None:
if hasattr(coro, 'cr_await'):
# A native coroutine or duck-type compatible iterator
st.append(FrameCallGraphEntry(coro.cr_frame))
if coro.cr_frame is not None:
st.append(FrameCallGraphEntry(coro.cr_frame))
coro = coro.cr_await
elif hasattr(coro, 'ag_await'):
# A native async generator or duck-type compatible iterator
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_asyncio/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,24 @@ def test_capture_call_graph_non_future(self):
with self.assertRaises(TypeError):
asyncio.capture_call_graph("not a future")

async def test_call_graph_finished_task(self):
# gh-156408: the call graph must not record a finished coroutine's None frame
async def boom():
raise ValueError

done = asyncio.create_task(asyncio.sleep(0), name='done')
failed = asyncio.create_task(boom(), name='failed')
cancelled = asyncio.create_task(asyncio.Event().wait(), name='cancelled')
cancelled.cancel()
await asyncio.gather(done, failed, cancelled, return_exceptions=True)

for task in (done, failed, cancelled):
with self.subTest(task=task.get_name()):
buf = io.StringIO()
asyncio.print_call_graph(task, file=buf)
self.assertEqual(asyncio.capture_call_graph(task).call_stack, ())
self.assertIn(f"name={task.get_name()!r}", buf.getvalue())

async def test_capture_call_graph_no_current_task(self):
results = []

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`asyncio.print_call_graph` raising :exc:`AttributeError` when
called on a task that has already finished.
Loading