fix: #2246 handle ContextVar token mismatch in concurrent execution#2255
Closed
Ray0907 wants to merge 1 commit intoopenai:mainfrom
Closed
fix: #2246 handle ContextVar token mismatch in concurrent execution#2255Ray0907 wants to merge 1 commit intoopenai:mainfrom
Ray0907 wants to merge 1 commit intoopenai:mainfrom
Conversation
…tion Add fallback for ValueError when resetting trace/span context in concurrent Runner.run() scenarios. Prevents crashes while preserving tracing functionality.
Member
|
Thanks for sharing this idea! I think #2260 can resolve this in a stabler way, so we'll have the per-run config enhancement instead. |
Member
|
Closing this in favor of #2260; thanks again for taking the time to send this! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix ValueError crash when using concurrent
Runner.run()calls withasyncio.gather().Problem:
When multiple
Runner.run()calls execute concurrently, theContextVar.reset(token)operation can fail with:ValueError: <Token ...> was created in a different Context
This forces users to disable tracing (
tracing_disabled=True) as a workaround, losing all observability.Solution:
Add defensive fallback mechanism:
ValueErrorwhenreset()fails due to context mismatchset(prev_value)to restore the previous stateChanges:
src/agents/tracing/scope.py: Add fallback handling inreset_current_span()andreset_current_trace()src/agents/tracing/traces.py: Store_prev_traceinNoOpTraceandTraceImplsrc/agents/tracing/spans.py: Store_prev_spaninNoOpSpanandSpanImplTest plan
Issue number
#2246
Checks
make lintandmake format