Work around ktor server instrumentation leaking scope#16487
Merged
trask merged 5 commits intoopen-telemetry:mainfrom Mar 19, 2026
Merged
Work around ktor server instrumentation leaking scope#16487trask merged 5 commits intoopen-telemetry:mainfrom
trask merged 5 commits intoopen-telemetry:mainfrom
Conversation
trask
approved these changes
Mar 16, 2026
| * any time. | ||
| */ | ||
| object KtorServerTelemetryUtil { | ||
| val emptyInterceptor = object : ContinuationInterceptor { |
Member
There was a problem hiding this comment.
Suggested change
| val emptyInterceptor = object : ContinuationInterceptor { | |
| // A no-op ContinuationInterceptor that forces withContext to actually intercept (and later | |
| // restore) coroutine thread-context elements. Without this wrapper, Ktor 3.x's Unconfined | |
| // event-loop dispatcher can skip restoration, leaking the OpenTelemetry context across requests. | |
| // See https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/16430 | |
| private val emptyInterceptor = object : ContinuationInterceptor { |
Contributor
Author
There was a problem hiding this comment.
I'm not sure that this comment is completely correct. I added a slightly different one.
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.
Related to #16430
Looks like calling s suspend function in traced ktor server causes a context leak. For some reason it does not reproduce any more when a continuation interceptor is added. Similar issue has also been reported in https://youtrack.jetbrains.com/issue/KTOR-6118/CallMonitoring-SuspendFunctionGun-sometimes-leaks-coroutine-context