diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 0a82f3a..325ec85 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1150,9 +1150,16 @@ subtype_dealloc(PyObject *self)
}
/* If we added a weaklist, we clear it. Do this *before* calling
tp_del, clearing slots, or clearing the instance dict. */
- if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
+ if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
+ if (has_finalizer)
+ _PyObject_GC_UNTRACK(self);
+
PyObject_ClearWeakRefs(self);
+ if (has_finalizer)
+ _PyObject_GC_TRACK(self);
+ }
+
if (type->tp_del) {
type->tp_del(self);
if (self->ob_refcnt > 0) {