https://github.com/python/cpython/commit/16caf71e0dae21e9e41cc90c327060b74cd7cfe0
commit: 16caf71e0dae21e9e41cc90c327060b74cd7cfe0
branch: 3.15
author: Hugo van Kemenade <[email protected]>
committer: asvetlov <[email protected]>
date: 2026-11-15T21:31:30+01:00
summary:

[3.15] gh-161124: Fix asyncio call graph reporting event loop frames for 
excessive depth (GH-161126) (#162036)

(cherry picked from commit ed27405a6e9660baac6b6b066ee817ccb6239808)

Co-authored-by: Timofei Ivankov <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-11-01-22-07-24.gh-issue-161124.c2YmcV.rst
M Lib/asyncio/graph.py
M Lib/test/test_asyncio/test_graph.py

diff --git a/Lib/asyncio/graph.py b/Lib/asyncio/graph.py
index d0a54f3625b37e5..cd74b5b944b2b03 100644
--- a/Lib/asyncio/graph.py
+++ b/Lib/asyncio/graph.py
@@ -2,6 +2,7 @@
 
 import dataclasses
 import io
+import operator
 import sys
 import types
 
@@ -152,7 +153,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
@@ -170,6 +172,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:
diff --git a/Lib/test/test_asyncio/test_graph.py 
b/Lib/test/test_asyncio/test_graph.py
index ba02263dff0b241..9a2bca3bf04a7cd 100644
--- a/Lib/test/test_asyncio/test_graph.py
+++ b/Lib/test/test_asyncio/test_graph.py
@@ -457,6 +457,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_call_graph_finished_task(self):
         # gh-156408: the call graph must not record a finished coroutine's 
None frame
         async def boom():
diff --git 
a/Misc/NEWS.d/next/Library/2026-11-01-22-07-24.gh-issue-161124.c2YmcV.rst 
b/Misc/NEWS.d/next/Library/2026-11-01-22-07-24.gh-issue-161124.c2YmcV.rst
new file mode 100644
index 000000000000000..32cfb56adb83167
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-01-22-07-24.gh-issue-161124.c2YmcV.rst
@@ -0,0 +1,2 @@
+Fix :func:`asyncio.print_call_graph` reporting event loop frames when
+*depth* exceeds the task stack. Patch by Timofei Ivankov.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to