Skip to content

Commit 697da9c

Browse files
authored
[3.14] gh-161502: Fix missing tasks in asyncio pstree with intermediate Futures (#162339)
Backport GH-161508 to Python 3.14.
1 parent 99d2327 commit 697da9c

6 files changed

Lines changed: 192 additions & 30 deletions

File tree

‎Lib/asyncio/tools.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,31 @@ def get_all_awaited_by(pid):
157157
return unwinder.get_all_awaited_by()
158158

159159

160-
def build_async_tree(result, task_emoji="(T)", cor_emoji=""):
160+
def build_async_tree(result, task_emoji="(T)", cor_emoji="", fut_emoji="(F)"):
161161
"""
162162
Build a list of strings for pretty-print an async call tree.
163163
164164
The call tree is produced by `get_all_async_stacks()`, prefixing tasks
165-
with `task_emoji` and coroutine frames with `cor_emoji`.
165+
with `task_emoji`, futures with `fut_emoji`, and coroutine frames with
166+
`cor_emoji`.
166167
"""
167168
id2name, awaits, task_stacks = _index(result)
168169
g = _task_graph(awaits)
169170
cycles = _find_cycles(g)
170171
if cycles:
171172
raise CycleFoundException(cycles, id2name)
172173
labels, children = _build_tree(id2name, awaits, task_stacks)
174+
future_ids = {
175+
node.task_id
176+
for thread in result
177+
for node in thread.awaited_by
178+
if node.is_future
179+
}
173180

174181
def pretty(node):
175182
flag = task_emoji if node[0] == NodeType.TASK else cor_emoji
183+
if node[0] == NodeType.TASK and node[1] in future_ids:
184+
flag = fut_emoji
176185
return f"{flag} {labels[node]}"
177186

178187
def render(root):

‎Lib/test/test_asyncio/test_tools.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
FrameInfo = namedtuple('FrameInfo', ['funcname', 'filename', 'lineno'])
99
CoroInfo = namedtuple('CoroInfo', ['call_stack', 'task_name'])
10-
TaskInfo = namedtuple('TaskInfo', ['task_id', 'task_name', 'coroutine_stack', 'awaited_by'])
10+
class TaskInfo(namedtuple('TaskInfoBase',
11+
['task_id', 'task_name', 'coroutine_stack', 'awaited_by'])):
12+
is_future = False
13+
14+
1115
AwaitedInfo = namedtuple('AwaitedInfo', ['thread_id', 'awaited_by'])
1216

1317

‎Lib/test/test_external_inspection.py‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,73 @@ async def main():
135135
stacks = self._extract_coroutine_funcnames(asyncio.run(main()))
136136
self.assertEqual(stacks["anext"], ["agen"])
137137

138+
@skip_if_not_supported
139+
def test_future_between_tasks(self):
140+
# gh-161502
141+
from asyncio.tools import build_async_tree
142+
143+
async def worker(method):
144+
return getattr(RemoteUnwinder(os.getpid()), method)()
145+
146+
async def main(method):
147+
parent = asyncio.current_task()
148+
parent.set_name("main")
149+
future = asyncio.Future()
150+
task = asyncio.create_task(worker(method), name="worker")
151+
asyncio.future_add_to_awaited_by(task, future)
152+
task.add_done_callback(future.set_result)
153+
try:
154+
await future
155+
return task.result(), {
156+
id(parent): [],
157+
id(future): [id(parent)],
158+
id(task): [id(future)],
159+
}
160+
finally:
161+
asyncio.future_discard_from_awaited_by(task, future)
162+
163+
for method in ("get_all_awaited_by", "get_async_stack_trace"):
164+
with self.subTest(method=method):
165+
snapshot, expected = asyncio.run(main(method))
166+
tree, = build_async_tree(snapshot)
167+
rendered = "\n".join(tree)
168+
self.assertIn("(T) main", rendered)
169+
self.assertIn("(F) Future-", rendered)
170+
self.assertIn("(T) worker", rendered)
171+
relationships = {
172+
node.task_id: [edge.task_name for edge in node.awaited_by]
173+
for thread in snapshot for node in thread.awaited_by
174+
}
175+
self.assertEqual(relationships, expected)
176+
177+
@skip_if_not_supported
178+
def test_shared_future_waiter(self):
179+
async def main():
180+
layers = [[asyncio.current_task()]]
181+
for _ in range(14):
182+
waiters = [asyncio.Future(), asyncio.Future()]
183+
for child in layers[-1]:
184+
for waiter in waiters:
185+
asyncio.future_add_to_awaited_by(child, waiter)
186+
self.addCleanup(asyncio.future_discard_from_awaited_by,
187+
child, waiter)
188+
layers.append(waiters)
189+
190+
leaf, root = layers[-1][0], layers[0][0]
191+
asyncio.future_add_to_awaited_by(leaf, root)
192+
self.addCleanup(asyncio.future_discard_from_awaited_by, leaf, root)
193+
194+
expected_ids = [id(node) for layer in layers for node in layer]
195+
unwinder = RemoteUnwinder(os.getpid())
196+
for method in (unwinder.get_all_awaited_by,
197+
unwinder.get_async_stack_trace):
198+
with self.subTest(method=method.__name__):
199+
ids = [node.task_id for thread in method()
200+
for node in thread.awaited_by]
201+
self.assertCountEqual(ids, expected_ids)
202+
203+
asyncio.run(main())
204+
138205
@skip_if_not_supported
139206
def test_async_generator_running_task_stack(self):
140207
# gh-159620
@@ -1820,6 +1887,37 @@ def _assert_unwinder_limit_error(self, unwind, expected_substring):
18201887
f"{last_error!r}"
18211888
)
18221889

