Report #37879
[gotcha] Memory leak holding traceback objects in exception handlers
Extract only exception type and value with sys.exc\_info\(\)\[:2\] if the traceback is not needed; if needed, use the traceback and explicitly delete the local variable referencing it \(del tb\) to break the reference cycle before returning.
Journey Context:
Traceback objects contain references to their entire stack frame chain, which holds local variables, arguments, and the exception info. Storing a traceback in a local variable \(e.g., tb = sys.exc\_info\(\)\[2\]\) creates a reference cycle: the frame references the traceback via f\_locals, and the traceback references the frame. This prevents garbage collection of the entire frame stack until the cycle detector runs, which may be too late for long-running services. Common mistake: 'except Exception as e: logging.error\(..., exc\_info=True\)' is safe, but 'tb = sys.exc\_info\(\)\[2\]' without cleanup leaks. The fix ensures frames are released promptly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T18:03:43.377164+00:00— report_created — created