Skip to content
Merged
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
6 changes: 5 additions & 1 deletion Lib/asyncio/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dataclasses
import io
import operator
import sys
import types

Expand Down Expand Up @@ -160,7 +161,8 @@ def capture_call_graph(

call_stack: list[FrameCallGraphEntry] = []

f = sys._getframe(depth) if limit != 0 else None
# gh-161124: find the task boundary before skipping frames
f = sys._getframe(0) if limit != 0 else None
try:
while f is not None:
# gh-156988: sync gen should not clear the call chain
Expand All @@ -178,6 +180,8 @@ def capture_call_graph(
finally:
del f

call_stack = call_stack[max(0, operator.index(depth)):]

awaited_by = []
if future._asyncio_awaited_by:
for parent in future._asyncio_awaited_by:
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_asyncio/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,11 @@ async def main():

self.assertTrue(stack_for_fut[1].startswith('* Future(id='))

async def test_call_graph_depth_beyond_task(self):
# gh-161124
graph = asyncio.capture_call_graph(depth=2)
self.assertEqual(graph.call_stack, ())

async def test_capture_call_graph_positive_limit(self):
captured = None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`asyncio.print_call_graph` reporting event loop frames when
*depth* exceeds the task stack. Patch by Timofei Ivankov.
Loading