@@ -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