Skip to content

fix: delegate traced generator send and throw operations - #1077

Closed
Chen Yufeiyang (feiiiiii5) wants to merge 1 commit into
braintrustdata:mainfrom
feiiiiii5:codex/traced-generator-delegation
Closed

Chen Yufeiyang (feiiiiii5) wants to merge 1 commit into
braintrustdata:mainfrom
feiiiiii5:codex/traced-generator-delegation

Conversation

@feiiiiii5

Copy link
Copy Markdown
Contributor

@traced currently advances generator functions with for / async for. Values passed through send() / asend() are discarded, and throw() / athrow() exceptions never reach the decorated generator. For example, sending "response" into value = yield "ready"; yield value yields None instead.

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 GeneratorExit propagation. 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T15:21:01.098702Z b9e44f0 PR opened
ℹ️ About Codex in GitHub

Your 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

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.

2 participants