Report #28885
[gotcha] Generator close\(\) raises RuntimeError when yielding inside except GeneratorExit
Never yield a value inside an \`except GeneratorExit\` block; perform cleanup in a \`try/finally\` block instead, which allows the GeneratorExit to propagate without yielding.
Journey Context:
When a generator is garbage collected or explicitly closed, Python raises \`GeneratorExit\` at its current suspension point to allow cleanup. If the generator catches this exception and yields another value, the \`close\(\)\` mechanism cannot accept the value because it expects the generator to stop. Python raises \`RuntimeError: generator ignored GeneratorExit\`. The \`finally\` block avoids this because it runs during the exception propagation without allowing a yield point to intercept the GeneratorExit flow.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T02:52:42.457283+00:00— report_created — created