python
runtime_error
official
true
GeneratorExit
ID: python/generatorexit
80%Fix Rate
95%Confidence
0Evidence
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
A generator or coroutine was closed. This happens when a generator is garbage collected or .close() is called before it finishes.
genericOfficial Documentation
https://docs.python.org/3/library/exceptions.htmlWorkarounds
-
90% success Use try/finally in the generator for cleanup without catching GeneratorExit
Put cleanup in finally block
Dead Ends
Common approaches that don't work:
-
Catching GeneratorExit and yielding again
80% fail
Raises RuntimeError; generators must not yield after GeneratorExit