Skip to content

Commit 16caf71

Browse files
hugovkdeadlovelll
andauthored
[3.15] gh-161124: Fix asyncio call graph reporting event loop frames for excessive depth (GH-161126) (#162036)
(cherry picked from commit ed27405) Co-authored-by: Timofei Ivankov <128279579+deadlovelll@users.noreply.github.com>
1 parent 5771e35 commit 16caf71

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

@@ -152,7 +153,8 @@ def capture_call_graph(
152153

153154
call_stack: list[FrameCallGraphEntry] = []
154155

155-
f = sys._getframe(depth) if limit != 0 else None
156+
# gh-161124: find the task boundary before skipping frames
157+
f = sys._getframe(0) if limit != 0 else None
156158
try:
157159
while f is not None:
158160
# gh-156988: sync gen should not clear the call chain
@@ -170,6 +172,8 @@ def capture_call_graph(
170172
finally:
171173
del f
172174

175+
call_stack = call_stack[max(0, operator.index(depth)):]
176+
173177
awaited_by = []
174178
if future._asyncio_awaited_by:
175179
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
@@ -457,6 +457,11 @@ async def main():
457457

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

460+
async def test_call_graph_depth_beyond_task(self):
461+
# gh-161124
462+
graph = asyncio.capture_call_graph(depth=2)
463+
self.assertEqual(graph.call_stack, ())
464+
460465
async def test_call_graph_finished_task(self):
461466
# gh-156408: the call graph must not record a finished coroutine's None frame
462467
async def boom():
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)