Report #57270
[gotcha] RuntimeError: generator ignored GeneratorExit when closing a generator that yields from a subgenerator
Ensure subgenerators \(the target of yield from\) do not catch GeneratorExit and yield a value. If cleanup is needed, catch GeneratorExit, perform cleanup without yielding, then re-raise or allow propagation. Use contextlib.closing or async cleanup patterns for complex resource management.
Journey Context:
When generator.close\(\) is called \(or when a generator is garbage collected\), Python raises GeneratorExit at the current yield point. If the generator uses yield from to delegate to a subgenerator, the GeneratorExit is propagated to the subgenerator. If the subgenerator catches GeneratorExit and yields a value in response, the delegating generator cannot accept the value \(it is closing\) and raises RuntimeError. Developers often write cleanup code in subgenerators that catches all exceptions including GeneratorExit and yields a final status, which crashes the parent. The correct pattern is to catch GeneratorExit only for resource cleanup without yielding, then allow the exception to propagate.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T02:36:53.508232+00:00— report_created — created