From 51c294926595e778a30a5cc2c4e887b42bc671d2 Mon Sep 17 00:00:00 2001 From: Timofei Ivankov <128279579+deadlovelll@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:16:04 +0300 Subject: [PATCH] gh-161502: Fix missing tasks in asyncio pstree with intermediate Futures (#161508) Include intermediate Future nodes when the remote asyncio unwinder builds awaited-by graphs. (cherry picked from commit ab5b8f87a70b62a22c1ed78b3e26ccd9ba84caaa) --- Lib/asyncio/tools.py | 13 ++- Lib/test/test_asyncio/test_tools.py | 6 +- Lib/test/test_external_inspection.py | 98 +++++++++++++++++++ ...-11-06-20-12-44.gh-issue-161502.rYZtwK.rst | 3 + Modules/_remote_debugging_module.c | 92 ++++++++++++----- Modules/clinic/_remote_debugging_module.c.h | 10 +- 6 files changed, 192 insertions(+), 30 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-06-20-12-44.gh-issue-161502.rYZtwK.rst diff --git a/Lib/asyncio/tools.py b/Lib/asyncio/tools.py index e65d029f9ec165..fd5b60f5766096 100644 --- a/Lib/asyncio/tools.py +++ b/Lib/asyncio/tools.py @@ -157,12 +157,13 @@ def get_all_awaited_by(pid): return unwinder.get_all_awaited_by() -def build_async_tree(result, task_emoji="(T)", cor_emoji=""): +def build_async_tree(result, task_emoji="(T)", cor_emoji="", fut_emoji="(F)"): """ Build a list of strings for pretty-print an async call tree. The call tree is produced by `get_all_async_stacks()`, prefixing tasks - with `task_emoji` and coroutine frames with `cor_emoji`. + with `task_emoji`, futures with `fut_emoji`, and coroutine frames with + `cor_emoji`. """ id2name, awaits, task_stacks = _index(result) g = _task_graph(awaits) @@ -170,9 +171,17 @@ def build_async_tree(result, task_emoji="(T)", cor_emoji=""): if cycles: raise CycleFoundException(cycles, id2name) labels, children = _build_tree(id2name, awaits, task_stacks) + future_ids = { + node.task_id + for thread in result + for node in thread.awaited_by + if node.is_future + } def pretty(node): flag = task_emoji if node[0] == NodeType.TASK else cor_emoji + if node[0] == NodeType.TASK and node[1] in future_ids: + flag = fut_emoji return f"{flag} {labels[node]}" def render(root): diff --git a/Lib/test/test_asyncio/test_tools.py b/Lib/test/test_asyncio/test_tools.py index ef8b5c4aef6807..a1a89a12a33994 100644 --- a/Lib/test/test_asyncio/test_tools.py +++ b/Lib/test/test_asyncio/test_tools.py @@ -7,7 +7,11 @@ FrameInfo = namedtuple('FrameInfo', ['funcname', 'filename', 'lineno']) CoroInfo = namedtuple('CoroInfo', ['call_stack', 'task_name']) -TaskInfo = namedtuple('TaskInfo', ['task_id', 'task_name', 'coroutine_stack', 'awaited_by']) +class TaskInfo(namedtuple('TaskInfoBase', + ['task_id', 'task_name', 'coroutine_stack', 'awaited_by'])): + is_future = False + + AwaitedInfo = namedtuple('AwaitedInfo', ['thread_id', 'awaited_by']) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 03f8e27a4e1ea1..862b4f34245831 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -135,6 +135,73 @@ async def main(): stacks = self._extract_coroutine_funcnames(asyncio.run(main())) self.assertEqual(stacks["anext"], ["agen"]) + @skip_if_not_supported + def test_future_between_tasks(self): + # gh-161502 + from asyncio.tools import build_async_tree + + async def worker(method): + return getattr(RemoteUnwinder(os.getpid()), method)() + + async def main(method): + parent = asyncio.current_task() + parent.set_name("main") + future = asyncio.Future() + task = asyncio.create_task(worker(method), name="worker") + asyncio.future_add_to_awaited_by(task, future) + task.add_done_callback(future.set_result) + try: + await future + return task.result(), { + id(parent): [], + id(future): [id(parent)], + id(task): [id(future)], + } + finally: + asyncio.future_discard_from_awaited_by(task, future) + + for method in ("get_all_awaited_by", "get_async_stack_trace"): + with self.subTest(method=method): + snapshot, expected = asyncio.run(main(method)) + tree, = build_async_tree(snapshot) + rendered = "\n".join(tree) + self.assertIn("(T) main", rendered) + self.assertIn("(F) Future-", rendered) + self.assertIn("(T) worker", rendered) + relationships = { + node.task_id: [edge.task_name for edge in node.awaited_by] + for thread in snapshot for node in thread.awaited_by + } + self.assertEqual(relationships, expected) + + @skip_if_not_supported + def test_shared_future_waiter(self): + async def main(): + layers = [[asyncio.current_task()]] + for _ in range(14): + waiters = [asyncio.Future(), asyncio.Future()] + for child in layers[-1]: + for waiter in waiters: + asyncio.future_add_to_awaited_by(child, waiter) + self.addCleanup(asyncio.future_discard_from_awaited_by, + child, waiter) + layers.append(waiters) + + leaf, root = layers[-1][0], layers[0][0] + asyncio.future_add_to_awaited_by(leaf, root) + self.addCleanup(asyncio.future_discard_from_awaited_by, leaf, root) + + expected_ids = [id(node) for layer in layers for node in layer] + unwinder = RemoteUnwinder(os.getpid()) + for method in (unwinder.get_all_awaited_by, + unwinder.get_async_stack_trace): + with self.subTest(method=method.__name__): + ids = [node.task_id for thread in method() + for node in thread.awaited_by] + self.assertCountEqual(ids, expected_ids) + + asyncio.run(main()) + @skip_if_not_supported def test_async_generator_running_task_stack(self): # gh-159620 @@ -1820,6 +1887,37 @@ def _assert_unwinder_limit_error(self, unwind, expected_substring): f"{last_error!r}" ) + @skip_if_not_supported + def test_get_all_awaited_by_at_waiter_limit(self): + # gh-161502 + async def main(): + task = asyncio.current_task() + limit = self.TASK_WAITER_WALK_TASKS - 1 + for _ in range(limit): + future = asyncio.Future() + asyncio.future_add_to_awaited_by(task, future) + self.addCleanup(asyncio.future_discard_from_awaited_by, task, future) + + release = asyncio.Event() + child = asyncio.create_task(release.wait()) + waiter = asyncio.create_task(asyncio.wait([child]), eager_start=True) + unwinder = RemoteUnwinder(os.getpid()) + snapshot = unwinder.get_all_awaited_by() + count = sum(node.is_future for thread in snapshot + for node in thread.awaited_by) + self.assertEqual(count, limit) + + extra = asyncio.Future() + asyncio.future_add_to_awaited_by(task, extra) + self.addCleanup(asyncio.future_discard_from_awaited_by, task, extra) + with self.assertRaisesRegex(RuntimeError, "Too many task waiters"): + unwinder.get_all_awaited_by() + + release.set() + await waiter + + asyncio.run(main()) + @skip_if_not_supported @unittest.skipIf( sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED, diff --git a/Misc/NEWS.d/next/Library/2026-11-06-20-12-44.gh-issue-161502.rYZtwK.rst b/Misc/NEWS.d/next/Library/2026-11-06-20-12-44.gh-issue-161502.rYZtwK.rst new file mode 100644 index 00000000000000..f284c37bd73894 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-06-20-12-44.gh-issue-161502.rYZtwK.rst @@ -0,0 +1,3 @@ +Fix missing tasks in ``python -m asyncio pstree`` output when tasks are +connected through an intermediate :class:`asyncio.Future`. Patch by Timofei +Ivankov diff --git a/Modules/_remote_debugging_module.c b/Modules/_remote_debugging_module.c index 18f915e022b94f..f47380467e3e4c 100644 --- a/Modules/_remote_debugging_module.c +++ b/Modules/_remote_debugging_module.c @@ -604,12 +604,13 @@ static PyStructSequence_Field TaskInfo_fields[] = { {"task_name", "Task name"}, {"coroutine_stack", "Coroutine call stack"}, {"awaited_by", "Tasks awaiting this task"}, + {"is_future", "Whether this node is a plain Future rather than a Task"}, {NULL} }; static PyStructSequence_Desc TaskInfo_desc = { "_remote_debugging.TaskInfo", - "Information about an asyncio task", + "Information about an asyncio task or future", TaskInfo_fields, 4 }; @@ -792,7 +793,8 @@ parse_async_frame_chain( static int read_py_ptr(RemoteUnwinderObject *unwinder, uintptr_t address, uintptr_t *ptr_addr); -static int process_task_waiters(RemoteUnwinderObject *unwinder, PyObject *result); +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); @@ -1739,7 +1741,16 @@ process_single_task_node( PyObject *result_item = NULL; PyObject *coroutine_stack = NULL; - tn = parse_task_name(unwinder, task_addr); + char is_task; + // task_is_task = 1 => task + // task_is_task = 0 => future + if (read_char(unwinder, + task_addr + unwinder->async_debug_offsets.asyncio_task_object.task_is_task, + &is_task) < 0) { + goto error; + } + tn = is_task ? parse_task_name(unwinder, task_addr) + : PyUnicode_FromFormat("Future-%p", (void *)task_addr); if (tn == NULL) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse task name in single task node"); goto error; @@ -1780,6 +1791,7 @@ process_single_task_node( PyStructSequence_SetItem(result_item, 1, tn); // steals ref PyStructSequence_SetItem(result_item, 2, coroutine_stack); // steals ref PyStructSequence_SetItem(result_item, 3, current_awaited_by); // steals ref + PyStructSequence_SetItem(result_item, 4, PyBool_FromLong(!is_task)); // References transferred to tuple task_id = NULL; @@ -1837,10 +1849,11 @@ parse_task_awaited_by( uintptr_t task_address, PyObject *awaited_by ) { - // Read the entire TaskObj at once char task_obj[SIZEOF_TASK_OBJ]; + size_t size = unwinder->async_debug_offsets.asyncio_task_object.task_awaited_by_is_set + + sizeof(char); if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address, - (size_t)unwinder->async_debug_offsets.asyncio_task_object.size, + size, task_obj) < 0) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task object"); return -1; @@ -1906,7 +1919,7 @@ process_running_task_chain( } // Now find all tasks that are waiting for this task and process them - if (process_task_waiters(unwinder, result) < 0) { + if (process_task_waiters(unwinder, result, false) < 0) { return -1; } @@ -1977,37 +1990,68 @@ process_thread_for_async_stack_trace( static int process_task_waiters( RemoteUnwinderObject *unwinder, - PyObject *result + PyObject *result, + bool futures_only ) { + Py_ssize_t initial_count = futures_only ? PyList_GET_SIZE(result) : 0; + PyObject *visited = PySet_New(NULL); + if (visited == NULL) { + return -1; + } for (Py_ssize_t i = 0; i < PyList_GET_SIZE(result); i++) { PyObject *task_info = PyList_GET_ITEM(result, i); + if (PySet_Add(visited, PyStructSequence_GET_ITEM(task_info, 0)) < 0) { + goto error; + } PyObject *waiters = PyStructSequence_GET_ITEM(task_info, 3); for (Py_ssize_t j = 0; j < PyList_GET_SIZE(waiters); j++) { - if (PyList_GET_SIZE(result) >= MAX_TASK_WAITER_WALK_TASKS) { - PyErr_SetString(PyExc_RuntimeError, - "Too many task waiters (possible infinite loop)"); - set_exception_cause(unwinder, PyExc_RuntimeError, - "Task waiter walk size limit exceeded"); - return -1; - } PyObject *waiter = PyList_GET_ITEM(waiters, j); - // CoroInfo item 1 holds the waiter task address stored by parse_task(). PyObject *task_id = PyStructSequence_GET_ITEM(waiter, 1); + int seen = PySet_Contains(visited, task_id); + if (seen > 0) { + continue; + } + if (seen < 0 || PySet_Add(visited, task_id) < 0) { + goto error; + } void *task_ptr = PyLong_AsVoidPtr(task_id); if (task_ptr == NULL && PyErr_Occurred()) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse waiter task ID"); - return -1; + goto error; + } + if (futures_only) { + char is_task; + if (read_char(unwinder, + (uintptr_t)task_ptr + unwinder->async_debug_offsets.asyncio_task_object.task_is_task, + &is_task) < 0) { + goto error; + } + if (is_task) { + continue; + } + } + if (PyList_GET_SIZE(result) - initial_count >= MAX_TASK_WAITER_WALK_TASKS) { + PyErr_SetString(PyExc_RuntimeError, + "Too many task waiters (possible infinite loop)"); + set_exception_cause(unwinder, PyExc_RuntimeError, + "Task waiter walk size limit exceeded"); + goto error; } if (process_single_task_node( unwinder, (uintptr_t)task_ptr, NULL, result) < 0) { - return -1; + goto error; } } } + Py_DECREF(visited); return 0; + +error: + Py_DECREF(visited); + return -1; } static int @@ -3011,7 +3055,7 @@ append_awaited_by( return -1; } - return 0; + return process_task_waiters(unwinder, awaited_by_for_thread, true); } /* ============================================================================ @@ -3447,11 +3491,12 @@ _remote_debugging.RemoteUnwinder.get_all_awaited_by Get all tasks and their awaited_by relationships from the remote process. -This provides a tree structure showing which tasks are waiting for other tasks. +This provides a tree structure showing which tasks and futures are waiting for +other tasks. For each task, returns: 1. The call stack frames leading to where the task is currently executing -2. The name of the task +2. The name of the task, or a generated name for a future 3. A list of tasks that this task is waiting for, with their own frames/names/etc Returns a list of [frames, task_name, subtasks] where: @@ -3487,7 +3532,7 @@ Example output: static PyObject * _remote_debugging_RemoteUnwinder_get_all_awaited_by_impl(RemoteUnwinderObject *self) -/*[clinic end generated code: output=6a49cd345e8aec53 input=a452c652bb00701a]*/ +/*[clinic end generated code: output=6a49cd345e8aec53 input=9125e1b63ae0c0a9]*/ { if (!self->async_debug_offsets_available) { PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available"); @@ -3537,6 +3582,7 @@ Get the currently running async tasks and their dependency graphs from the remot This returns information about running tasks and all tasks that are waiting for them, forming a complete dependency graph for each thread's active task. +Intermediate futures are included in each dependency graph. For each thread with a running task, returns the running task plus all tasks that transitively depend on it (tasks waiting for the running task, tasks waiting for @@ -3548,7 +3594,7 @@ Returns a list of per-thread results, where each thread result contains: Each task info contains: - Task ID (memory address) -- Task name +- Task name, or a generated name for a future - Call stack frames: List of (func_name, filename, lineno) - List of tasks waiting for this task (recursive structure) @@ -3576,7 +3622,7 @@ Example output (similar structure to get_all_awaited_by but only for running tas static PyObject * _remote_debugging_RemoteUnwinder_get_async_stack_trace_impl(RemoteUnwinderObject *self) -/*[clinic end generated code: output=6433d52b55e87bbe input=8744b47c9ec2220a]*/ +/*[clinic end generated code: output=6433d52b55e87bbe input=75424725716fbcda]*/ { if (!self->async_debug_offsets_available) { PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available"); diff --git a/Modules/clinic/_remote_debugging_module.c.h b/Modules/clinic/_remote_debugging_module.c.h index f6a51cdba6b401..790259535936ae 100644 --- a/Modules/clinic/_remote_debugging_module.c.h +++ b/Modules/clinic/_remote_debugging_module.c.h @@ -180,11 +180,12 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_all_awaited_by__doc__, "\n" "Get all tasks and their awaited_by relationships from the remote process.\n" "\n" -"This provides a tree structure showing which tasks are waiting for other tasks.\n" +"This provides a tree structure showing which tasks and futures are waiting for\n" +"other tasks.\n" "\n" "For each task, returns:\n" "1. The call stack frames leading to where the task is currently executing\n" -"2. The name of the task\n" +"2. The name of the task, or a generated name for a future\n" "3. A list of tasks that this task is waiting for, with their own frames/names/etc\n" "\n" "Returns a list of [frames, task_name, subtasks] where:\n" @@ -239,6 +240,7 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, "\n" "This returns information about running tasks and all tasks that are waiting for them,\n" "forming a complete dependency graph for each thread\'s active task.\n" +"Intermediate futures are included in each dependency graph.\n" "\n" "For each thread with a running task, returns the running task plus all tasks that\n" "transitively depend on it (tasks waiting for the running task, tasks waiting for\n" @@ -250,7 +252,7 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, "\n" "Each task info contains:\n" "- Task ID (memory address)\n" -"- Task name\n" +"- Task name, or a generated name for a future\n" "- Call stack frames: List of (func_name, filename, lineno)\n" "- List of tasks waiting for this task (recursive structure)\n" "\n" @@ -288,4 +290,4 @@ _remote_debugging_RemoteUnwinder_get_async_stack_trace(PyObject *self, PyObject return return_value; } -/*[clinic end generated code: output=0dd1e6e8bab2a8b1 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=9d3800769c130513 input=a9049054013a1b77]*/