@@ -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
610611static 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
793794static 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 );
796798static int parse_task_awaited_by (RemoteUnwinderObject * unwinder , uintptr_t task_address , PyObject * awaited_by );
797799static int find_running_task_in_thread (RemoteUnwinderObject * unwinder , uintptr_t thread_state_addr , uintptr_t * running_task_addr );
798800static 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(
19771990static int
19781991process_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
20132057static 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
34483492Get 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
34523497For each task, returns:
345334981. 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
345535003. A list of tasks that this task is waiting for, with their own frames/names/etc
34563501
34573502Returns a list of [frames, task_name, subtasks] where:
@@ -3487,7 +3532,7 @@ Example output:
34873532
34883533static 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
35383583This returns information about running tasks and all tasks that are waiting for them,
35393584forming a complete dependency graph for each thread's active task.
3585+ Intermediate futures are included in each dependency graph.
35403586
35413587For each thread with a running task, returns the running task plus all tasks that
35423588transitively 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
35493595Each 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
35773623static 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