Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions Lib/test/test_external_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a graph where two waiters share the same parent? Both changed tests still pass without the seen set. The extra b task does not make the walk visit a task twice.

My shared-parent check returns root, p, q, shared, shared without seen. It returns four unique tasks with this patch. A cycle case can also check that the walk terminates.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. just pushed the change!

# 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():
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 4 additions & 2 deletions Modules/_remote_debugging/asyncio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Loading