Hello! Spent the end of the day trying to understa...
# ktor
g
Hello! Spent the end of the day trying to understand why my correlation id from http header doesn't get propagated in MDC context for child coroutines. It turned out the problem exists only for Ktor v3😯. Feels like a bug. Submitted KTOR-8501
a
Hey. The problem is that
this
within the route handler isn't a coroutine scope, so the
launch
is executed from the scope of the
embeddedServer
. As a workaround, you can define a coroutine scope in the route handler:
Copy code
get("/") {
    coroutineScope {
        launch {
            println("Is null with Ktor v3: " + MDC.get(CLIENT_ID_MDC_KEY))
        }
        call.respondText("Retrieved as expected: " + MDC.get(CLIENT_ID_MDC_KEY))
    }
}
g
Hello @Aleksei Tirman [JB]. Thank you for the explanation! That gives me an understanding why
this
has a
SupervisorJob
type. Looks like things have changed since Ktor v2, I left some more details in the issue comments. After some experiments I think, it's generally not a problem for me. I can indeed call
call.launch{...}
from routing or start another coroutine context deep in my business logic layer, this way MDC context gets properly inherited anyway.