Skip to content

Work around ktor server instrumentation leaking scope#16487

Merged
trask merged 5 commits intoopen-telemetry:mainfrom
laurit:ktor3-workaround
Mar 19, 2026
Merged

Work around ktor server instrumentation leaking scope#16487
trask merged 5 commits intoopen-telemetry:mainfrom
laurit:ktor3-workaround

Conversation

@laurit
Copy link
Copy Markdown
Contributor

@laurit laurit commented Mar 13, 2026

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

@laurit laurit requested a review from a team as a code owner March 13, 2026 10:39
@laurit laurit changed the title Work around ktor3 server instrumentation leaking scope Work around ktor server instrumentation leaking scope Mar 13, 2026
* any time.
*/
object KtorServerTelemetryUtil {
val emptyInterceptor = object : ContinuationInterceptor {
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.

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 {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure that this comment is completely correct. I added a slightly different one.

@trask trask merged commit 8416457 into open-telemetry:main Mar 19, 2026
93 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants