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

Comment From: sdeleuze

Could you please try with Spring Boot 4.0.0-SNAPSHOT as support for context propagation has been introduced very recently via https://github.com/spring-projects/spring-framework/issues/35485 and related issues ?

Comment From: PheelTi

Could you please try with Spring Boot 4.0.0-SNAPSHOT as support for context propagation has been introduced very recently via #35485 and related issues ?

Sure, will try this tonight, I will post a similar plain java projet, kotlin may not be the root cause.

Comment From: PheelTi

Hi, I updated the repo with a multi module maven project : https://github.com/PheelTi/spring-webflux-context 4 modules : - 1 : springboot 3.5.6 in Kotlin (the initial one I posted with this "issue") - 2 : springboot 3.5.6 in pure Java -> same behaviour - 3 : springboot 4 snapshot in Kotlin -> same behaviour - 4 : springboot 4 snapshot in pure Java -> same behaviour

I think Kotlin is not in cause... Maybe a .contextCapture() or defer() is missing in the Java core framework...

That being said, Springboot 4 webflux plays very well with Kotlin for context propagation, we won't need any custom CoWebFilter (see here) to propagate the context, nice job !