Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Lib/test/test_external_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
73 changes: 32 additions & 41 deletions Modules/_remote_debugging/asyncio.c
Original file line number Diff line number Diff line change
Expand Up @@ -894,11 +894,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);
Expand All @@ -907,33 +907,47 @@ 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");
return -1;
}
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;
}
Expand Down Expand Up @@ -1038,32 +1052,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;
Expand Down
Loading