Open Legion2 opened 3 months ago
/cc @geoand (kotlin)
Can someone point me to the source code of the coroutine dispatcher implementation, then I can take a look and try to debug the problem.
I found this dispatcher implementation which is used by the resteasy reactive client.
There is also a similar implementation in the vertx extension https://github.com/quarkusio/quarkus/blob/ea2c6a4090d1eccb4b39250198601f7da9454833/extensions/vertx/kotlin/runtime/src/main/kotlin/io/quarkus/vertx/kotlin/runtime/VertxDispatcher.kt
Both implementations do not use the existing vertx Dispatcher and implement a very basic execution of coroutine on the vertx event loop.
I am also interested in the outcome of the analysis of this issue.
Thanks for opening this!
Just so I understand this, you are saying that when are manually handling the launch of coroutines, the dispatcher is single threader?
Also, some comments on things mentioned above:
I found this dispatcher implementation which is used by the resteasy reactive client.
The code you have linked to is for the Quarkus REST server part, not the client.
Both implementations do not use the existing vertx Dispatcher
Which one are you refering to as the existing vertx Dispatcher
?
Which one are you refering to as the existing vertx Dispatcher?
The vertx.dispatcher()
from vertx-lang-kotlin-coroutines
(https://vertx.io/docs/vertx-lang-kotlin-coroutines/kotlin/#_running_a_coroutine_from_a_vert_x_context)
Oh, I was not aware of that one at all...
I wonder if we should start using it elsewhere too Never mind, that would not work in Quarkus
@geoand why can the Dispatcher from vertx-lang-kotlin-coroutines not be used in quarkus. If it can not reused, what changes are needed in quarkus to port the Dispatcher from vertx-lang-kotlin-coroutines.
See the code in RESTEasy Reactive that uses its own dispatcher
@geoand you mean this code?
requestScope.activate()
CurrentRequestManager.set(rrContext)
try {
block.run()
} finally {
requestScope.deactivate()
}
I don't know what this code is used for, but it looks like it wraps the actual request code. I think there are two options:
I see you also implemented coroutine canceling for canceled requests, which is realy cool. Can't wait having this in my projects.
I don't know what this code is used for
You essentially need to propagate a lot of context from the thread were the request initiates to whatever thread is going to be used for running each stage of the suspend(able) function
I see you also implemented coroutine canceling for canceled requests, which is realy cool. Can't wait having this in my projects.
Thanks. I was actually surprised we didn't have this already...
You essentially need to propagate a lot of context from the thread were the request initiates to whatever thread is going to be used for running each stage of the suspend(able) function
Have a look at ThreadContextElement
I was not aware of that, thanks
Describe the bug
When working with kotlin coroutines and suspend functions in quarkus, the code is executed sequentially by a single thread by default. One has to explicitly change the kotlin coroutine dispatcher to get the expected behavior of the
Dispatchers.Default
which uses a Thread pool.Expected behavior
By default when using suspend function with quarkus the
Dispatchers.Default
orVertx.vertx().dispatcher()
should be used as the dispatchers. Or it should be possible to configure the used Dispatcher.Actual behavior
Some single threaded Dispatcher is used (maybe
runBlocking
is used) to run the suspend functions.How to Reproduce?
Add the following code to an RestEasy Resource and call the endpoint
Output: total time 10 seconds
Output: total time 1 second
Output: total time 1 second
Output of
uname -a
orver
Linux leon-pc 6.5.0-35-generic #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 7 09:00:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Output of
java -version
openjdk version "17.0.11" 2024-04-16 OpenJDK Runtime Environment (build 17.0.11+9-Ubuntu-122.04.1) OpenJDK 64-Bit Server VM (build 17.0.11+9-Ubuntu-122.04.1, mixed mode, sharing)
Quarkus version or git rev
3.7.3
Build tool (ie. output of
mvnw --version
orgradlew --version
)Gradle 8.5
Additional information
No response