-
Notifications
You must be signed in to change notification settings - Fork 18.4k
fix(core): context preservation in shielded async callbacks #32163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Skipped Deployment
|
Co-authored-by: mdrxy <[email protected]>
CodSpeed WallTime Performance ReportMerging #32163 will not alter performanceComparing
|
CodSpeed Instrumentation Performance ReportMerging #32163 will not alter performanceComparing Summary
|
The
@shielded
decorator in async callback managers was not preserving context variables, breaking OpenTelemetry instrumentation and other context-dependent functionality.Problem
When using async callbacks with the
@shielded
decorator (applied to methods likeon_llm_end
,on_chain_end
, etc.), context variables were not being preserved across the shield boundary. This caused issues with:The issue was reproducible with:
Root Cause
The original
shielded
decorator implementation:Used
asyncio.shield()
directly without preserving the current execution context, causing context variables to be lost.Solution
Modified the
shielded
decorator to:copy_context()
asyncio.create_task(coro, context=ctx)
for Python 3.11+Testing
test_shielded_callback_context_preservation()
that validates context variables are preserved across shielded callback boundariesThe fix is minimal, maintains backward compatibility, and ensures proper context preservation for both modern Python versions and older ones.
Fixes #31398.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.