-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Request scope sometimes not propagated to ManagedExecutor.runAsync() #13013
Comments
@wicksim I've spent some fiar time debugging and I think the problem lies elsewhere. Posting the scheduled method here for clarity:
You're using Basically, whether the If you add There's another issue here - why are you getting I am not even sure we should do that, the only difference in this particular case would be an exception instead of |
I'll need a little bit more time to really understand your answer. 😃 But for now: My "real" case is not a scheduler, but an invocation of a REST-Endpoint. But I think this will not make any difference? I forgot to mention in the bug report that the scheduler was just an easy way for the reproducer. |
And by the way: Is there another preferred way to execute an async task in which the context propagation works corrrectly? |
Well, I can't be certain without the actual code for that use case, but I'd say it is indeed similar. As in, launching an async to-be-done-later task might mean that it would happen after the request is already over.
Hm, I don't think this is about context propagation. Maybe I am poorly explaining this, but the issue lies in how request scope/context works in CDI. There is, by design, no way to use an information stored inside request scoped bean after its context has been shut down. You'd need to store that information in a bean (or elsewhere) that outlives destruction of request context (such as application scoped bean). Alternatively, you'd have to perform that async operation in a manner where you know it ends before you leave the request. Context propagation for CDI works so that while original thread has req. context active, so will any other thread trying to use it. But it is not intended as means of outliving the original thread's context. That would defeat the point of having a limited scope on a bean. |
Okay, so I do understand: Context propagation is about propagating the context (incl. request scoped beans, transactions, etc.) to other threads, but only as long as the original thread lives. But what about reactive programming (Mutiny)? Isn't it the case there, that things are executed async? So no request scoped information, too? Sorry, I'm kind of a newbie to this reactive thing... So if I need to execute anything async without waiting for it in the original thread, I need to clone the request scoped bean or map the content to local variables in the original thread and use this variables in the async executed code? |
We should probably try to summarize this limitation of CDI context propagation in the documentation (along with the fact that due to this, one shouldn't rely on
I think the trick is that the original thread never destroys request context in this case hence you can keep using it inside CP. But I am not sure as I don't use reactive myself. Maybe @FroMage or @cescoffier would know? |
If you do this in a REST endpoint and you your return type is an async type such as But the client will not get a REST response back until after your request completes. |
OK, I think I understand. One last question: If I need to perform a really async task (in the meaning of: the user gets an immediate response), the only possibility is to copy the relevant values from the request scoped bean to a local variable in the original thread an use this variable in the async execution, right? Or is there any other recommended way of doing this? Shall I close this bug or will there be any documentation change? |
As far as CDI context propagation goes, you could only use something like
I'll try to add at least some documentation regarding the pre destroy so keep it open please. |
Describe the bug
When executing code in ManagedExecutor.runAsync(), request-scoped beans are not always propagated. It often works on 1st call, but not on subsequent calls.
quarkus-smallrye-context-propagation
is active.Expected behavior
Reqest-scoped beans should always be available if context is propagated.
Actual behavior
Request-scoped beans are available only sometimes.
To Reproduce
async-request-scope-problem.zip
Steps to reproduce the behavior:
mvn compile quarkus:dev
System.out
5
(most often on first call) and sometimesnull
Configuration
None
Screenshots
(If applicable, add screenshots to help explain your problem.)
Environment (please complete the following information):
uname -a
orver
: Microsoft Windows [Version 10.0.17763.1457]java -version
: OpenJDK Runtime Environment Corretto-11.0.6.10.1 (build 11.0.6+10-LTS)mvnw --version
orgradlew --version
): Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)Additional context
(Add any other context about the problem here.)
The text was updated successfully, but these errors were encountered: