From 19a22416363b4a494cf1ff2521599a8150f0f7ce Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Fri, 6 Nov 2026 20:31:33 +0300 Subject: [PATCH 1/9] gh-161502: Fix missing tasks in asyncio pstree with intermediate Futures --- Lib/asyncio/tools.py | 8 ++++ Lib/test/test_asyncio/test_tools.py | 3 +- Lib/test/test_external_inspection.py | 43 ++++++++++++++++++- ...-11-06-20-12-44.gh-issue-161502.rYZtwK.rst | 3 ++ Modules/_remote_debugging/asyncio.c | 39 +++++++++++++---- Modules/_remote_debugging/clinic/module.c.h | 26 +++++++---- Modules/_remote_debugging/module.c | 33 +++++++++----- 7 files changed, 126 insertions(+), 29 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 6bc53f4d7d3e5f..5f07d9c3f165cb 100644 --- a/Lib/asyncio/tools.py +++ b/Lib/asyncio/tools.py @@ -170,9 +170,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 = "(F)" 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 164b735353de2e..875506e694cc77 100644 --- a/Lib/test/test_asyncio/test_tools.py +++ b/Lib/test/test_asyncio/test_tools.py @@ -19,7 +19,8 @@ def CoroInfo(call_stack, task_name): def TaskInfo(task_id, task_name, coroutine_stack, awaited_by): - return _remote_debugging.TaskInfo((task_id, task_name, coroutine_stack, awaited_by)) + return _remote_debugging.TaskInfo( + (task_id, task_name, coroutine_stack, awaited_by, False)) def AwaitedInfo(thread_id, awaited_by): diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index a3f2eabacf6e88..c621a7510726cf 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -575,6 +575,45 @@ 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_async_generator_running_task_stack(self): # gh-159620 @@ -1583,7 +1622,7 @@ async def main(): ] self.assertIn( TaskInfo( - [ANY, "Task-1", [CoroInfo([main_stack, ANY])], []] + [ANY, "Task-1", [CoroInfo([main_stack, ANY])], [], False] ), entries, ) @@ -1637,6 +1676,7 @@ async def main(): ] ) ], + False, ] ), entries, @@ -1704,6 +1744,7 @@ async def main(): ] ) ], + False, ] ), entries, 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/asyncio.c b/Modules/_remote_debugging/asyncio.c index d31f783186a2c3..4465724dabdcc1 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -554,10 +554,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; @@ -591,7 +592,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; @@ -632,6 +642,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; @@ -672,13 +683,15 @@ process_single_task_node( 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; for (Py_ssize_t i = 0; i < PyList_GET_SIZE(result); i++) { PyObject *task_info = PyList_GET_ITEM(result, i); 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) { + 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, @@ -686,7 +699,6 @@ process_task_waiters( 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); void *task_ptr = PyLong_AsVoidPtr(task_id); if (task_ptr == NULL && PyErr_Occurred()) { @@ -694,6 +706,17 @@ process_task_waiters( "Failed to parse waiter task ID"); return -1; } + 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) { + return -1; + } + if (is_task) { + continue; + } + } if (process_single_task_node( unwinder, (uintptr_t)task_ptr, NULL, result) < 0) { @@ -935,7 +958,7 @@ append_awaited_by( return -1; } - return 0; + return process_task_waiters(unwinder, awaited_by_for_thread, true); } /* ============================================================================ @@ -999,7 +1022,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; } diff --git a/Modules/_remote_debugging/clinic/module.c.h b/Modules/_remote_debugging/clinic/module.c.h index 9895286cdb8019..bb545fdf03df0d 100644 --- a/Modules/_remote_debugging/clinic/module.c.h +++ b/Modules/_remote_debugging/clinic/module.c.h @@ -295,14 +295,17 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_all_awaited_by__doc__, "\n" " - thread_id (int): Identifier of the thread, or 0 for tasks in the\n" " interpreter\'s fallback task list.\n" -" - awaited_by (list[TaskInfo]): Tasks registered with this thread.\n" +" - awaited_by (list[TaskInfo]): Tasks registered with this thread and\n" +" intermediate futures in their awaited_by chains.\n" "\n" "Each TaskInfo contains:\n" " - task_id (int): Identifier of the task.\n" -" - task_name (str): Name of the task.\n" +" - task_name (str): Name of the task, or a generated name for\n" +" a future.\n" " - coroutine_stack (list[CoroInfo]): Stack of coroutine frames.\n" " - awaited_by (list[CoroInfo]): Coroutine information for tasks or futures\n" " awaiting this task.\n" +" - is_future (bool): True for a Future that is not a Task.\n" "\n" "Each CoroInfo contains:\n" " - call_stack (list[FrameInfo]): Call stack frames for the coroutine.\n" @@ -324,7 +327,8 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_all_awaited_by__doc__, " task_id=1,\n" " task_name=\"Task-1\",\n" " coroutine_stack=[...],\n" -" awaited_by=[]\n" +" awaited_by=[],\n" +" is_future=False\n" " )\n" " ]\n" " )\n" @@ -367,14 +371,16 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, "\n" " - thread_id (int): Identifier of the thread.\n" " - awaited_by (list[TaskInfo]): The running task and all its\n" -" transitive waiters.\n" +" transitive waiters, including intermediate futures.\n" "\n" "Each TaskInfo contains:\n" " - task_id (int): Identifier of the task.\n" -" - task_name (str): Name of the task.\n" +" - task_name (str): Name of the task, or a generated name for\n" +" a future.\n" " - coroutine_stack (list[CoroInfo]): Stack of coroutine frames.\n" " - awaited_by (list[CoroInfo]): Coroutine information for\n" " tasks or futures awaiting this task.\n" +" - is_future (bool): True for a Future that is not a Task.\n" "\n" "Each CoroInfo contains:\n" " - call_stack (list[FrameInfo]): Frames for the coroutine.\n" @@ -408,7 +414,10 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, " coroutine_stack=[\n" " CoroInfo(call_stack=[...], task_name=1)\n" " ],\n" -" awaited_by=[CoroInfo(call_stack=[...], task_name=2)]\n" +" awaited_by=[\n" +" CoroInfo(call_stack=[...], task_name=2)\n" +" ],\n" +" is_future=False\n" " ),\n" " TaskInfo(\n" " task_id=2,\n" @@ -416,7 +425,8 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, " coroutine_stack=[\n" " CoroInfo(call_stack=[...], task_name=2)\n" " ],\n" -" awaited_by=[]\n" +" awaited_by=[],\n" +" is_future=False\n" " )\n" " ])\n" " ]"); @@ -1740,4 +1750,4 @@ _remote_debugging_get_gc_stats(PyObject *module, PyObject *const *args, Py_ssize exit: return return_value; } -/*[clinic end generated code: output=2667550106e045bd input=a9049054013a1b77]*/ +/*[clinic end generated code: output=14b1276cd3302ecc input=a9049054013a1b77]*/ diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index 28f73a86dcdbda..d4c2b8dfb63742 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -34,14 +34,15 @@ 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} }; PyStructSequence_Desc TaskInfo_desc = { "_remote_debugging.TaskInfo", - "Information about an asyncio task", + "Information about an asyncio task or future", TaskInfo_fields, - 4 + 5 }; // LocationInfo structseq type @@ -914,14 +915,17 @@ Get awaited_by relationships for tasks in the remote process. - thread_id (int): Identifier of the thread, or 0 for tasks in the interpreter's fallback task list. - - awaited_by (list[TaskInfo]): Tasks registered with this thread. + - awaited_by (list[TaskInfo]): Tasks registered with this thread and + intermediate futures in their awaited_by chains. Each TaskInfo contains: - task_id (int): Identifier of the task. - - task_name (str): Name of the task. + - task_name (str): Name of the task, or a generated name for + a future. - coroutine_stack (list[CoroInfo]): Stack of coroutine frames. - awaited_by (list[CoroInfo]): Coroutine information for tasks or futures awaiting this task. + - is_future (bool): True for a Future that is not a Task. Each CoroInfo contains: - call_stack (list[FrameInfo]): Call stack frames for the coroutine. @@ -943,7 +947,8 @@ Example output: task_id=1, task_name="Task-1", coroutine_stack=[...], - awaited_by=[] + awaited_by=[], + is_future=False ) ] ) @@ -952,7 +957,7 @@ Example output: static PyObject * _remote_debugging_RemoteUnwinder_get_all_awaited_by_impl(RemoteUnwinderObject *self) -/*[clinic end generated code: output=6a49cd345e8aec53 input=79e556b3973d21a8]*/ +/*[clinic end generated code: output=6a49cd345e8aec53 input=96fbf9a0e9096045]*/ { if (ensure_async_debug_offsets(self) < 0) { return NULL; @@ -1015,14 +1020,16 @@ running task, tasks waiting for those tasks, etc.). - thread_id (int): Identifier of the thread. - awaited_by (list[TaskInfo]): The running task and all its - transitive waiters. + transitive waiters, including intermediate futures. Each TaskInfo contains: - task_id (int): Identifier of the task. - - task_name (str): Name of the task. + - task_name (str): Name of the task, or a generated name for + a future. - coroutine_stack (list[CoroInfo]): Stack of coroutine frames. - awaited_by (list[CoroInfo]): Coroutine information for tasks or futures awaiting this task. + - is_future (bool): True for a Future that is not a Task. Each CoroInfo contains: - call_stack (list[FrameInfo]): Frames for the coroutine. @@ -1056,7 +1063,10 @@ Example output: coroutine_stack=[ CoroInfo(call_stack=[...], task_name=1) ], - awaited_by=[CoroInfo(call_stack=[...], task_name=2)] + awaited_by=[ + CoroInfo(call_stack=[...], task_name=2) + ], + is_future=False ), TaskInfo( task_id=2, @@ -1064,7 +1074,8 @@ Example output: coroutine_stack=[ CoroInfo(call_stack=[...], task_name=2) ], - awaited_by=[] + awaited_by=[], + is_future=False ) ]) ] @@ -1073,7 +1084,7 @@ Example output: static PyObject * _remote_debugging_RemoteUnwinder_get_async_stack_trace_impl(RemoteUnwinderObject *self) -/*[clinic end generated code: output=6433d52b55e87bbe input=0532031a7c495491]*/ +/*[clinic end generated code: output=6433d52b55e87bbe input=648a5752d51242b9]*/ { if (ensure_async_debug_offsets(self) < 0) { return NULL; From bcae8525e48c6a36f04b286ce32f92351c424ece Mon Sep 17 00:00:00 2001 From: Timofei Ivankov <128279579+deadlovelll@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:40:58 +0300 Subject: [PATCH 2/9] Update Modules/_remote_debugging/asyncio.c Co-authored-by: Kumar Aditya --- Modules/_remote_debugging/asyncio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_remote_debugging/asyncio.c b/Modules/_remote_debugging/asyncio.c index 4465724dabdcc1..79950e0dba4fe3 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -593,8 +593,8 @@ process_single_task_node( PyObject *coroutine_stack = NULL; char is_task; - // task_is_task == 1 - task - // task_is_task == 0 - future + // 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) { From 9d03238241b44f35ee6a37074dc128afbf5f1905 Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Mon, 9 Nov 2026 17:48:24 +0300 Subject: [PATCH 3/9] review changes --- Lib/asyncio/tools.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/asyncio/tools.py b/Lib/asyncio/tools.py index 5f07d9c3f165cb..1ee4a471ba5bb0 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) @@ -180,7 +181,7 @@ def build_async_tree(result, task_emoji="(T)", cor_emoji=""): 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 = "(F)" + flag = fut_emoji return f"{flag} {labels[node]}" def render(root): From dd8fa032d26ddfcb13cb0e30c7891590f208417b Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Fri, 20 Nov 2026 14:03:48 +0300 Subject: [PATCH 4/9] regen clinic --- Modules/_remote_debugging/clinic/module.c.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_remote_debugging/clinic/module.c.h b/Modules/_remote_debugging/clinic/module.c.h index 8f5d6cb15421ad..1843f2d5e76c9f 100644 --- a/Modules/_remote_debugging/clinic/module.c.h +++ b/Modules/_remote_debugging/clinic/module.c.h @@ -1750,4 +1750,4 @@ _remote_debugging_get_gc_stats(PyObject *module, PyObject *const *args, Py_ssize exit: return return_value; } -/*[clinic end generated code: output=ef0d1eaac8262c2c input=a9049054013a1b77]*/ +/*[clinic end generated code: output=53620298ea6bc685 input=a9049054013a1b77]*/ From a657e860282aa473cdb9d86e784a7804fba0d820 Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Fri, 20 Nov 2026 14:17:23 +0300 Subject: [PATCH 5/9] rename awaited_by_for_thread to task_list --- Modules/_remote_debugging/asyncio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_remote_debugging/asyncio.c b/Modules/_remote_debugging/asyncio.c index 04d260f7441946..bfef8750062ee0 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -950,7 +950,7 @@ append_awaited_by( return -1; } - return process_task_waiters(unwinder, awaited_by_for_thread, true); + return process_task_waiters(unwinder, task_list, true); } /* ============================================================================ From 91f2625ae05e27d945c1108414c00a4bcff78ecd Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Fri, 20 Nov 2026 21:00:53 +0300 Subject: [PATCH 6/9] return TaskInfo to 4 parameters --- Lib/test/test_asyncio/test_tools.py | 3 +-- Lib/test/test_external_inspection.py | 4 +--- Modules/_remote_debugging/clinic/module.c.h | 15 ++++++--------- Modules/_remote_debugging/module.c | 19 ++++++++----------- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/Lib/test/test_asyncio/test_tools.py b/Lib/test/test_asyncio/test_tools.py index 875506e694cc77..164b735353de2e 100644 --- a/Lib/test/test_asyncio/test_tools.py +++ b/Lib/test/test_asyncio/test_tools.py @@ -19,8 +19,7 @@ def CoroInfo(call_stack, task_name): def TaskInfo(task_id, task_name, coroutine_stack, awaited_by): - return _remote_debugging.TaskInfo( - (task_id, task_name, coroutine_stack, awaited_by, False)) + return _remote_debugging.TaskInfo((task_id, task_name, coroutine_stack, awaited_by)) def AwaitedInfo(thread_id, awaited_by): diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index d6f8dddc6394dc..7cdd09316538db 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -1666,7 +1666,7 @@ async def main(): ] self.assertIn( TaskInfo( - [ANY, "Task-1", [CoroInfo([main_stack, ANY])], [], False] + [ANY, "Task-1", [CoroInfo([main_stack, ANY])], []] ), entries, ) @@ -1720,7 +1720,6 @@ async def main(): ] ) ], - False, ] ), entries, @@ -1788,7 +1787,6 @@ async def main(): ] ) ], - False, ] ), entries, diff --git a/Modules/_remote_debugging/clinic/module.c.h b/Modules/_remote_debugging/clinic/module.c.h index 1843f2d5e76c9f..26e4a3c2c2e84b 100644 --- a/Modules/_remote_debugging/clinic/module.c.h +++ b/Modules/_remote_debugging/clinic/module.c.h @@ -305,7 +305,7 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_all_awaited_by__doc__, " - coroutine_stack (list[CoroInfo]): Stack of coroutine frames.\n" " - awaited_by (list[CoroInfo]): Coroutine information for tasks or futures\n" " awaiting this task.\n" -" - is_future (bool): True for a Future that is not a Task.\n" +" - is_future (bool, named-only): True for a plain Future.\n" "\n" "Each CoroInfo contains:\n" " - call_stack (list[FrameInfo]): Call stack frames for the coroutine.\n" @@ -327,8 +327,7 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_all_awaited_by__doc__, " task_id=1,\n" " task_name=\"Task-1\",\n" " coroutine_stack=[...],\n" -" awaited_by=[],\n" -" is_future=False\n" +" awaited_by=[]\n" " )\n" " ]\n" " )\n" @@ -380,7 +379,7 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, " - coroutine_stack (list[CoroInfo]): Stack of coroutine frames.\n" " - awaited_by (list[CoroInfo]): Coroutine information for\n" " tasks or futures awaiting this task.\n" -" - is_future (bool): True for a Future that is not a Task.\n" +" - is_future (bool, named-only): True for a plain Future.\n" "\n" "Each CoroInfo contains:\n" " - call_stack (list[FrameInfo]): Frames for the coroutine.\n" @@ -416,8 +415,7 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, " ],\n" " awaited_by=[\n" " CoroInfo(call_stack=[...], task_name=2)\n" -" ],\n" -" is_future=False\n" +" ]\n" " ),\n" " TaskInfo(\n" " task_id=2,\n" @@ -425,8 +423,7 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, " coroutine_stack=[\n" " CoroInfo(call_stack=[...], task_name=2)\n" " ],\n" -" awaited_by=[],\n" -" is_future=False\n" +" awaited_by=[]\n" " )\n" " ])\n" " ]"); @@ -1750,4 +1747,4 @@ _remote_debugging_get_gc_stats(PyObject *module, PyObject *const *args, Py_ssize exit: return return_value; } -/*[clinic end generated code: output=53620298ea6bc685 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=2be4cb3f27f6c8d8 input=a9049054013a1b77]*/ diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index 8739e349cfa86a..2031fe0364f4eb 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -42,7 +42,7 @@ PyStructSequence_Desc TaskInfo_desc = { "_remote_debugging.TaskInfo", "Information about an asyncio task or future", TaskInfo_fields, - 5 + 4 }; // LocationInfo structseq type @@ -898,7 +898,7 @@ Each TaskInfo contains: - coroutine_stack (list[CoroInfo]): Stack of coroutine frames. - awaited_by (list[CoroInfo]): Coroutine information for tasks or futures awaiting this task. - - is_future (bool): True for a Future that is not a Task. + - is_future (bool, named-only): True for a plain Future. Each CoroInfo contains: - call_stack (list[FrameInfo]): Call stack frames for the coroutine. @@ -920,8 +920,7 @@ Example output: task_id=1, task_name="Task-1", coroutine_stack=[...], - awaited_by=[], - is_future=False + awaited_by=[] ) ] ) @@ -930,7 +929,7 @@ Example output: static PyObject * _remote_debugging_RemoteUnwinder_get_all_awaited_by_impl(RemoteUnwinderObject *self) -/*[clinic end generated code: output=6a49cd345e8aec53 input=96fbf9a0e9096045]*/ +/*[clinic end generated code: output=6a49cd345e8aec53 input=17fcd3d7bb835624]*/ { if (ensure_async_debug_offsets(self) < 0) { return NULL; @@ -1002,7 +1001,7 @@ Each TaskInfo contains: - coroutine_stack (list[CoroInfo]): Stack of coroutine frames. - awaited_by (list[CoroInfo]): Coroutine information for tasks or futures awaiting this task. - - is_future (bool): True for a Future that is not a Task. + - is_future (bool, named-only): True for a plain Future. Each CoroInfo contains: - call_stack (list[FrameInfo]): Frames for the coroutine. @@ -1038,8 +1037,7 @@ Example output: ], awaited_by=[ CoroInfo(call_stack=[...], task_name=2) - ], - is_future=False + ] ), TaskInfo( task_id=2, @@ -1047,8 +1045,7 @@ Example output: coroutine_stack=[ CoroInfo(call_stack=[...], task_name=2) ], - awaited_by=[], - is_future=False + awaited_by=[] ) ]) ] @@ -1057,7 +1054,7 @@ Example output: static PyObject * _remote_debugging_RemoteUnwinder_get_async_stack_trace_impl(RemoteUnwinderObject *self) -/*[clinic end generated code: output=6433d52b55e87bbe input=648a5752d51242b9]*/ +/*[clinic end generated code: output=6433d52b55e87bbe input=a95958816358476a]*/ { if (ensure_async_debug_offsets(self) < 0) { return NULL; From 914e14e686f554de30637c547de925002c161854 Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Fri, 20 Nov 2026 21:47:18 +0300 Subject: [PATCH 7/9] dedup adresses with set --- Lib/test/test_external_inspection.py | 24 +++++++++++++++++++++ Modules/_remote_debugging/asyncio.c | 31 ++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 7cdd09316538db..8524dd6b05f42b 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -647,6 +647,30 @@ async def main(method): } 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) + + 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 diff --git a/Modules/_remote_debugging/asyncio.c b/Modules/_remote_debugging/asyncio.c index bfef8750062ee0..fa12d71d2d3e7c 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -667,31 +667,45 @@ process_task_waiters( 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++) { + PyObject *waiter = PyList_GET_ITEM(waiters, j); + 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; + } 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"); - return -1; + goto error; } - PyObject *waiter = PyList_GET_ITEM(waiters, j); - PyObject *task_id = PyStructSequence_GET_ITEM(waiter, 1); 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) { - return -1; + goto error; } if (is_task) { continue; @@ -700,12 +714,17 @@ process_task_waiters( 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; } /* ============================================================================ From 744368826c74374bebe59743eb4f7b8fb0280d8a Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Fri, 20 Nov 2026 22:02:50 +0300 Subject: [PATCH 8/9] skip tasks before checking limit --- Lib/test/test_external_inspection.py | 23 +++++++++++++++++++++++ Modules/_remote_debugging/asyncio.c | 14 +++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 8524dd6b05f42b..b4f05615890fed 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -4850,6 +4850,29 @@ 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) + snapshot = RemoteUnwinder(os.getpid()).get_all_awaited_by() + count = sum(node.is_future for thread in snapshot + for node in thread.awaited_by) + self.assertEqual(count, limit) + 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/Modules/_remote_debugging/asyncio.c b/Modules/_remote_debugging/asyncio.c index fa12d71d2d3e7c..04c0e0ba0200f5 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -687,13 +687,6 @@ process_task_waiters( if (seen < 0 || PySet_Add(visited, task_id) < 0) { goto error; } - 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; - } void *task_ptr = PyLong_AsVoidPtr(task_id); if (task_ptr == NULL && PyErr_Occurred()) { set_exception_cause(unwinder, PyExc_RuntimeError, @@ -711,6 +704,13 @@ process_task_waiters( 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) { From 2ba957cb26a3e0c4d66cb11ca91a93fb7ac06b9c Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Fri, 20 Nov 2026 22:10:21 +0300 Subject: [PATCH 9/9] slightly improve tests quality --- Lib/test/test_external_inspection.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index b4f05615890fed..abee03621b1ae8 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -660,6 +660,10 @@ async def main(): 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, @@ -4864,10 +4868,18 @@ async def main(): release = asyncio.Event() child = asyncio.create_task(release.wait()) waiter = asyncio.create_task(asyncio.wait([child]), eager_start=True) - snapshot = RemoteUnwinder(os.getpid()).get_all_awaited_by() + 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