Skip to content

Commit ed27405

Browse files
authored
gh-161124: Fix asyncio call graph reporting event loop frames for excessive depth (#161126)
1 parent 7054b71 commit ed27405

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎Lib/asyncio/graph.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dataclasses
44
import io
5+
import operator
56
import sys
67
import types
78

@@ -160,7 +161,8 @@ def capture_call_graph(
160161

161162
call_stack: list[FrameCallGraphEntry] = []
162163

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

183+
call_stack = call_stack[max(0, operator.index(depth)):]
184+
181185
awaited_by = []
182186
if future._asyncio_awaited_by:
183187
for parent in future._asyncio_awaited_by:

‎Lib/test/test_asyncio/test_graph.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,11 @@ async def main():
785785

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

788+
async def test_call_graph_depth_beyond_task(self):
789+
# gh-161124
790+
graph = asyncio.capture_call_graph(depth=2)
791+
self.assertEqual(graph.call_stack, ())
792+
788793
async def test_capture_call_graph_positive_limit(self):
789794
captured = None
790795

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`asyncio.print_call_graph` reporting event loop frames when
2+
*depth* exceeds the task stack. Patch by Timofei Ivankov.

0 commit comments

Comments
 (0)