1890+
@skip_if_not_supported
1891+
def test_get_all_awaited_by_at_waiter_limit(self):
1892+
# gh-161502
1893+
async def main():
1894+
task = asyncio.current_task()
1895+
limit = self.TASK_WAITER_WALK_TASKS - 1
1896+
for _ in range(limit):
1897+
future = asyncio.Future()
1898+
asyncio.future_add_to_awaited_by(task, future)
1899+
self.addCleanup(asyncio.future_discard_from_awaited_by, task, future)
1900+
1901+
release = asyncio.Event()
1902+
child = asyncio.create_task(release.wait())
1903+
waiter = asyncio.create_task(asyncio.wait([child]), eager_start=True)
1904+
unwinder = RemoteUnwinder(os.getpid())
1905+
snapshot = unwinder.get_all_awaited_by()
1906+
count = sum(node.is_future for thread in snapshot
1907+
for node in thread.awaited_by)
1908+
self.assertEqual(count, limit)
1909+
1910+
extra = asyncio.Future()
1911+
asyncio.future_add_to_awaited_by(task, extra)
1912+
self.addCleanup(asyncio.future_discard_from_awaited_by, task, extra)
1913+
with self.assertRaisesRegex(RuntimeError, "Too many task waiters"):
1914+
unwinder.get_all_awaited_by()
1915+
1916+
release.set()
1917+
await waiter
1918+
1919+
asyncio.run(main())
1920+
18231921
@skip_if_not_supported
18241922
@unittest.skipIf(
18251923
sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix missing tasks in ``python -m asyncio pstree`` output when tasks are
2+
connected through an intermediate :class:`asyncio.Future`. Patch by Timofei
3+
Ivankov

‎Modules/_remote_debugging_module.c‎

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -604,12 +604,13 @@ static PyStructSequence_Field TaskInfo_fields[] = {
604604
{"task_name", "Task name"},
605605
{"coroutine_stack", "Coroutine call stack"},
606606
{"awaited_by", "Tasks awaiting this task"},
607+
{"is_future", "Whether this node is a plain Future rather than a Task"},
607608
{NULL}
608609
};
609610

610611
static PyStructSequence_Desc TaskInfo_desc = {
611612
"_remote_debugging.TaskInfo",
612-
"Information about an asyncio task",
613+
"Information about an asyncio task or future",
613614
TaskInfo_fields,
614615
4
615616
};
@@ -792,7 +793,8 @@ parse_async_frame_chain(
792793

793794
static int read_py_ptr(RemoteUnwinderObject *unwinder, uintptr_t address, uintptr_t *ptr_addr);
794795

795-
static int process_task_waiters(RemoteUnwinderObject *unwinder, PyObject *result);
796+
static int process_task_waiters(RemoteUnwinderObject *unwinder, PyObject *result,
797+
bool futures_only);
796798
static int parse_task_awaited_by(RemoteUnwinderObject *unwinder, uintptr_t task_address, PyObject *awaited_by);
797799
static int find_running_task_in_thread(RemoteUnwinderObject *unwinder, uintptr_t thread_state_addr, uintptr_t *running_task_addr);
798800
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(
17391741
PyObject *result_item = NULL;
17401742
PyObject *coroutine_stack = NULL;
17411743

1742-
tn = parse_task_name(unwinder, task_addr);
1744+
char is_task;
1745+
// task_is_task = 1 => task
1746+
// task_is_task = 0 => future
1747+
if (read_char(unwinder,
1748+
task_addr + unwinder->async_debug_offsets.asyncio_task_object.task_is_task,
1749+
&is_task) < 0) {
1750+
goto error;
1751+
}
1752+
tn = is_task ? parse_task_name(unwinder, task_addr)
1753+
: PyUnicode_FromFormat("Future-%p", (void *)task_addr);
17431754
if (tn == NULL) {
17441755
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse task name in single task node");
17451756
goto error;
@@ -1780,6 +1791,7 @@ process_single_task_node(
17801791
PyStructSequence_SetItem(result_item, 1, tn); // steals ref
17811792
PyStructSequence_SetItem(result_item, 2, coroutine_stack); // steals ref
17821793
PyStructSequence_SetItem(result_item, 3, current_awaited_by); // steals ref
1794+
PyStructSequence_SetItem(result_item, 4, PyBool_FromLong(!is_task));
17831795

17841796
// References transferred to tuple
17851797
task_id = NULL;
@@ -1837,10 +1849,11 @@ parse_task_awaited_by(
18371849
uintptr_t task_address,
18381850
PyObject *awaited_by
18391851
) {
1840-
// Read the entire TaskObj at once
18411852
char task_obj[SIZEOF_TASK_OBJ];
1853+
size_t size = unwinder->async_debug_offsets.asyncio_task_object.task_awaited_by_is_set
1854+
+ sizeof(char);
18421855
if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address,
1843-
(size_t)unwinder->async_debug_offsets.asyncio_task_object.size,
1856+
size,
18441857
task_obj) < 0) {
18451858
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task object");
18461859
return -1;
@@ -1906,7 +1919,7 @@ process_running_task_chain(
19061919
}
19071920

19081921
// Now find all tasks that are waiting for this task and process them
1909-
if (process_task_waiters(unwinder, result) < 0) {
1922+
if (process_task_waiters(unwinder, result, false) < 0) {
19101923
return -1;
19111924
}
19121925

@@ -1977,37 +1990,68 @@ process_thread_for_async_stack_trace(
19771990
static int
19781991
process_task_waiters(
19791992
RemoteUnwinderObject *unwinder,
1980-
PyObject *result
1993+
PyObject *result,
1994+
bool futures_only
19811995
) {
1996+
Py_ssize_t initial_count = futures_only ? PyList_GET_SIZE(result) : 0;
1997+
PyObject *visited = PySet_New(NULL);
1998+
if (visited == NULL) {
1999+
return -1;
2000+
}
19822001
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(result); i++) {
19832002
PyObject *task_info = PyList_GET_ITEM(result, i);
2003+
if (PySet_Add(visited, PyStructSequence_GET_ITEM(task_info, 0)) < 0) {
2004+
goto error;
2005+
}
19842006
PyObject *waiters = PyStructSequence_GET_ITEM(task_info, 3);
19852007
for (Py_ssize_t j = 0; j < PyList_GET_SIZE(waiters); j++) {
1986-
if (PyList_GET_SIZE(result) >= MAX_TASK_WAITER_WALK_TASKS) {
1987-
PyErr_SetString(PyExc_RuntimeError,
1988-
"Too many task waiters (possible infinite loop)");
1989-
set_exception_cause(unwinder, PyExc_RuntimeError,
1990-
"Task waiter walk size limit exceeded");
1991-
return -1;
1992-
}
19932008
PyObject *waiter = PyList_GET_ITEM(waiters, j);
1994-
// CoroInfo item 1 holds the waiter task address stored by parse_task().
19952009
PyObject *task_id = PyStructSequence_GET_ITEM(waiter, 1);
2010+
int seen = PySet_Contains(visited, task_id);
2011+
if (seen > 0) {
2012+
continue;
2013+
}
2014+
if (seen < 0 || PySet_Add(visited, task_id) < 0) {
2015+
goto error;
2016+
}
19962017
void *task_ptr = PyLong_AsVoidPtr(task_id);
19972018
if (task_ptr == NULL && PyErr_Occurred()) {
19982019
set_exception_cause(unwinder, PyExc_RuntimeError,
19992020
"Failed to parse waiter task ID");
2000-
return -1;
2021+
goto error;
2022+
}
2023+
if (futures_only) {
2024+
char is_task;
2025+
if (read_char(unwinder,
2026+
(uintptr_t)task_ptr + unwinder->async_debug_offsets.asyncio_task_object.task_is_task,
2027+
&is_task) < 0) {
2028+
goto error;
2029+
}
2030+
if (is_task) {
2031+
continue;
2032+
}
2033+
}
2034+
if (PyList_GET_SIZE(result) - initial_count >= MAX_TASK_WAITER_WALK_TASKS) {
2035+
PyErr_SetString(PyExc_RuntimeError,
2036+
"Too many task waiters (possible infinite loop)");
2037+
set_exception_cause(unwinder, PyExc_RuntimeError,
2038+
"Task waiter walk size limit exceeded");
2039+
goto error;
20012040
}
20022041
if (process_single_task_node(
20032042
unwinder, (uintptr_t)task_ptr, NULL, result) < 0)
20042043
{
2005-
return -1;
2044+
goto error;
20062045
}
20072046
}
20082047
}
20092048

2049+
Py_DECREF(visited);
20102050
return 0;
2051+
2052+
error:
2053+
Py_DECREF(visited);
2054+
return -1;
20112055
}
20122056

20132057
static int
@@ -3011,7 +3055,7 @@ append_awaited_by(
30113055
return -1;
30123056
}
30133057

3014-
return 0;
3058+
return process_task_waiters(unwinder, awaited_by_for_thread, true);
30153059
}
30163060

30173061
/* ============================================================================
@@ -3447,11 +3491,12 @@ _remote_debugging.RemoteUnwinder.get_all_awaited_by
34473491
34483492
Get all tasks and their awaited_by relationships from the remote process.
34493493
3450-
This provides a tree structure showing which tasks are waiting for other tasks.
3494+
This provides a tree structure showing which tasks and futures are waiting for
3495+
other tasks.
34513496
34523497
For each task, returns:
34533498
1. The call stack frames leading to where the task is currently executing
3454-
2. The name of the task
3499+
2. The name of the task, or a generated name for a future
34553500
3. A list of tasks that this task is waiting for, with their own frames/names/etc
34563501
34573502
Returns a list of [frames, task_name, subtasks] where:
@@ -3487,7 +3532,7 @@ Example output:
34873532

34883533
static PyObject *
34893534
_remote_debugging_RemoteUnwinder_get_all_awaited_by_impl(RemoteUnwinderObject *self)
3490-
/*[clinic end generated code: output=6a49cd345e8aec53 input=a452c652bb00701a]*/
3535+
/*[clinic end generated code: output=6a49cd345e8aec53 input=9125e1b63ae0c0a9]*/
34913536
{
34923537
if (!self->async_debug_offsets_available) {
34933538
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
35373582
35383583
This returns information about running tasks and all tasks that are waiting for them,
35393584
forming a complete dependency graph for each thread's active task.
3585+
Intermediate futures are included in each dependency graph.
35403586
35413587
For each thread with a running task, returns the running task plus all tasks that
35423588
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:
35483594
35493595
Each task info contains:
35503596
- Task ID (memory address)
3551-
- Task name
3597+
- Task name, or a generated name for a future
35523598
- Call stack frames: List of (func_name, filename, lineno)
35533599
- List of tasks waiting for this task (recursive structure)
35543600
@@ -3576,7 +3622,7 @@ Example output (similar structure to get_all_awaited_by but only for running tas
35763622

35773623
static PyObject *
35783624
_remote_debugging_RemoteUnwinder_get_async_stack_trace_impl(RemoteUnwinderObject *self)
3579-
/*[clinic end generated code: output=6433d52b55e87bbe input=8744b47c9ec2220a]*/
3625+
/*[clinic end generated code: output=6433d52b55e87bbe input=75424725716fbcda]*/
35803626
{
35813627
if (!self->async_debug_offsets_available) {
35823628
PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available");

0 commit comments

Comments
 (0)