diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index b763f395bb24eb..51d9bf43ad7358 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -537,6 +537,36 @@ async def rec(n): self.assertEqual(asyncio.run(rec(3)), ["rec"] * 4) + @skip_if_not_supported + def test_deep_awaiter_chain_is_walked_fully(self): + # gh-158688 + depth = 20000 + + async def root(): + await asyncio.sleep(0) + return [ + task.task_name + for info in RemoteUnwinder(os.getpid()).get_async_stack_trace() + for task in info.awaited_by + ] + + async def link(awaited): + return await awaited + + async def main(): + first = asyncio.create_task(root(), name="root") + top = first + for i in range(depth): + top = asyncio.create_task(link(top), name=f"a{i}") + second = asyncio.create_task(link(first), name="b") + await asyncio.wait((top, second)) + second.exception() + return top.result(), asyncio.current_task().get_name() + + names, main_name = asyncio.run(main()) + expected_names = ["root", "b", main_name] + [f"a{i}" for i in range(depth)] + self.assertCountEqual(names, expected_names) + @skip_if_not_supported def test_future_waiter_has_empty_stack(self): async def main(): @@ -4920,9 +4950,9 @@ def recurse(n): sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED, "Test only runs on Linux with process_vm_readv support", ) - def test_get_async_stack_trace_deep_task_waiter_chain_aborts(self): - """Test that a task waiter chain deeper than the limit aborts - the walk instead of overflowing the C stack.""" + def test_get_async_stack_trace_deep_task_waiter_chain_is_walked(self): + """Test that a task waiter chain longer than the walk used to allow + is walked to the end instead of overflowing the C stack.""" script_body = f"""\ import asyncio @@ -4939,10 +4969,12 @@ async def chain(n): """ with self._target_process(script_body) as (p, client_socket, _): _wait_for_signal(client_socket, b"ready") - self._assert_unwinder_limit_error( - lambda: RemoteUnwinder(p.pid).get_async_stack_trace(), - "Too many task waiters", - ) + tasks = [ + task + for info in get_async_stack_trace(p.pid) + for task in info.awaited_by + ] + self.assertEqual(len(tasks), self.TASK_WAITER_WALK_TASKS + 1) client_socket.sendall(b"done") @skip_if_not_supported diff --git a/Misc/NEWS.d/next/Library/2026-10-07-14-35-29.gh-issue-158688.X8kcFc.rst b/Misc/NEWS.d/next/Library/2026-10-07-14-35-29.gh-issue-158688.X8kcFc.rst new file mode 100644 index 00000000000000..10357b889a3986 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-07-14-35-29.gh-issue-158688.X8kcFc.rst @@ -0,0 +1,2 @@ +Fix a crash in the sampling profiler when dumping a program with a deep +chain of awaiting tasks. Patch by Timofei Ivankov. diff --git a/Modules/_remote_debugging/asyncio.c b/Modules/_remote_debugging/asyncio.c index 04c0e0ba0200f5..7b9aae943cbee9 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -666,7 +666,7 @@ process_task_waiters( PyObject *result, bool futures_only ) { - Py_ssize_t initial_count = futures_only ? PyList_GET_SIZE(result) : 0; + Py_ssize_t initial_count = PyList_GET_SIZE(result); PyObject *visited = PySet_New(NULL); if (visited == NULL) { return -1; @@ -704,7 +704,9 @@ process_task_waiters( continue; } } - if (PyList_GET_SIZE(result) - initial_count >= MAX_TASK_WAITER_WALK_TASKS) { + if (futures_only && + 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,