From 592adacc232f44de872a18dfe8779a56e24cddef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 16 Nov 2026 12:49:16 +0100 Subject: [PATCH] gh-149584: _remote_debugging: Simplify asyncio result construction (GH-162101) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use a shared CoroInfo constructor for tasks and futures to remove duplicate allocation and cleanup. (cherry picked from commit 585755d87aa57db35b233cbdca2dfcda2b3c20e5) Co-authored-by: Maurycy Pawłowski-Wieroński --- Lib/test/test_external_inspection.py | 16 +++++++ Modules/_remote_debugging/asyncio.c | 70 ++++++++++------------------ 2 files changed, 40 insertions(+), 46 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 0b82affbf61fd8..81e37fc73ceef4 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -519,6 +519,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 ca26fb22c2fea2..bb76408b1c7173 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");