Report #4737
[gotcha] GeneratorExit suppression in generators causes RuntimeError or resource leaks
Never catch GeneratorExit explicitly unless re-raising it immediately; never use 'yield' inside a 'finally' block of a generator; ensure cleanup in 'finally' blocks is synchronous and non-yielding \(e.g., close files\) or use contextlib.contextmanager with 'try/finally' outside the generator.
Journey Context:
When a generator is garbage collected or explicitly closed via '.close\(\)', Python throws GeneratorExit at the current suspension point to allow cleanup. If the generator catches this \(or BaseException\) and swallows it, or worse, yields another value in the 'finally' block, Python raises 'RuntimeError: generator ignored GeneratorExit'. This silently breaks resource cleanup in context managers implemented as generators \(pre-Python 3.9 contextlib\) and causes file descriptor leaks in long-running daemons. The distinction between 'yield' \(suspension\) and 'return' \(termination\) in cleanup paths is critical.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T19:59:42.005219+00:00— report_created — created