https://github.com/python/cpython/commit/585755d87aa57db35b233cbdca2dfcda2b3c20e5
commit: 585755d87aa57db35b233cbdca2dfcda2b3c20e5
branch: main
author: Maurycy Pawłowski-Wieroński <[email protected]>
committer: pablogsal <[email protected]>
date: 2026-11-16T11:49:16Z
summary:

gh-149584: _remote_debugging: Simplify asyncio result construction (GH-162101)

Use a shared CoroInfo constructor for tasks and futures to remove duplicate 
allocation and cleanup.

files:
M Lib/test/test_external_inspection.py
M Modules/_remote_debugging/asyncio.c

diff --git a/Lib/test/test_external_inspection.py 
b/Lib/test/test_external_inspection.py
index 104efaebd85c16f..edcac9d141ff5aa 100644
--- a/Lib/test/test_external_inspection.py
+++ b/Lib/test/test_external_inspection.py
@@ -520,6 +520,22 @@ async def rec(n):
 
         self.assertEqual(asyncio.run(rec(3)), ["rec"] * 4)
 
+    @skip_if_not_supported
+    def test_future_waiter_has_empty_stack(self):
+        async def main():
+            task = asyncio.current_task()
+            future = asyncio.get_running_loop().create_future()
+            asyncio.future_add_to_awaited_by(task, future)
+            try:
+                trace = RemoteUnwinder(os.getpid()).get_all_awaited_by()
+                task_info = self._get_task_id_map(trace)[id(task)]
+                self.assertEqual(task_info.awaited_by, [([], id(future))])
+                self.assertTrue(task_info.coroutine_stack[0].call_stack)
+            finally:
+                asyncio.future_discard_from_awaited_by(task, future)
+
+        asyncio.run(main())
+
     @skip_if_not_supported
     def test_async_generator_stack_is_not_truncated(self):
         # gh-159620
diff --git a/Modules/_remote_debugging/asyncio.c 
b/Modules/_remote_debugging/asyncio.c
index 11fa40eb3e5f00c..f9fcea54cacefea 100644
--- a/Modules/_remote_debugging/asyncio.c
+++ b/Modules/_remote_debugging/asyncio.c
@@ -422,13 +422,12 @@ parse_coro_chain(
 static PyObject*
 create_task_result(
     RemoteUnwinderObject *unwinder,
-    uintptr_t task_address
+    uintptr_t task_address,
+    char is_task
 ) {
     PyObject* result = NULL;
     PyObject *call_stack = NULL;
     PyObject *tn = NULL;
-    char task_obj[SIZEOF_TASK_OBJ];
-    uintptr_t coro_addr;
 
     // Create call_stack first since it's the first tuple element
     call_stack = PyList_New(0);
@@ -444,25 +443,27 @@ create_task_result(
         goto error;
     }
 
-    // Parse coroutine chain
-    if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address,
-                                              
(size_t)unwinder->async_debug_offsets.asyncio_task_object.size,
-                                              task_obj) < 0) {
-        set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task 
object for coro chain");
-        goto error;
-    }
-
-    coro_addr = GET_MEMBER_NO_TAG(uintptr_t, task_obj, 
unwinder->async_debug_offsets.asyncio_task_object.task_coro);
-
-    if ((void*)coro_addr != NULL) {
-        if (parse_coro_chain(unwinder, coro_addr, call_stack) < 0) {
-            set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse 
coroutine chain");
+    if (is_task) {
+        char task_obj[SIZEOF_TASK_OBJ];
+        if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, 
task_address,
+                                                  
(size_t)unwinder->async_debug_offsets.asyncio_task_object.size,
+                                                  task_obj) < 0) {
+            set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read 
task object for coro chain");
             goto error;
         }
 
-        if (PyList_Reverse(call_stack)) {
-            set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to 
reverse call stack");
-            goto error;
+        uintptr_t coro_addr = GET_MEMBER_NO_TAG(uintptr_t, task_obj, 
unwinder->async_debug_offsets.asyncio_task_object.task_coro);
+
+        if ((void*)coro_addr != NULL) {
+            if (parse_coro_chain(unwinder, coro_addr, call_stack) < 0) {
+                set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to 
parse coroutine chain");
+                goto error;
+            }
+
+            if (PyList_Reverse(call_stack)) {
+                set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to 
reverse call stack");
+                goto error;
+            }
         }
     }
 
@@ -506,33 +507,10 @@ parse_task(
         goto error;
     }
 
-    if (is_task) {
-        result = create_task_result(unwinder, task_address);
-        if (!result) {
-            set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to 
create task result");
-            goto error;
-        }
-    } else {
-        // Create an empty CoroInfo for non-task objects
-        RemoteDebuggingState *state = 
RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
-        result = PyStructSequence_New(state->CoroInfo_Type);
-        if (result == NULL) {
-            set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create 
empty CoroInfo");
-            goto error;
-        }
-        PyObject *empty_list = PyList_New(0);
-        if (empty_list == NULL) {
-            set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create 
empty list");
-            goto error;
-        }
-        PyObject *task_name = PyLong_FromUnsignedLongLong(task_address);
-        if (task_name == NULL) {
-            Py_DECREF(empty_list);
-            set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to 
create task name");
-            goto error;
-        }
-        PyStructSequence_SetItem(result, 0, empty_list);  // This steals the 
reference
-        PyStructSequence_SetItem(result, 1, task_name);  // This steals the 
reference
+    result = create_task_result(unwinder, task_address, is_task);
+    if (result == NULL) {
+        set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create 
task result");
+        goto error;
     }
     if (PyList_Append(render_to, result)) {
         set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append 
task result to render list");

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to