Report #70645
[gotcha] Generator raises RuntimeError when catching GeneratorExit and yielding
Never yield from a finally block in a generator; if you must catch GeneratorExit for cleanup, re-raise it immediately without yielding.
Journey Context:
When a generator is closed \(via close\(\), context manager exit, or breaking out of a for-loop\), Python injects GeneratorExit at the current suspension point. If the generator catches this exception and yields another value—or swallows it without re-raising—CPython raises RuntimeError: 'generator ignored GeneratorExit'. This is an uncatchable crash. The common trap is putting resource cleanup in a try/except that catches Exception or BaseException and yields a sentinel value, or using yield in a finally block. The only safe pattern is to catch GeneratorExit specifically for non-yielding cleanup \(e.g., releasing locks\) and always re-raise it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T01:09:17.925521+00:00— report_created — created