diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index e8862660baf30d..805604d2c29443 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -1,6 +1,7 @@ import unittest from contextlib import contextmanager import asyncio +import collections.abc import contextlib import os import textwrap @@ -13,6 +14,7 @@ from unittest.mock import ANY from test.support import ( os_helper, + threading_helper, SHORT_TIMEOUT, busy_retry, requires_gil_enabled, @@ -80,6 +82,85 @@ def _extract_coroutine_funcnames(self, trace): for task in info.awaited_by } + @skip_if_not_supported + def test_running_task_stack_between_steps(self): + # gh-159645 + traces = [] + + class Loop(asyncio.SelectorEventLoop): + def call_soon(self, *args, **kwargs): + if asyncio.current_task(self) is not None: + unwinder = RemoteUnwinder(os.getpid()) + traces.append(unwinder.get_async_stack_trace()) + return super().call_soon(*args, **kwargs) + + async def child(): + await asyncio.sleep(0) + + async def main(): + await asyncio.create_task(child(), name="child") + + asyncio.run(main(), loop_factory=Loop) + stacks = [list(self._extract_coroutine_funcnames(trace).values()) + for trace in traces] + # In 3.14 the coroutine walk stops at the generator used by sleep(0). + self.assertIn([["sleep", "child"], ["main"]], stacks) + self.assertIn([[], ["main"]], stacks) + + @skip_if_not_supported + def test_running_task_frame_not_in_thread_stack(self): + # gh-159645 + ready = threading.Event() + release = threading.Event() + + async def child(): + ready.set() + release.wait() + await asyncio.sleep(0) + + async def main(): + loop = asyncio.get_running_loop() + coro = child() + task = asyncio.create_task(coro, name="child") + thread = threading.Thread(target=coro.send, args=(None,)) + with threading_helper.start_threads([thread], unlock=release.set): + self.assertTrue(ready.wait(SHORT_TIMEOUT)) + self.assertTrue(coro.cr_running) + previous = tasks._swap_current_task(loop, task) + try: + trace = RemoteUnwinder(os.getpid()).get_async_stack_trace() + finally: + tasks._swap_current_task(loop, previous) + await task + return trace + + stacks = self._extract_coroutine_funcnames(asyncio.run(main())) + self.assertEqual(stacks["child"], ["child"]) + + @skip_if_not_supported + def test_running_task_with_non_native_coroutine(self): + # gh-159645 + class Wrapper(collections.abc.Coroutine): + def __init__(self, coro): + self._coro = coro + def send(self, value): + return self._coro.send(value) + def throw(self, *args): + return self._coro.throw(*args) + def close(self): + return self._coro.close() + def __await__(self): + return self._coro.__await__() + + async def inner(): + return RemoteUnwinder(os.getpid()).get_async_stack_trace() + + async def main(): + return await asyncio.create_task(Wrapper(inner()), name="wrapper") + + stacks = self._extract_coroutine_funcnames(asyncio.run(main())) + self.assertEqual(stacks["wrapper"], []) + @skip_if_not_supported def test_async_generator_stack_is_not_truncated(self): # gh-159620 diff --git a/Misc/NEWS.d/next/Library/2026-10-18-19-18-20.gh-issue-159645.IOXcpT.rst b/Misc/NEWS.d/next/Library/2026-10-18-19-18-20.gh-issue-159645.IOXcpT.rst new file mode 100644 index 00000000000000..c517e81b18e6b3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-18-19-18-20.gh-issue-159645.IOXcpT.rst @@ -0,0 +1,2 @@ +Fix remote inspection of suspended asyncio tasks. Their stacks could include +frames from the event loop. Patch by Timofei Ivankov. diff --git a/Modules/_remote_debugging_module.c b/Modules/_remote_debugging_module.c index f47380467e3e4c..f226f1590376ec 100644 --- a/Modules/_remote_debugging_module.c +++ b/Modules/_remote_debugging_module.c @@ -788,7 +788,7 @@ parse_async_frame_chain( RemoteUnwinderObject *unwinder, PyObject *calls, uintptr_t address_of_thread, - uintptr_t running_task_code_obj + uintptr_t running_task_frame ); static int read_py_ptr(RemoteUnwinderObject *unwinder, uintptr_t address, uintptr_t *ptr_addr); @@ -797,7 +797,7 @@ static int process_task_waiters(RemoteUnwinderObject *unwinder, PyObject *result bool futures_only); static int parse_task_awaited_by(RemoteUnwinderObject *unwinder, uintptr_t task_address, PyObject *awaited_by); static int find_running_task_in_thread(RemoteUnwinderObject *unwinder, uintptr_t thread_state_addr, uintptr_t *running_task_addr); -static int get_task_code_object(RemoteUnwinderObject *unwinder, uintptr_t task_addr, uintptr_t *code_obj_addr); +static int get_task_frame(RemoteUnwinderObject *unwinder, uintptr_t task_addr, uintptr_t *frame_addr); static int append_awaited_by(RemoteUnwinderObject *unwinder, unsigned long tid, uintptr_t head_addr, PyObject *result); /* ============================================================================ @@ -1881,8 +1881,8 @@ process_running_task_chain( uintptr_t thread_state_addr, PyObject *result ) { - uintptr_t running_task_code_obj = 0; - if(get_task_code_object(unwinder, running_task_addr, &running_task_code_obj) < 0) { + uintptr_t running_task_frame = 0; + if(get_task_frame(unwinder, running_task_addr, &running_task_frame) < 0) { return -1; } @@ -1892,6 +1892,11 @@ process_running_task_chain( return -1; } + // Non-native coroutines have no frame to look for. + if (running_task_frame == 0) { + return process_task_waiters(unwinder, result, false); + } + // Get the chain from the current frame to this task PyObject *coro_chain = PyStructSequence_GET_ITEM(task_info, 2); assert(coro_chain != NULL); @@ -1907,14 +1912,8 @@ process_running_task_chain( PyObject *frame_chain = PyStructSequence_GET_ITEM(coro_info, 0); assert(frame_chain != NULL); - // Clear the coro_chain - if (PyList_Clear(frame_chain) < 0) { - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to clear coroutine chain"); - return -1; - } - // Add the chain from the current frame to this task - if (parse_async_frame_chain(unwinder, frame_chain, thread_state_addr, running_task_code_obj) < 0) { + if (parse_async_frame_chain(unwinder, frame_chain, thread_state_addr, running_task_frame) < 0) { return -1; } @@ -2090,7 +2089,7 @@ find_running_task_in_thread( } static int -get_task_code_object(RemoteUnwinderObject *unwinder, uintptr_t task_addr, uintptr_t *code_obj_addr) { +get_task_frame(RemoteUnwinderObject *unwinder, uintptr_t task_addr, uintptr_t *frame_addr) { uintptr_t running_coro_addr = 0; if(read_py_ptr( @@ -2110,22 +2109,12 @@ get_task_code_object(RemoteUnwinderObject *unwinder, uintptr_t task_addr, uintpt if (get_coro_like_address(unwinder, running_coro_addr, &running_coro_addr) < 0) { return -1; } - - // note: genobject's gi_iframe is an embedded struct so the address to - // the offset leads directly to its first field: f_executable - if (read_py_ptr( - unwinder, - running_coro_addr + (uintptr_t)unwinder->debug_offsets.gen_object.gi_iframe, code_obj_addr) < 0) { - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read running task code object"); - return -1; - } - - if (*code_obj_addr == 0) { - PyErr_SetString(PyExc_RuntimeError, "Running task code object is NULL"); - set_exception_cause(unwinder, PyExc_RuntimeError, "Running task code object address is NULL"); - return -1; + if (running_coro_addr == 0) { + *frame_addr = 0; + return 0; } + *frame_addr = running_coro_addr + (uintptr_t)unwinder->debug_offsets.gen_object.gi_iframe; return 0; } @@ -2904,8 +2893,9 @@ static int RemoteUnwinderObject *unwinder, PyObject *calls, uintptr_t address_of_thread, - uintptr_t running_task_code_obj + uintptr_t running_task_frame ) { + Py_ssize_t coro_chain_size = PyList_GET_SIZE(calls); uintptr_t address_of_current_frame; if (find_running_frame(unwinder, address_of_thread, &address_of_current_frame) < 0) { set_exception_cause(unwinder, PyExc_RuntimeError, "Running frame search failed in async chain"); @@ -2921,6 +2911,7 @@ static int } PyObject* frame_info = NULL; uintptr_t address_of_code_object; + uintptr_t this_frame = address_of_current_frame; int res = parse_frame_object( unwinder, &frame_info, @@ -2946,12 +2937,14 @@ static int Py_DECREF(frame_info); - if (address_of_code_object == running_task_code_obj) { - break; + // Code objects can be shared by recursive calls. Match the frame itself. + if (this_frame == running_task_frame) { + return PyList_SetSlice(calls, 0, coro_chain_size, NULL); } } - return 0; + // Keep the coroutine chain if the task's frame was not on this thread. + return PyList_SetSlice(calls, coro_chain_size, PY_SSIZE_T_MAX, NULL); } /* ============================================================================