Skip to content

Commit 585755d

Browse files
authored
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.
1 parent 91ab655 commit 585755d

2 files changed

Lines changed: 40 additions & 46 deletions

File tree

‎Lib/test/test_external_inspection.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,22 @@ async def rec(n):
520520

521521
self.assertEqual(asyncio.run(rec(3)), ["rec"] * 4)
522522

523+
@skip_if_not_supported
524+
def test_future_waiter_has_empty_stack(self):
525+
async def main():
526+
task = asyncio.current_task()
527+
future = asyncio.get_running_loop().create_future()
528+
asyncio.future_add_to_awaited_by(task, future)
529+
try:
530+
trace = RemoteUnwinder(os.getpid()).get_all_awaited_by()
531+
task_info = self._get_task_id_map(trace)[id(task)]
532+
self.assertEqual(task_info.awaited_by, [([], id(future))])
533+
self.assertTrue(task_info.coroutine_stack[0].call_stack)
534+
finally:
535+
asyncio.future_discard_from_awaited_by(task, future)
536+
537+
asyncio.run(main())
538+
523539
@skip_if_not_supported
524540
def test_async_generator_stack_is_not_truncated(self):
525541
# gh-159620

‎Modules/_remote_debugging/asyncio.c‎

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,12 @@ parse_coro_chain(
422422
static PyObject*
423423
create_task_result(
424424
RemoteUnwinderObject *unwinder,
425-
uintptr_t task_address
425+
uintptr_t task_address,
426+
char is_task
426427
) {
427428
PyObject* result = NULL;
428429
PyObject *call_stack = NULL;
429430
PyObject *tn = NULL;
430-
char task_obj[SIZEOF_TASK_OBJ];
431-
uintptr_t coro_addr;
432431

433432
// Create call_stack first since it's the first tuple element
434433
call_stack = PyList_New(0);
@@ -444,25 +443,27 @@ create_task_result(
444443
goto error;
445444
}
446445

447-
// Parse coroutine chain
448-
if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address,
449-
(size_t)unwinder->async_debug_offsets.asyncio_task_object.size,
450-
task_obj) < 0) {
451-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task object for coro chain");
452-
goto error;
453-
}
454-
455-
coro_addr = GET_MEMBER_NO_TAG(uintptr_t, task_obj, unwinder->async_debug_offsets.asyncio_task_object.task_coro);
456-
457-
if ((void*)coro_addr != NULL) {
458-
if (parse_coro_chain(unwinder, coro_addr, call_stack) < 0) {
459-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse coroutine chain");
446+
if (is_task) {
447+
char task_obj[SIZEOF_TASK_OBJ];
448+
if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address,
449+
(size_t)unwinder->async_debug_offsets.asyncio_task_object.size,
450+
task_obj) < 0) {
451+
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task object for coro chain");
460452
goto error;
461453
}
462454

463-
if (PyList_Reverse(call_stack)) {
464-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to reverse call stack");
465-
goto error;
455+
uintptr_t coro_addr = GET_MEMBER_NO_TAG(uintptr_t, task_obj, unwinder->async_debug_offsets.asyncio_task_object.task_coro);
456+
457+
if ((void*)coro_addr != NULL) {
458+
if (parse_coro_chain(unwinder, coro_addr, call_stack) < 0) {
459+
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse coroutine chain");
460+
goto error;
461+
}
462+
463+
if (PyList_Reverse(call_stack)) {
464+
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to reverse call stack");
465+
goto error;
466+
}
466467
}
467468
}
468469

@@ -506,33 +507,10 @@ parse_task(
506507
goto error;
507508
}
508509

509-
if (is_task) {
510-
result = create_task_result(unwinder, task_address);
511-
if (!result) {
512-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create task result");
513-
goto error;
514-
}
515-
} else {
516-
// Create an empty CoroInfo for non-task objects
517-
RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
518-
result = PyStructSequence_New(state->CoroInfo_Type);
519-
if (result == NULL) {
520-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create empty CoroInfo");
521-
goto error;
522-
}
523-
PyObject *empty_list = PyList_New(0);
524-
if (empty_list == NULL) {
525-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create empty list");
526-
goto error;
527-
}
528-
PyObject *task_name = PyLong_FromUnsignedLongLong(task_address);
529-
if (task_name == NULL) {
530-
Py_DECREF(empty_list);
531-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create task name");
532-
goto error;
533-
}
534-
PyStructSequence_SetItem(result, 0, empty_list); // This steals the reference
535-
PyStructSequence_SetItem(result, 1, task_name); // This steals the reference
510+
result = create_task_result(unwinder, task_address, is_task);
511+
if (result == NULL) {
512+
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create task result");
513+
goto error;
536514
}
537515
if (PyList_Append(render_to, result)) {
538516
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append task result to render list");

0 commit comments

Comments
 (0)