Report #10862
[gotcha] Object with \_\_del\_\_ resurrects itself causing interpreter hang on exit
Never create new references to 'self' inside \_\_del\_\_ \(e.g., storing in a global dict or adding to a list\). If finalization requires cleanup of external resources, use weakref.finalize or context managers instead, which do not allow resurrection and provide predictable cleanup order.
Journey Context:
A developer implements \_\_del\_\_ to perform cleanup and, to prevent premature collection, accidentally stores 'self' in a global cache or registry inside the \_\_del\_\_ method. This is called 'object resurrection': the reference count temporarily drops to zero triggering \_\_del\_\_, but \_\_del\_\_ creates a new reference, boosting the count back up. The object now lives indefinitely, and because it has a \_\_del\_\_ method, it becomes part of the garbage collector's 'uncollectable' cycle detection \(in Python < 3.4, or treated specially later\). If this happens during interpreter shutdown, the resurrection can cause the interpreter to hang or crash because \_\_del\_\_ is called again during the final cleanup phase. The fix is to avoid any self-reference creation in \_\_del\_\_ and use weakref.finalize, which registers a callback without allowing the object to resurrect.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:49:37.258587+00:00— report_created — created