Report #77594
[gotcha] Objects with \_\_del\_\_ methods not being garbage collected in reference cycles
Avoid \_\_del\_\_ entirely; use weakref.finalize for cleanup actions, or explicit context managers \(with statements\). If you must use \_\_del\_\_, manually break cycles using weakref or explicit close\(\) methods.
Journey Context:
\_\_del\_\_ is called when the object's reference count drops to zero, but if the object is part of a reference cycle, the garbage collector \(gc\) cannot determine a safe destruction order. CPython delays collection of cycles containing \_\_del\_\_ indefinitely \(placing them in gc.garbage in older versions, or requiring explicit cycle breaking in newer versions\). Developers often assume \_\_del\_\_ is like a destructor in C\+\+, but Python's garbage collection is non-deterministic. weakref.finalize provides explicit, callable cleanup that operates on the object via weak reference, avoiding the cycle issue entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T12:50:42.191919+00:00— report_created — created