GeneratorExit
Python Programming Language
Severity: MinorWhat Does This Error Mean?
GeneratorExit is raised inside a generator function when it is closed with .close(). A generator is a special function that produces values one at a time using the 'yield' keyword. When someone calls .close() on a generator, Python sends GeneratorExit into it so the generator can clean up before stopping. This is normal Python behavior — not a bug.
Affected Models
- Python 2.x
- Python 3.x
- All Python versions
Common Causes
- The generator's .close() method was called explicitly to stop it early
- The for loop that was consuming the generator finished or used a 'break' statement
- The generator object was garbage collected — Python automatically closes it
- A with statement using a context manager triggered cleanup of the generator
- Code inside the generator's finally block re-raised or mishandled GeneratorExit
How to Fix It
-
If you see GeneratorExit in a traceback, first check whether it is actually causing a problem or just appearing in a log. Most of the time it is normal cleanup.
GeneratorExit appearing in a traceback usually means it was raised but not re-raised correctly, or a finally block raised a different exception that masked it.
-
If your generator has a try/except block, do NOT catch GeneratorExit with a broad 'except Exception' — that will swallow it. Use 'except GeneratorExit' if you need to catch it specifically.
Remember: GeneratorExit inherits from BaseException, not Exception, so 'except Exception' will not catch it anyway — but be aware of this distinction.
-
If your generator has a finally block for cleanup, that cleanup code will run when GeneratorExit is raised. Make sure your finally block does not raise a new exception.
If a finally block raises a RuntimeError or another exception, it will hide the original GeneratorExit and produce a confusing traceback.
-
If you want to do cleanup when a generator is closed, put the cleanup code in a try/finally block inside your generator function.
Example: try: while True: yield value finally: close_database_connection()
-
If you need to handle GeneratorExit specifically, you can catch it, do cleanup, and then re-raise it or just return. Do not try to yield another value after catching GeneratorExit — that raises a RuntimeError.
Example: except GeneratorExit: do_cleanup() return — do NOT use 'yield' after catching GeneratorExit.
When to Call a Professional
GeneratorExit is almost always expected behavior and not a real problem. You only need to act if your generator crashes with an error inside a finally block or try/except that mishandles GeneratorExit. This is an advanced Python topic — if you are just starting out, focus on simpler error types first.
Frequently Asked Questions
What is a generator in Python?
A generator is a function that uses the 'yield' keyword to produce values one at a time instead of returning all values at once. For example, a function that reads a huge file line by line without loading the whole file into memory. Generators are memory-efficient and very useful for large data sets.
Is GeneratorExit an error I need to fix?
Usually not. GeneratorExit is part of Python's normal cleanup process for generators. You only need to worry if it is causing your program to crash or behave unexpectedly. If you just see it in a log without any actual problem, you can safely ignore it.
What happens if I yield inside a GeneratorExit handler?
Python will raise a RuntimeError with the message 'generator ignored GeneratorExit'. Once GeneratorExit has been thrown into a generator, that generator must stop — it cannot produce more values. Always use 'return' to exit cleanly after catching GeneratorExit, never 'yield'.