https://github.com/python/cpython/commit/6a3fff7bae07e7cc9e970aa1b96c80eb3a00efe7
commit: 6a3fff7bae07e7cc9e970aa1b96c80eb3a00efe7
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: pablogsal <[email protected]>
date: 2026-11-16T12:45:53Z
summary:

[3.15] gh-149584: _remote_debugging: Deduplicate AwaitedInfo construction 
(GH-162104)

Use one helper to construct and append AwaitedInfo results for both asyncio 
APIs. Keep reference ownership and cleanup in the shared helper.
(cherry picked from commit 6f049d37d9b655c14f183784d8a6ddd9eb9dd147)

Co-authored-by: Maurycy Pawłowski-Wieroński <[email protected]>

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 81e37fc73ceef4..b3b24fdc1183af 100644
--- a/Lib/test/test_external_inspection.py
+++ b/Lib/test/test_external_inspection.py
@@ -54,7 +54,7 @@
 try:
     from _remote_debugging import PROCESS_VM_READV_SUPPORTED
     from _remote_debugging import RemoteUnwinder
-    from _remote_debugging import FrameInfo, CoroInfo, TaskInfo
+    from _remote_debugging import FrameInfo, CoroInfo, TaskInfo, AwaitedInfo
 except ImportError:
     raise unittest.SkipTest(
         "Test only runs when _remote_debugging is available"
@@ -486,6 +486,23 @@ def _extract_coroutine_funcnames(self, trace):
             for task in info.awaited_by
         }
 
+    @skip_if_not_supported
+    def test_async_awaited_info(self):
+        async def main():
+            task = asyncio.current_task()
+            unwinder = RemoteUnwinder(os.getpid())
+            for method in (unwinder.get_async_stack_trace,
+                           unwinder.get_all_awaited_by):
+                with self.subTest(method=method.__name__):
+                    info, = (info for info in method()
+                             if info.thread_id == threading.get_native_id())
+                    self.assertIsInstance(info, AwaitedInfo)
+                    task_info, = info.awaited_by
+                    self.assertEqual(task_info.task_id, id(task))
+                    self.assertEqual(task_info.task_name, task.get_name())
+
+        asyncio.run(main())
+
     @skip_if_not_supported
     def test_long_task_name_is_truncated(self):
         # gh-157788
diff --git a/Modules/_remote_debugging/asyncio.c 
b/Modules/_remote_debugging/asyncio.c
index bb76408b1c7173..fc94bfe719d196 100644
--- a/Modules/_remote_debugging/asyncio.c
+++ b/Modules/_remote_debugging/asyncio.c
@@ -872,11 +872,11 @@ append_awaited_by_for_thread(
     return 0;
 }
 
-int
-append_awaited_by(
+static int
+append_awaited_info(
     RemoteUnwinderObject *unwinder,
     unsigned long tid,
-    uintptr_t head_addr,
+    PyObject *task_list,
     PyObject *result)
 {
     PyObject *tid_py = PyLong_FromUnsignedLong(tid);
@@ -885,24 +885,16 @@ append_awaited_by(
         return -1;
     }
 
-    PyObject* awaited_by_for_thread = PyList_New(0);
-    if (awaited_by_for_thread == NULL) {
-        Py_DECREF(tid_py);
-        set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create 
awaited_by thread list");
-        return -1;
-    }
-
     RemoteDebuggingState *state = 
RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
     PyObject *result_item = PyStructSequence_New(state->AwaitedInfo_Type);
     if (result_item == NULL) {
         Py_DECREF(tid_py);
-        Py_DECREF(awaited_by_for_thread);
         set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create 
AwaitedInfo");
         return -1;
     }
 
-    PyStructSequence_SetItem(result_item, 0, tid_py);  // steals ref
-    PyStructSequence_SetItem(result_item, 1, awaited_by_for_thread);  // 
steals ref
+    PyStructSequence_SetItem(result_item, 0, tid_py);
+    PyStructSequence_SetItem(result_item, 1, Py_NewRef(task_list));
     if (PyList_Append(result, result_item)) {
         Py_DECREF(result_item);
         set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append 
awaited_by result item");
@@ -910,8 +902,30 @@ append_awaited_by(
     }
     Py_DECREF(result_item);
 
-    if (append_awaited_by_for_thread(unwinder, head_addr, 
awaited_by_for_thread))
-    {
+    return 0;
+}
+
+int
+append_awaited_by(
+    RemoteUnwinderObject *unwinder,
+    unsigned long tid,
+    uintptr_t head_addr,
+    PyObject *result)
+{
+    PyObject *task_list = PyList_New(0);
+    if (task_list == NULL) {
+        set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create 
awaited_by thread list");
+        return -1;
+    }
+
+    if (append_awaited_info(unwinder, tid, task_list, result) < 0) {
+        Py_DECREF(task_list);
+        return -1;
+    }
+
+    int err = append_awaited_by_for_thread(unwinder, head_addr, task_list);
+    Py_DECREF(task_list);
+    if (err < 0) {
         set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append 
awaited_by for thread");
         return -1;
     }
@@ -1016,32 +1030,9 @@ process_thread_for_async_stack_trace(
             return -1;
         }
 
-        // Create AwaitedInfo structure for this thread
-        PyObject *tid_py = PyLong_FromUnsignedLong(tid);
-        if (tid_py == NULL) {
-            Py_DECREF(task_list);
-            set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to 
create thread ID");
-            return -1;
-        }
-
-        RemoteDebuggingState *state = 
RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
-        PyObject *awaited_info = PyStructSequence_New(state->AwaitedInfo_Type);
-        if (awaited_info == NULL) {
-            Py_DECREF(tid_py);
-            Py_DECREF(task_list);
-            set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create 
AwaitedInfo");
-            return -1;
-        }
-
-        PyStructSequence_SetItem(awaited_info, 0, tid_py);  // steals ref
-        PyStructSequence_SetItem(awaited_info, 1, task_list);  // steals ref
-
-        if (PyList_Append(result, awaited_info)) {
-            Py_DECREF(awaited_info);
-            set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to 
append AwaitedInfo to result");
-            return -1;
-        }
-        Py_DECREF(awaited_info);
+        int err = append_awaited_info(unwinder, tid, task_list, result);
+        Py_DECREF(task_list);
+        return err;
     }
 
     return 0;

_______________________________________________
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