Repository navigation
fix: delegate traced generator send and throw operations - #1077
Chen Yufeiyang (feiiiiii5) wants to merge 1 commit into
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b9e44f0a74
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".
| try: | ||
| sent = yield value | ||
| except GeneratorExit: | ||
| await async_gen.aclose() |
There was a problem hiding this comment.
Avoid double-closing async generators during loop shutdown
If a partially consumed traced async generator survives until asyncio.run() shuts down and its finally block awaits, the loop has registered both this wrapper and async_gen, so shutdown_asyncgens() schedules aclose() on both concurrently. This delegated close races with the loop's close, producing RuntimeError: aclose(): asynchronous generator is already running through the loop's exception handler. This is reproducible with the changed wrapper on Python 3.14; the parent revision does not produce the shutdown error. Coordinate finalization ownership so normal shutdown does not close the same source generator twice.
Useful? React with 👍 / 👎.
@tracedcurrently advances generator functions withfor/async for. Values passed throughsend()/asend()are discarded, andthrow()/athrow()exceptions never reach the decorated generator. For example, sending"response"intovalue = yield "ready"; yield valueyieldsNoneinstead.Delegate sent values and exceptions to the wrapped generator, retain the synchronous generator return value, and close the wrapped generator when the wrapper closes (awaiting async cleanup). Keep output collection, truncation and partial-error logging in the same path.
Regression tests cover sync/async generators with and without IO tracing, normal completion, explicit close and
GeneratorExitpropagation. Against unchanged production code: 14 failures and 2 passes. After the fix: logger tests 234 passed, 4 skipped, 23 subtests passed; Python 3.10 generator tests 36 passed. Full core: 1,135 passed, 114 skipped, 12 xfailed, 23 subtests passed. All pre-commit hooks and targeted Pylint passed. Provider integration matrices were not run.Commands:
pytest src/braintrust/test_logger.py -q;nox -R -s test_core -- --vcr-record=none -q.