Catch Generator Early Exit Resource Leak
Breaking out of a generator loop early raises GeneratorExit inside the generator. If cleanup logic is placed outside try...finally blocks, or if an assigned generator is never closed, active file handles or database transactions remain open. Always guard generator teardown with finally.
def file_reader(path):
f = open(path, 'r')
try:
for line in f:
yield line
finally:
f.close()
# Catch Generator Early Exit Resource Leak When you write a generator that connects to a database, yields rows, and closes the connection at the bottom of the function, what happens when the caller only reads 2 rows and exits with `break`? 🛑🌊 If your cleanup isn't wrapped in a `try...finally` block, the bottom of the generator is never reached! Even worse, if the generator is held in a variable (`gen = stream()`), Python won't trigger `GeneratorExit` until that variable is deleted or garbage-collected. ```python def stream_records(db_pool): conn = db_pool.acquire() try: for row in conn.fetch(): yield row finally: # Guaranteed to execute on break, exception, or .close() db_pool.release(conn) ``` ## Pop Quiz What exception does Python inject into an active generator when the consumer breaks or calls `.close()`? - [ ] GeneratorExit, which unwinds the generator stack and executes active finally blocks. - [ ] StopIteration, which terminates the entire calling thread immediately. - [ ] RuntimeError, which dumps the generator frame directly into sys.stderr. - [ ] BrokenPipeError, which notifies the kernel to flush underlying file buffers. ## Real-World Challenge Implement `safe_stream(tracker: list)`. It must append `"OPEN"` upon starting, yield consecutive integers ($1, 2, 3\dots$), and append `"CLOSED"` inside a `finally` block. Verify that breaking early from an anonymous loop (`for x in safe_stream(log):`) or explicitly calling `gen.close()` executes cleanup deterministically. <details> <summary>Solution</summary> ```python def safe_stream(tracker: list): tracker.append("OPEN") try: count = 1 while True: yield count count += 1 finally: tracker.append("CLOSED") # Verification 1: Anonymous generator loop with break log1 = [] for val in safe_stream(log1): if val == 3: break assert log1 == ["OPEN", "CLOSED"], f"Expected closed, got {log1}" # Verification 2: Explicit variable with close() log2 = [] gen = safe_stream(log2) first_val = next(gen) assert first_val == 1 gen.close() assert log2 == ["OPEN", "CLOSED"] ``` </details>