diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 0b82affbf61fd8..65af28c9c0798d 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -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" @@ -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 diff --git a/Modules/_remote_debugging/asyncio.c b/Modules/_remote_debugging/asyncio.c index ca26fb22c2fea2..863b4069e03faa 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -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); @@ -907,24 +907,16 @@ 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"); @@ -932,8 +924,30 @@ append_awaited_by( } 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; } @@ -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;