Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestAttributesThreadLocalAccessor still not working for SSE #32813

Open
dsyer opened this issue May 13, 2024 · 2 comments
Open

RequestAttributesThreadLocalAccessor still not working for SSE #32813

dsyer opened this issue May 13, 2024 · 2 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@dsyer
Copy link
Member

dsyer commented May 13, 2024

#32296 and #32112 are now closed but the sample that I was interested in is still broken (see link in the latter): the RequestContextHolder is still empty in an HttpMessageConverter being used to render a Flux as a MediaType.TEXT_EVENT_STREAM.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 13, 2024
@jhoeller jhoeller added the in: web Issues in web modules (web, webmvc, webflux, websocket) label May 13, 2024
@rstoyanchev
Copy link
Contributor

rstoyanchev commented May 14, 2024

Those are related issues but not the same. It means after the changes, the following works:

@GetMapping(path = "/", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Greeting> stream() {
	return Flux.interval(Duration.ofSeconds(1))
			.doOnNext(o -> {
				RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
				System.out.println("RequestAttributes: " + attributes);
			})
			.map(value -> new Greeting("bar"));
}

When Spring MVC subscribes to the Flux, it uses Micrometer Context Propagation to capture ThreadLocals it knows about, and inserts those into the Reactor Context where they can be accessed as key-value pairs. In addition, Reactor supports restoring ThreadLocals in the Flux chain, which is why doOnNext above finds the request.

The HttpMessageConverter, however, is called from the subscriber to the Flux, and it's not covered by the above, and message converters generally don't depend on thread local context.

@dsyer
Copy link
Member Author

dsyer commented May 14, 2024

I'm not sure I follow yet - isn't the subscriber Spring MVC, which you said would propagate the context? By "works" you mean "prints out request attributes", I guess, but that's not helpful if the HttpMessageConverter cannot access the thread local context. What I really want to do is render a View in the message converter, so I'm going to need the web context. Is there another way? Can we make it work somehow without leaking thread context - I don't think users (i.e. me) should be trusted to get that right without help from the framework?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

4 participants