spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Revise approach for RequestAttributesThreadLocalAccessor

Open rstoyanchev opened this issue 1 year ago • 2 comments

#32243 added a built-in ThreadLocalAccessor adapter for RequestContextHolder. However, the FrameworkServlet which manages the thread local, also calls RequestAttributes#requestCompleted on the way out, so in case of an asynchronous request, the propagated RequestAttributes instance ends up having requestActive=false which results in IllegalStateException if you try access any attributes.

We need to consider how to address this. The simplest option would be to create a separate ServletRequestAttributes instance for context propagation purposes.

rstoyanchev avatar Feb 19 '24 21:02 rstoyanchev

I've moved this to a task as the original proposal is not released yet and therefore this isn't user facing if it is fixed in the same milestone.

snicoll avatar Feb 20 '24 08:02 snicoll

There's a link to a sample SSE renderer project in https://github.com/spring-projects/spring-framework/issues/32112. It breaks if you use the ThreadLocalAccessor from 6.2 snapshots, but not by throwing exceptions - you just get a null request context where you needed it to be not-null. So I'm not sure the IllegalStateException mentioned above is the only problem with async requests.

dsyer avatar Apr 08 '24 20:04 dsyer