@@ -1123,11 +1123,6 @@ subtype_dealloc(PyObject *self)
11231123 Py_TRASHCAN_SAFE_BEGIN (self );
11241124 -- _PyTrash_delete_nesting ;
11251125 -- tstate -> trash_delete_nesting ;
1126- /* DO NOT restore GC tracking at this point. weakref callbacks
1127- * (if any, and whether directly here or indirectly in something we
1128- * call) may trigger GC, and if self is tracked at that point, it
1129- * will look like trash to GC and GC will try to delete self again.
1130- */
11311126
11321127 /* Find the nearest base with a different tp_dealloc */
11331128 base = type ;
@@ -1138,30 +1133,36 @@ subtype_dealloc(PyObject *self)
11381133
11391134 has_finalizer = type -> tp_finalize || type -> tp_del ;
11401135
1141- /* Maybe call finalizer; exit early if resurrected */
1142- if (has_finalizer )
1143- _PyObject_GC_TRACK (self );
1144-
11451136 if (type -> tp_finalize ) {
1137+ _PyObject_GC_TRACK (self );
11461138 if (PyObject_CallFinalizerFromDealloc (self ) < 0 ) {
11471139 /* Resurrected */
11481140 goto endlabel ;
11491141 }
1142+ _PyObject_GC_UNTRACK (self );
11501143 }
1151- /* If we added a weaklist, we clear it. Do this *before* calling
1152- tp_del, clearing slots, or clearing the instance dict. */
1144+ /*
1145+ If we added a weaklist, we clear it. Do this *before* calling tp_del,
1146+ clearing slots, or clearing the instance dict.
1147+
1148+ GC tracking must be off at this point. weakref callbacks (if any, and
1149+ whether directly here or indirectly in something we call) may trigger GC,
1150+ and if self is tracked at that point, it will look like trash to GC and GC
1151+ will try to delete self again.
1152+ */
11531153 if (type -> tp_weaklistoffset && !base -> tp_weaklistoffset )
11541154 PyObject_ClearWeakRefs (self );
11551155
11561156 if (type -> tp_del ) {
1157+ _PyObject_GC_TRACK (self );
11571158 type -> tp_del (self );
11581159 if (self -> ob_refcnt > 0 ) {
11591160 /* Resurrected */
11601161 goto endlabel ;
11611162 }
1163+ _PyObject_GC_UNTRACK (self );
11621164 }
11631165 if (has_finalizer ) {
1164- _PyObject_GC_UNTRACK (self );
11651166 /* New weakrefs could be created during the finalizer call.
11661167 If this occurs, clear them out without calling their
11671168 finalizers since they might rely on part of the object
0 commit comments