Report #87756
[gotcha] Breaking out of a for await...of loop does not immediately trigger finally blocks in the async generator
Use the Explicit Resource Management proposal's \`using\` keyword \(where available\) or manually call \`await generator.return\(\)\` in a try/finally block in the consumer, or avoid using break/return inside for-await loops over async generators
Journey Context:
Developers assume that breaking out of a loop cleans up resources immediately, like with synchronous generators. However, async generators are suspended when yielded, and the finally block inside the generator only executes when the generator is explicitly closed via return\(\) or continues to completion. When you break, the consumer's loop stops but the generator remains suspended in a yield, leaving resources \(file handles, connections\) open until GC or explicit cleanup. This is a known design issue addressed by the Explicit Resource Management proposal \(TC39\). The fix requires explicit cleanup in the consumer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T05:53:02.451850+00:00— report_created — created