Skip to content

Commit 6a3fff7

Browse files
[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 6f049d3) Co-authored-by: Maurycy Pawłowski-Wieroński <maurycy@maurycy.com>
1 parent 2997820 commit 6a3fff7

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
@@ -872,11 +872,11 @@ append_awaited_by_for_thread(
872872
return 0;
873873
}
874874

875-
int
876-
append_awaited_by(
875+
static int
876+
append_awaited_info(
877877
RemoteUnwinderObject *unwinder,
878878
unsigned long tid,
879-
uintptr_t head_addr,
879+
PyObject *task_list,
880880
PyObject *result)
881881
{
882882
PyObject *tid_py = PyLong_FromUnsignedLong(tid);
@@ -885,33 +885,47 @@ append_awaited_by(
885885
return -1;
886886
}
887887

888-
PyObject* awaited_by_for_thread = PyList_New(0);
889-
if (awaited_by_for_thread == NULL) {
890-
Py_DECREF(tid_py);
891-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create awaited_by thread list");
892-
return -1;
893-
}
894-
895888
RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
896889
PyObject *result_item = PyStructSequence_New(state->AwaitedInfo_Type);
897890
if (result_item == NULL) {
898891
Py_DECREF(tid_py);
899-
Py_DECREF(awaited_by_for_thread);
900892
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create AwaitedInfo");
901893
return -1;
902894
}
903895

904-
PyStructSequence_SetItem(result_item, 0, tid_py); // steals ref
905-
PyStructSequence_SetItem(result_item, 1, awaited_by_for_thread); // steals ref
896+
PyStructSequence_SetItem(result_item, 0, tid_py);
897+
PyStructSequence_SetItem(result_item, 1, Py_NewRef(task_list));
906898
if (PyList_Append(result, result_item)) {
907899
Py_DECREF(result_item);
908900
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append awaited_by result item");
909901
return -1;
910902
}
911903
Py_DECREF(result_item);
912904

913-
if (append_awaited_by_for_thread(unwinder, head_addr, awaited_by_for_thread))
914-
{
905+
return 0;
906+
}
907+
908+
int
909+
append_awaited_by(
910+
RemoteUnwinderObject *unwinder,
911+
unsigned long tid,
912+
uintptr_t head_addr,
913+
PyObject *result)
914+
{
915+
PyObject *task_list = PyList_New(0);
916+
if (task_list == NULL) {
917+
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create awaited_by thread list");
918+
return -1;
919+
}
920+
921+
if (append_awaited_info(unwinder, tid, task_list, result) < 0) {
922+
Py_DECREF(task_list);
923+
return -1;
924+
}
925+
926+
int err = append_awaited_by_for_thread(unwinder, head_addr, task_list);
927+
Py_DECREF(task_list);
928+
if (err < 0) {
915929
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append awaited_by for thread");
916930
return -1;
917931
}
@@ -1016,32 +1030,9 @@ process_thread_for_async_stack_trace(
10161030
return -1;
10171031
}
10181032

1019-
// Create AwaitedInfo structure for this thread
1020-
PyObject *tid_py = PyLong_FromUnsignedLong(tid);
1021-
if (tid_py == NULL) {
1022-
Py_DECREF(task_list);
1023-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create thread ID");
1024-
return -1;
1025-
}
1026-
1027-
RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
1028-
PyObject *awaited_info = PyStructSequence_New(state->AwaitedInfo_Type);
1029-
if (awaited_info == NULL) {
1030-
Py_DECREF(tid_py);
1031-
Py_DECREF(task_list);
1032-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create AwaitedInfo");
1033-
return -1;
1034-
}
1035-
1036-
PyStructSequence_SetItem(awaited_info, 0, tid_py); // steals ref
1037-
PyStructSequence_SetItem(awaited_info, 1, task_list); // steals ref
1038-
1039-
if (PyList_Append(result, awaited_info)) {
1040-
Py_DECREF(awaited_info);
1041-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append AwaitedInfo to result");
1042-
return -1;
1043-
}
1044-
Py_DECREF(awaited_info);
1033+
int err = append_awaited_info(unwinder, tid, task_list, result);
1034+
Py_DECREF(task_list);
1035+
return err;
10451036
}
10461037

10471038
return 0;

0 commit comments

Comments
 (0)