Skip to content

withLoggingContextAsync leaves MDC entries on the thread where the block started #607

Open

Description

@timobraveweber

withLoggingContextAsync writes its entries to the MDC of the current thread. Only then does it switch to withContext(MDCContext()). The body may suspend and resume on another thread. In that case the first thread keeps the entries. The cleanup then writes the previous values onto the second thread.

Unrelated work on either thread then logs with these entries.

runBlocking(Dispatchers.Unconfined) {
    withLoggingContextAsync("requestId" to "42") {
        withContext(Dispatchers.IO) {} // the body resumes on an IO thread
    }
}
println(MDC.get("requestId")) // prints 42

#75 reported the same problem for withLoggingContext and is closed. #597 adds a new function next to the existing one. Fixing withLoggingContextAsync itself is the smaller change. Callers keep their code and get the fix with the next upgrade. I will open a PR for that.

Activity

  1. github-actions commented on Oct 23, 2026

    @github-actions

    Thank you for reporting an issue. See the wiki for documentation and slack for questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions