@@ -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
926918 PyStructSequence_SetItem (result_item , 0 , tid_py ); // steals ref
927- PyStructSequence_SetItem (result_item , 1 , awaited_by_for_thread ); // steals ref
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