Skip to content

Commit cc2e923

Browse files
maurycymiss-islington
authored andcommitted
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 6f049d3) Co-authored-by: Maurycy Pawłowski-Wieroński <maurycy@maurycy.com>
1 parent e970097 commit cc2e923

2 files changed

Lines changed: 50 additions & 42 deletions

File tree

‎Lib/test/test_external_inspection.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
try:
5555
from _remote_debugging import PROCESS_VM_READV_SUPPORTED
5656
from _remote_debugging import RemoteUnwinder
57-
from _remote_debugging import FrameInfo, CoroInfo, TaskInfo
57+
from _remote_debugging import FrameInfo, CoroInfo, TaskInfo, AwaitedInfo
5858
except ImportError:
5959
raise unittest.SkipTest(
6060
"Test only runs when _remote_debugging is available"
@@ -486,6 +486,23 @@ def _extract_coroutine_funcnames(self, trace):
486486
for task in info.awaited_by
487487
}
488488

489+
@skip_if_not_supported
490+
def test_async_awaited_info(self):
491+
async def main():
492+
task = asyncio.current_task()
493+
unwinder = RemoteUnwinder(os.getpid())
494+
for method in (unwinder.get_async_stack_trace,
495+
unwinder.get_all_awaited_by):
496+
with self.subTest(method=method.__name__):
497+
info, = (info for info in method()
498+
if info.thread_id == threading.get_native_id())
499+
self.assertIsInstance(info, AwaitedInfo)
500+
task_info, = info.awaited_by
501+
self.assertEqual(task_info.task_id, id(task))
502+
self.assertEqual(task_info.task_name, task.get_name())
503+
504+
asyncio.run(main())
505+
489506
@skip_if_not_supported
490507
def test_long_task_name_is_truncated(self):
491508
# gh-157788

‎Modules/_remote_debugging/asyncio.c‎

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -894,11 +894,11 @@ append_awaited_by_for_thread(
894894
return 0;
895895
}
896896

897-
int
898-
append_awaited_by(
897+
static int
898+
append_awaited_info(
899899
RemoteUnwinderObject *unwinder,
900900
unsigned long tid,
901-
uintptr_t head_addr,
901+
PyObject *task_list,
902902
PyObject *result)
903903
{
904904
PyObject *tid_py = PyLong_FromUnsignedLong(tid);
@@ -907,33 +907,47 @@ append_awaited_by(
907907
return -1;
908908
}
909909

910-
PyObject* awaited_by_for_thread = PyList_New(0);
911-
if (awaited_by_for_thread == NULL) {
912-
Py_DECREF(tid_py);
913-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create awaited_by thread list");
914-
return -1;
915-
}
916-
917910
RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
918911
PyObject *result_item = PyStructSequence_New(state->AwaitedInfo_Type);
919912
if (result_item == NULL) {
920913
Py_DECREF(tid_py);
921-
Py_DECREF(awaited_by_for_thread);
922914
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create AwaitedInfo");
923915
return -1;
924916
}
925917

926-
PyStructSequence_SetItem(result_item, 0, tid_py); // steals ref
927-
PyStructSequence_SetItem(result_item, 1, awaited_by_for_thread); // steals ref
918+
PyStructSequence_SetItem(result_item, 0, tid_py);
919+
PyStructSequence_SetItem(result_item, 1, Py_NewRef(task_list));
928920
if (PyList_Append(result, result_item)) {
929921
Py_DECREF(result_item);
930922
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append awaited_by result item");
931923
return -1;
932924
}
933925
Py_DECREF(result_item);
934926

935-
if (append_awaited_by_for_thread(unwinder, head_addr, awaited_by_for_thread))
936-
{
927+
return 0;
928+
}
929+
930+
int
931+
append_awaited_by(
932+
RemoteUnwinderObject *unwinder,
933+
unsigned long tid,
934+
uintptr_t head_addr,
935+
PyObject *result)
936+
{
937+
PyObject *task_list = PyList_New(0);
938+
if (task_list == NULL) {
939+
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create awaited_by thread list");
940+
return -1;
941+
}
942+
943+
if (append_awaited_info(unwinder, tid, task_list, result) < 0) {
944+
Py_DECREF(task_list);
945+
return -1;
946+
}
947+
948+
int err = append_awaited_by_for_thread(unwinder, head_addr, task_list);
949+
Py_DECREF(task_list);
950+
if (err < 0) {
937951
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append awaited_by for thread");
938952
return -1;
939953
}
@@ -1038,32 +1052,9 @@ process_thread_for_async_stack_trace(
10381052
return -1;
10391053
}
10401054

1041-
// Create AwaitedInfo structure for this thread
1042-
PyObject *tid_py = PyLong_FromUnsignedLong(tid);
1043-
if (tid_py == NULL) {
1044-
Py_DECREF(task_list);
1045-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create thread ID");
1046-
return -1;
1047-
}
1048-
1049-
RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
1050-
PyObject *awaited_info = PyStructSequence_New(state->AwaitedInfo_Type);
1051-
if (awaited_info == NULL) {
1052-
Py_DECREF(tid_py);
1053-
Py_DECREF(task_list);
1054-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create AwaitedInfo");
1055-
return -1;
1056-
}
1057-
1058-
PyStructSequence_SetItem(awaited_info, 0, tid_py); // steals ref
1059-
PyStructSequence_SetItem(awaited_info, 1, task_list); // steals ref
1060-
1061-
if (PyList_Append(result, awaited_info)) {
1062-
Py_DECREF(awaited_info);
1063-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append AwaitedInfo to result");
1064-
return -1;
1065-
}
1066-
Py_DECREF(awaited_info);
1055+
int err = append_awaited_info(unwinder, tid, task_list, result);
1056+
Py_DECREF(task_list);
1057+
return err;
10671058
}
10681059

10691060
return 0;

0 commit comments

Comments
 (0)