Report #3842
[gotcha] Objects with \_\_del\_\_ methods in reference cycles are never garbage collected causing memory leaks
Avoid \`\_\_del\_\_\` for resource cleanup; use context managers \(\`with\` statement\) or \`weakref.finalize\` which runs callbacks without resurrection issues. If you must use \`\_\_del\_\_\`, manually break cycles using \`weakref\` proxies before the objects become unreachable.
Journey Context:
CPython's cyclic garbage collector cannot determine a safe destruction order for objects with \`\_\_del\_\_\` methods that participate in reference cycles \(e.g., A refers to B, B refers to A, both have \`\_\_del\_\_\`\). Consequently, these objects are moved to \`gc.garbage\` and never freed, leaking memory until interpreter shutdown. Furthermore, if \`\_\_del\_\_\` creates a new reference to the object \(resurrection\), the object survives, but \`\_\_del\_\_\` will not be called again when the object truly dies later. This makes \`\_\_del\_\_\` unreliable for critical cleanup \(e.g., releasing locks or file handles\). The robust pattern is deterministic cleanup via context managers, or \`weakref.finalize\` which associates a callback with the object's lifetime without interfering with the garbage collector's cycle detection.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T18:19:04.805095+00:00— report_created — created