spring-projects / spring-projects/spring-framework

Context not propagated in exception handler in Webflux

Open
#35,636 3 comments 0 reactions 1 assignee View on GitHub

@sdeleuze is already working on this.

Since Oct 15, 2025.

status: feedback-provided status: waiting-for-triage theme: kotlin
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

Hi,

I think it is an issue... but you may qualify this as an enhancement.

in short : a context set in a high level WebFilter is not propagated in the exception handler

I have sample project here : https://github.com/PheelTi/spring-webflux-context

  • Weblux / Kotlin / Micrometer context propagation
  • a custom ErrorWebExceptionHandler which adds a log and delegate the error handling to a DefaultErrorWebExceptionHandler
  • I have a custom context MyContext managed in a ThreadLocal and I want it to be propagated, to do so, I have implemented :
    • a CoWebFilter which propagates the Kotlin context with a custom ThreadContextElement
    • I used the ContextRegistry.getInstance().registerThreadLocalAccessor(...) to register the access to MyContext
    • I activated Hooks.enableAutomaticContextPropagation()
  • the value of MyContext is created in a high level businessWebFilter, and set in its ThreadLocal, and in the Reactor Context, and I print its value in several places :
    • in the business filter juste after initialisation, and around its cleaning
    • in the controller
    • in the exception handler.

Here are the results :

[INFO]  Context from 'starting request processing' : is present with value 'request 6c8d08dd-6238-48c1-b295-5c134f1c6b7a'
[INFO]  Context from 'controller processing' :  is present with value 'request 6c8d08dd-6238-48c1-b295-5c134f1c6b7a'
[INFO]  Context from 'exception handler' : is not present
[ERROR]  500 Server Error for HTTP GET "/test"

java.lang.RuntimeException: test exception
...

[INFO]  Context from 'finishing request processing' : is present with value 'request 6c8d08dd-6238-48c1-b295-5c134f1c6b7a'
[INFO]  Context from 'cleaned request processing' : is not present

I would expect to have MyContext also present in the exception handler...

I found those workarounds though :

  • annotate the exception handler class with @ControllerAdvice and the handle method with @ExceptionHandler(Throwable::class)
  • set my context in a higher level place like HttpHandlerDecoratorFactory

Thanks

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.