Skip to content

Fix async generator stop iteration#129

Open
jaheyy wants to merge 2 commits intoawslabs:masterfrom
jaheyy:fix/async-generator-stopiteration-128
Open

Fix async generator stop iteration#129
jaheyy wants to merge 2 commits intoawslabs:masterfrom
jaheyy:fix/async-generator-stopiteration-128

Conversation

@jaheyy
Copy link

@jaheyy jaheyy commented Dec 3, 2025

Fixes #128

This PR fixes the async generator bug where generators would always throw exceptions on completion due to incorrectly catching StopIteration instead of StopAsyncIteration.

Changes

Commit 1: Bug Fix (fix:)

  • Replace manual anext() calls with async for loop syntax
  • Replace manual next() calls with for loop syntax
  • Change exception handling from catching StopIteration to using try-finally
  • Async generators now properly complete without raising exceptions
  • Ensure metrics are always flushed even when exceptions occur

Commit 2: Performance Optimization (perf:)

  • Remove flush calls after each yield in generators
  • Metrics are now flushed only when generator completes (in finally block)
  • Improves performance by reducing flush operations
  • Maintains correctness with guaranteed flush on completion or exception

@jaheyy jaheyy changed the title Fix/async generator stopiteration 128 Fix async generator stop iteration Dec 3, 2025
Copy link

@lksfrnz lksfrnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

@johnjang johnjang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Blazej Gorny added 2 commits March 2, 2026 21:12
- Replace manual __anext__() calls with async for loop syntax
- Replace manual next() calls with for loop syntax
- Change exception handling from catching StopIteration to using try-finally
- Async generators now properly complete without raising exceptions
- Ensure metrics are always flushed even when exceptions occur

Fixes awslabs#128
…h yield

- Remove flush calls after each yield in generators
- Metrics are now flushed only when generator completes (in finally block)
- Improves performance by reducing flush operations
- Maintains correctness with guaranteed flush on completion or exception
@jaheyy jaheyy force-pushed the fix/async-generator-stopiteration-128 branch from 8388419 to c091341 Compare March 2, 2026 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@metric_scope always throws for async generators

3 participants