fix(rack): fiber-safe context detachment in EventHandler#2130
Open
rsamoilov wants to merge 1 commit intoopen-telemetry:mainfrom
Open
fix(rack): fiber-safe context detachment in EventHandler#2130rsamoilov wants to merge 1 commit intoopen-telemetry:mainfrom
EventHandler#2130rsamoilov wants to merge 1 commit intoopen-telemetry:mainfrom
Conversation
Store fiber reference with context token to safely skip detachment when on_finish is called from a different fiber than on_start. Spans are always finished regardless of fiber. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
One or more co-authors of this pull request were not found. You must specify co-authors in commit message trailer via: Supported
Alternatively, if the co-author should not be included, remove the Please update your commit message(s) by doing |
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
Fixes context detachment errors in fiber-based environments.
Problem: When using
Rack::Events, theon_finishcallback can be invoked from a different fiber thanon_start(e.g., when streaming response bodies). This causesOpenTelemetry::Context.detach(token)to fail with:Solution: Store the fiber reference alongside the token and span. On
detach_context:Note on fiber pooling: Skipping
detachcould leave stale context in the original fiber. However, servers that reuse fibers across requests would be fundamentally incompatible with any Ruby feature relying on fiber-locals (e.g.,ActiveSupport::CurrentAttributes), so this isn't something we should solve at the instrumentation level.Changes
otel.rack.token_and_spantootel.context_info[token, span]to[fiber, token, span]detach_contextto check fiber identity before detachingTest plan