fix: prevent OpenTelemetry scope leaks masking original exceptions#681
Merged
Conversation
When a message send failed inside a traced flow, the scope activated in TracingChannelInterceptor::preSend was never detached, causing 'unexpected call to Scope::detach()' notices that replaced the original exception under Symfony's error handler. - SendingInterceptorAdapter guarantees exactly one afterSendCompletion per executed preSend on every path (success, failure, preSend failure, message drop), with per-interceptor guarded cleanup - TracingChannelInterceptor tracks open spans in a per-channel LIFO stack instead of a temporary message header - TracerInterceptor uses try/finally for the consumer scope and closes the Distributed Bus span on failure Fixes #419
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.
Why is this change proposed?
Fixes #419. When a message send failed inside a traced flow (e.g. a broker or database outage while running
ecotone:run), the OpenTelemetry scope activated inTracingChannelInterceptor::preSendwas never detached. Every subsequent span closing then triggeredScope: unexpected call to Scope::detach() for scope #XXXX, scope successfully detached but another scope should have been detached first. Under Symfony's error handler this notice becomes anErrorExceptionthat replaces the original exception, so users saw a confusing tracing error instead of the real cause of the failure.Description of Changes
SendingInterceptorAdapternow guarantees the channel-interceptor lifecycle: every interceptor whosepreSendcompleted receives exactly oneafterSendCompletionon every path — send success, send failure, failure during another interceptor'spreSend, and message drop (preSendreturningnull, used in production by the collector module)afterSendCompletion/postSend) are individually guarded, so one interceptor failing cannot prevent the others from cleaning up — and the original send exception always takes priority over cleanup failuresTracingChannelInterceptortracks its open span in a per-channel LIFO stack (newChannelSendSpanwith idempotent close) instead of storing the scope object in a temporary message header, so apreSendthat rebuilds the message can no longer break scope cleanupTracerInterceptor::traceAsynchronousEndpointusestry/finallyfor scope detach;traceDistributedBusnow records the exception and closes its span on failure instead of leaking the scopeTracingScopeCleanupTestcovering 8 scenarios: channel send failure, interceptor failing before send, message filtered out mid-send, cleanup failure of another interceptor,postSendfailure, message rebuilt without framework headers, error-channel send failure, and Distributed Bus send failureWhat users experience
Before — real failure hidden behind a tracing notice:
After — the original exception propagates and the failed send is recorded on the span:
No configuration changes are needed; existing tracing setups keep working as-is:
Use case scenarios
Scope::detach()noticesequenceDiagram participant Consumer as Polling Consumer participant Tracer as TracerInterceptor participant Adapter as SendingInterceptorAdapter participant Tracing as TracingChannelInterceptor participant Channel as Message Channel Consumer->>Tracer: consume message (activate consumer scope) Tracer->>Adapter: handler publishes event Adapter->>Tracing: preSend (activate send scope) Adapter->>Channel: send Channel-->>Adapter: ❌ broker failure rect rgb(220, 245, 220) Note over Adapter,Tracing: NEW: afterSendCompletion always invoked Adapter->>Tracing: afterSendCompletion(exception) Tracing->>Tracing: close span, detach scope end Adapter-->>Tracer: original exception rethrown Tracer->>Tracer: finally: detach consumer scope (balanced) Tracer-->>Consumer: original exception, no Scope noticesPull Request Contribution Terms