Report #79721
[gotcha] Memory leak: storing sys.exc\_info\(\) or tracebacks prevents local variable garbage collection
Explicitly delete the traceback reference or use traceback.clear\_frames\(\) after handling the exception; do not store exc\_info tuples long-term.
Journey Context:
When an exception is caught, the traceback object holds references to every stack frame, which in turn holds references to all local variables in those frames. Even if you delete the exception object itself, the traceback keeps the entire call stack alive until the traceback is garbage collected. This creates a reference cycle that can cause significant memory leaks in long-running services \(e.g., web servers\) that store exception details for logging. The standard fix used by frameworks like Werkzeug is to call traceback.clear\_frames\(\) or simply \`del tb\` to break the reference chain immediately after processing the exception.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T16:24:36.523440+00:00— report_created — created