Quick revision
The short answer
Trace ids in the logging MDC and Spring Security's SecurityContext are normally stored in ThreadLocals, values attached to the current thread. Stages of a CompletableFuture chain run on executor threads that never had those values set, so logs lose the trace id and security checks find no user. The fix is to wrap the executor so it captures the context when a task is submitted and restores it around the task, for example with Micrometer's context-propagation library or Spring Security's DelegatingSecurityContextExecutor, clearing the restored context afterwards, and to test propagation across every async boundary.
What to remember
In this answer, we saw that trace ids in the MDC and Spring Security's SecurityContext live in ThreadLocals, so CompletableFuture stages running on executor threads do not see them. Wrapping the executor to capture context at submission and restore it around each task, with Micrometer's context-propagation library, Spring Security's DelegatingSecurityContextExecutor or a TaskDecorator, fixes it, provided every Async stage uses the wrapped executor. Clearing the restored context after each task prevents leaks between requests, and tests at every async boundary keep the fix from regressing.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Trace ids in the logging MDC and Spring Security's SecurityContext are normally stored in ThreadLocals, values attached to the current thread. Stages of a CompletableFuture chain run on executor threads that never had those values set, so logs lose the trace id and security checks find no user. The fix is to wrap the executor so it captures the context when a task is submitted and restores it around the task, for example with Micrometer's context-propagation library or Spring Security's DelegatingSecurityContextExecutor, clearing the restored context afterwards, and to test propagation across every async boundary.
Why this question is asked
Moving work into async chains often looks like a pure performance change, so the side effects are a surprise: log lines that cannot be correlated, and authorization that suddenly fails or, worse, sees another user. Both have the same cause and the same fix. Interviewers ask this because it shows whether we understand how request context is really stored in a Java service. In this answer, we will recap how MDC and SecurityContext store their values, reproduce the lost context, fix it with executor wrappers, see why clearing matters, and then test it.
1A Quick Recap: Context Lives in ThreadLocals
A ThreadLocal is a variable that has a separate value for each thread. Code running on a thread sees only the value set on that thread. Frameworks use this to make request information available everywhere without passing it as a parameter.
The logging MDC, short for Mapped Diagnostic Context, is one example: a tracing library such as Micrometer Tracing puts the traceId and spanId into the MDC when a request starts, and the log pattern prints them on every line. Spring Security's SecurityContextHolder is another: by default it keeps the SecurityContext, which holds the authenticated user, in a ThreadLocal, and @PreAuthorize checks and SecurityContextHolder.getContext() read it from there. Both are set on the request thread by a filter and cleared when the request ends.
2Reproducing the Lost Context
Now we move part of the request into a chain that runs on a custom executor.
5 messages in order. Play them, or step through them one at a time.
ThreadLocal values do not travel with the task.
Notice that the first log line has the trace id and the second does not. The supplier ran on a thread of appExecutor. Nobody set the MDC or the SecurityContext on that thread, so it sees empty values. If the pool thread had leftover values from an earlier task, it could even see another request's user, which is far worse than seeing none.
3Fix: Wrap the Executor to Capture and Restore Context
The idea is simple. When a task is submitted, we are still on the request thread, so we take a snapshot of its context. When the task runs on a pool thread, we put that snapshot in place, run the task, and then remove it. A wrapped executor does this for every task, so the chain code does not change.
Notice that we pass the wrapped executor to every Async stage. A stage that uses the default common pool, or a non-async stage that runs on whatever thread completed the previous future, does not get the context. So every Async stage in the chain must name the wrapped executor, and context-sensitive work should not run in non-async stages after a client callback.
4Doing It Once for Spring-Managed Executors
For executors that Spring creates, such as the one behind @Async or a ThreadPoolTaskExecutor bean, we can set a TaskDecorator instead of wrapping by hand. Spring Framework provides ContextPropagatingTaskDecorator, built on the same context-propagation library, and Spring Boot can apply it to its auto-configured task executor; check your Spring Boot version for the exact property or bean. For MDC alone, without that library, a small decorator also works.
The finally block is the important part. Pool threads are reused, so whatever we put on a thread must be removed when the task ends. Otherwise the next task on that thread would log the previous request's trace id, or run with the previous user's SecurityContext. The library wrappers do this clean-up for us, which is one reason to prefer them.
5Testing Propagation Across Every Async Boundary
Context bugs come back whenever someone adds a new executor or a new async stage, so we test them. A simple test sets a known trace id and a known user, runs the async flow, and asserts that code inside the last stage sees both. A second test runs two requests with different users on a pool with a single thread and checks that the second never sees the first one's user, which catches missing clean-up.
So each boundary where work changes threads has a test proving that the trace id and the user arrive, and that nothing leaks to the next task.
6Follow-up questions
Would virtual threads fix the problem?
No. A virtual thread is still a different thread, with its own empty ThreadLocals. Context must be captured and restored in the same way. Scoped values, a newer JDK feature, are designed to be inherited by subtasks in structured concurrency, but they cover only that model; check their status in your JDK.
Why not use InheritableThreadLocal?
It copies values only when a thread is created. Pool threads are created once and reused for many requests, so they would keep the values of whichever request happened to create them. That is exactly the leak we want to avoid.
What about Reactor pipelines in the same service?
Reactor stores context in its own Context object rather than in ThreadLocals. Micrometer's context-propagation library bridges the two, and Reactor can restore ThreadLocals from its Context automatically when that is enabled; check your Reactor and Spring Boot versions for how it is switched on.
7Conclusion
In this answer, we saw that trace ids in the MDC and Spring Security's SecurityContext live in ThreadLocals, so CompletableFuture stages running on executor threads do not see them. Wrapping the executor to capture context at submission and restore it around each task, with Micrometer's context-propagation library, Spring Security's DelegatingSecurityContextExecutor or a TaskDecorator, fixes it, provided every Async stage uses the wrapped executor. Clearing the restored context after each task prevents leaks between requests, and tests at every async boundary keep the fix from regressing.
Open with
Both the trace id and the security context are stored in ThreadLocals. The tracing library puts traceId and spanId into the logging MDC, and Spring Security's SecurityContextHolder keeps the authenticated user in a ThreadLocal by default, all set on the request thread by filters.
Then, if they want more
When I move work into a CompletableFuture chain on a custom executor, the stages run on pool threads where nobody set those values, so the logs have an empty trace id and SecurityContextHolder returns no authentication. If cleanup is missing somewhere, a pool thread could even carry a previous request's user, which is worse.
The fix is to wrap the executor so that it captures the context at submission time, on the request thread, and restores it around the task on the pool thread. For security there is DelegatingSecurityContextExecutor, and for tracing and MDC I would use Micrometer's context-propagation library, which can wrap an executor service, or a TaskDecorator on Spring's executors.
The wrapper must clear what it restored in a finally block, because pool threads are reused. Then every Async stage must use the wrapped executor, not the common pool. Finally I would add tests at each async boundary that check the trace id and user arrive, and that a single-thread pool does not leak one request's context into the next.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After moving request processing into CompletableFuture chains on a custom executor, logs...": https://scrivio-interview-library.netlify.app/java/completable-future/ad63783dd0/