python runtime_error official true

GeneratorExit

ID: python/generatorexit

Also available as: JSON · Markdown
80%Fix Rate
95%Confidence
0Evidence

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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.

generic

Official Documentation

https://docs.python.org/3/library/exceptions.html

Workarounds

  1. 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:

  1. Catching GeneratorExit and yielding again 80% fail

    Raises RuntimeError; generators must not yield after GeneratorExit