Report #11047
[gotcha] Exception raised in \_\_del\_\_ is silently printed to stderr instead of propagating
Never raise exceptions in \_\_del\_\_; use weakref.finalize for cleanup with error handling or explicitly log critical errors before cleanup.
Journey Context:
Python's garbage collector calls \_\_del\_\_ during destruction; because the object is already being destroyed, there is no sensible way to propagate an exception to the caller. Python prints the traceback to stderr and continues, which silently masks resource cleanup failures \(e.g., database rollback errors\). Developers often assume \_\_del\_\_ is a reliable destructor like in C\+\+, but it is not—it's non-deterministic and exceptions are lost. The correct pattern is to avoid \_\_del\_\_ entirely for critical cleanup; use context managers for synchronous code, and weakref.finalize if you must trigger cleanup when an object dies, because finalize callbacks can log or handle errors properly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:20:49.131085+00:00— report_created — created