Report #93485
[gotcha] Suppressing GeneratorExit or yielding in finally block causes RuntimeError
Never catch GeneratorExit and continue \(silently suppress\), never yield inside a finally block of a generator, and never catch BaseException without re-raising GeneratorExit in a generator. Always allow GeneratorExit to propagate or exit the generator immediately.
Journey Context:
When a generator is garbage collected or explicitly closed via \`.close\(\)\`, Python raises \`GeneratorExit\` at the current yield point. If the generator catches this exception and yields again \(or doesn't exit\), Python raises \`RuntimeError: generator ignored GeneratorExit\`. Similarly, if a \`finally\` block in the generator contains a \`yield\`, and the generator is being closed, the behavior is undefined or raises RuntimeError. This is critical for resource management patterns where generators are used as context managers or for streaming data; improper cleanup can crash the interpreter or leak resources. The only safe action upon receiving GeneratorExit is to clean up non-yielding resources and return/raise.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T15:30:06.035436+00:00— report_created — created