Quick revision
The short answer
The logging MDC and Spring Security's SecurityContextHolder are ThreadLocal-based, so code running in a CompletableFuture on another thread does not see them unless we propagate them; Spring Security does this for Callable and the MVC async dispatch, but not for futures we start on our own executors or the common pool. We fix it with an executor whose TaskDecorator copies the MDC and security context, DelegatingSecurityContextExecutor, or Micrometer context propagation with ContextSnapshot. The hangs come from futures that are never completed, so every path must complete, exceptionally if needed, with orTimeout as a safety net; virtual threads with plain blocking code are often the simpler alternative.
What to remember
In this answer, we saw that the MDC and SecurityContextHolder are ThreadLocal-based, so a CompletableFuture running on the common pool or our own executor sees neither, while Spring Security propagates the context only for Callable and the async dispatch. We fixed it with an executor using a TaskDecorator that copies and clears both, or with DelegatingSecurityContextExecutor or ContextPropagatingTaskDecorator. The hangs came from futures never completed on error paths, fixed by completing exceptionally everywhere and adding orTimeout. Finally, virtual threads with plain blocking code avoid the whole problem for I/O-bound work.
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.
The logging MDC and Spring Security's SecurityContextHolder are ThreadLocal-based, so code running in a CompletableFuture on another thread does not see them unless we propagate them; Spring Security does this for Callable and the MVC async dispatch, but not for futures we start on our own executors or the common pool. We fix it with an executor whose TaskDecorator copies the MDC and security context, DelegatingSecurityContextExecutor, or Micrometer context propagation with ContextSnapshot. The hangs come from futures that are never completed, so every path must complete, exceptionally if needed, with orTimeout as a safety net; virtual threads with plain blocking code are often the simpler alternative.
Why this question is asked
These three symptoms usually arrive together after a team converts controllers to return CompletableFuture for performance. Logs suddenly lack trace ids, so incidents cannot be followed across services. Authorisation checks deep in the service fail because there is no user. And a few requests hang for 30 seconds and then fail with a 503. Each has a clear cause and a clear fix. Interviewers ask this because it tests both thread-level understanding and practical debugging. In this answer, we will recap ThreadLocal context, reproduce the lost context, see what Spring propagates on its own, fix the propagation, then explain and fix the hangs, and finally consider whether futures are needed at all.
1A Quick Recap of ThreadLocal Context
A ThreadLocal is a variable that holds a separate value for each thread. Many libraries use one to carry request context implicitly. The logging MDC, mapped diagnostic context, holds values such as traceId and userId that the log pattern prints on every line. SecurityContextHolder holds the authenticated user, by default in a ThreadLocal, so any code in the request can call SecurityContextHolder.getContext().getAuthentication().
This works because, in a classic MVC request, one thread runs everything. The moment work moves to another thread, the new thread has its own, empty ThreadLocals.
2Reproducing the Lost Context
Here is a typical converted controller. The future runs on the common ForkJoinPool, because supplyAsync without an executor uses it.
6 messages in order. Play them, or step through them one at a time.
ThreadLocals belong to threads; nothing copied them to the pool thread.
The first log line runs on the Tomcat thread, which has the MDC and the security context. The second runs on a ForkJoinPool thread, which has neither. Notice that this is not a Spring bug: the values were never copied to the new thread.
3What Spring Propagates on Its Own
Spring Security does propagate the security context in two places. For a Callable return value, a Security integration with Spring MVC's async support sets the context on the thread that runs the Callable. And on the async dispatch, when the result is written, Security's filters restore the context for that dispatch.
But a CompletableFuture is different. Spring did not create its thread; our code did, inside supplyAsync, before Spring even saw the future. So Spring has no chance to copy anything into it. The same applies to any executor we create and submit to ourselves. This is the gap the team fell into.
4Fixing the Propagation
The fix is to run the futures on an executor that copies the context from the submitting thread to the worker thread and clears it afterwards. A TaskDecorator does exactly that for Spring's ThreadPoolTaskExecutor: it wraps each Runnable at submission time.
There are ready-made alternatives. Spring Security's DelegatingSecurityContextExecutor and DelegatingSecurityContextAsyncTaskExecutor wrap an executor to copy the security context. And since Spring Framework 6.1, ContextPropagatingTaskDecorator uses Micrometer's context-propagation library: it takes a ContextSnapshot of every registered ThreadLocal accessor, including the observation and tracing context and, with the right accessors, the MDC and security context, and restores it on the worker thread. In a Boot service with Micrometer Tracing, that is often the cleanest choice, because traces keep their spans too. The key rule either way: never call supplyAsync without an executor in request code.
5Why Some Requests Hang Until the Timeout
Spring MVC keeps the response open until the returned future completes. If the future is never completed, the request waits for the async timeout, 30 seconds by default on Tomcat or whatever spring.mvc.async.request-timeout says, and then fails with a 503.
How does a future never complete? The common cases are a hand-made CompletableFuture that is completed from a callback which never fires, for example a listener that catches an exception and logs it without calling completeExceptionally; a callback library that drops the call on an error path; or a chain that waits on another future that itself never completes. The exception is not lost in a dramatic way; it is caught somewhere and the future is simply forgotten.
orTimeout, available since Java 9, completes the future exceptionally with TimeoutException if nothing else completes it in time. Setting it shorter than the MVC async timeout means our own exception handler decides the response, rather than a generic 503.
6The Simpler Alternative: Virtual Threads
Often the futures were introduced only to avoid blocking Tomcat's worker threads during downstream calls. On Java 21 or later, Spring Boot 3.2 and later can run each request on a virtual thread with spring.threads.virtual.enabled=true. Blocking a virtual thread is cheap, so the controller can call the service directly and return the result.
Because everything then runs on one thread per request again, the MDC and security context simply work, stack traces are readable, and there is no future to forget to complete. We still need timeouts on the downstream clients, but the whole category of context and completion bugs disappears. For a team that adopted futures purely for capacity, this is usually the best fix of all.
7Follow-up questions
Can I just switch SecurityContextHolder to inheritable mode?
MODE_INHERITABLETHREADLOCAL copies the context to threads created by the request thread, but pool threads are created once and reused, so they inherit whatever context existed when they were created, possibly another user's. It is unsafe with thread pools and should not be used for this.
How do you find which futures never complete?
Look for requests ending in AsyncRequestTimeoutException in the logs, group them by endpoint, and inspect the futures those endpoints create. Adding orTimeout with a specific message per call site makes the culprit name itself in the next occurrence.
Does WebFlux have the same problem?
The same underlying issue exists, since work hops between threads, but Reactor carries context in its own Context object rather than ThreadLocals, and Micrometer context propagation bridges the two. The fixes look different, but the principle, propagate explicitly, is the same.
8Conclusion
In this answer, we saw that the MDC and SecurityContextHolder are ThreadLocal-based, so a CompletableFuture running on the common pool or our own executor sees neither, while Spring Security propagates the context only for Callable and the async dispatch. We fixed it with an executor using a TaskDecorator that copies and clears both, or with DelegatingSecurityContextExecutor or ContextPropagatingTaskDecorator. The hangs came from futures never completed on error paths, fixed by completing exceptionally everywhere and adding orTimeout. Finally, virtual threads with plain blocking code avoid the whole problem for I/O-bound work.
Open with
All three symptoms come from moving work to other threads. The MDC, which carries the trace id and user for logging, and SecurityContextHolder are both ThreadLocal-based. When the controller does CompletableFuture.supplyAsync without an executor, the work runs on the ForkJoin common pool, whose threads have empty ThreadLocals, so logs lose the trace id and method security sees no authentication.
Then, if they want more
Spring Security does propagate the context for Callable and on the async dispatch, but it can't for a future my code started on its own thread.
The fix is to never use the common pool in request code and run futures on an executor that propagates context: a ThreadPoolTaskExecutor with a TaskDecorator that captures the MDC and security context at submit time, sets them on the worker, and clears them in finally because threads are pooled.
Alternatives are DelegatingSecurityContextExecutor for security, or ContextPropagatingTaskDecorator with Micrometer context propagation, which also keeps tracing spans. The hangs are futures that never complete. Spring keeps the response open until the future finishes, so if an error callback just logs and never calls completeExceptionally, the request waits for the async timeout and gets a 503.
So every path must complete the future, and I'd add orTimeout shorter than the MVC timeout so our own exception handler answers. Finally, I'd ask whether futures are needed at all. On Java 21 or later with spring.threads.virtual.enabled, plain blocking code on virtual threads gives the capacity and keeps ThreadLocal context working.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 9, Spring Boot 3.2, Spring Framework 6
- 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
- Spring Framework reference, Spring Web MVC
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 "A service returns CompletableFuture from controllers. Logs for the completion part lose...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/a1497480c9/