Skip to content

fix(otel): prevent contextvars leak across async generators (#5722)#5725

Open
chriskinzel wants to merge 2 commits into
google:mainfrom
chriskinzel:fix-otel-context-leak
Open

fix(otel): prevent contextvars leak across async generators (#5722)#5725
chriskinzel wants to merge 2 commits into
google:mainfrom
chriskinzel:fix-otel-context-leak

Conversation

@chriskinzel
Copy link
Copy Markdown

Fixes #5722. Explicitly manage OpenTelemetry context attachment and detachment around generator yield boundaries using context.attach and context.detach to prevent span context leaks across coroutine boundaries.

@rohityan rohityan self-assigned this May 18, 2026
@rohityan rohityan added core [Component] This issue is related to the core interface and implementation request clarification [Status] The maintainer need clarification or more information from the author labels May 18, 2026
@rohityan
Copy link
Copy Markdown
Collaborator

Hi @chriskinzel , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you please fix the failing unit tests before we can proceed with the review.

Comment on lines +292 to +296
token = context.attach(caller_ctx)
try:
yield event
finally:
context.detach(token)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like it's doing the opposite of what's proposed in #5722 (comment) (attaching before yield, not before).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation request clarification [Status] The maintainer need clarification or more information from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenTelemetry contextvars leak across asynchronous generator boundaries in BaseAgent and Runner

3 participants