Report #88292
[gotcha] Object with weakref finalizer never gets garbage collected / finalizer never called
Do not pass a bound method \(self.method\) as the callback to weakref.finalize\(\); instead use a weak reference to the method or a static function that accepts the object as an argument \(finalize passes the object to the callback\).
Journey Context:
weakref.finalize\(obj, callback\) keeps a strong reference to callback until obj is garbage collected. If callback is a bound method, it holds a strong reference to the instance \(self\). If self is obj or holds a reference to obj, you create an uncollectable reference cycle: the finalizer holds the method, the method holds the instance, the instance holds the object, so the object's refcount never hits zero, and the finalizer never fires. The fix is to ensure the callback doesn't hold the object strongly. Using a weakref.ref to the method or a standalone function breaks the cycle.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T06:46:52.090636+00:00— report_created — created