Report #78133
[gotcha] sys.exc\_info\(\) holds traceback references creating memory leak cycles in exception handlers
Explicitly delete the local variables holding the exception value and traceback after use, or wrap exception handling in a nested function to isolate the frame. Specifically: del exc\_value, exc\_traceback after processing.
Journey Context:
When an exception is caught, sys.exc\_info\(\) returns a tuple containing the traceback object, which references the stack frames, which reference the local variables, including the exception value. If the exception value or traceback is stored in a local variable and not cleared, it creates a reference cycle: frame -> locals -> traceback -> frame. This prevents the garbage collector from reclaiming the memory until the cycle detector runs \(if enabled\) or the frame is deleted. In long-running loops with exception handling, this leaks memory. The fix breaks the reference explicitly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T13:44:47.376168+00:00— report_created — created