-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Consider marking runBlocking
as @DelicateCoroutinesApi
#4242
Comments
That would be a nice idea if we were just to (re-)introduce The problem is, it exists for quite a few years already, is really widespread (i.e. single DuckDuckGo app has 83 usages) and already leveraged in coursed, lectures, talks and books. Marking it as a delicate API will likely create a serious disruption and a lot of immediate opt-ins (potentially module-wide), which will rather undermine the meaning and intent of |
This is fair enough. Then here are my suggestions:
|
To be honest I believe this argument is rather in favor of this change. Since |
It's worth mentioning that as far as I understand currently, suspending main functions are not available on Kotlin/Native yet. I reported this issue in YouTrack a while back and it hasn't received any attention. Also, if I understand correctly, runBlocking's event loop also implements delay so additional threads don't have to be utilized for delays. Suspending entry points don't and therefore would spin up the default delay dispatcher's threads to delay. I agree that documentation about where it's safe to use run blocking would be nice. I imagine it would essentially amount to saying it's only safe to use when bridging blocking functions that are known to not have been called from a coroutine. Finally, I agree that run blocking being widely used shouldn't be an impediment to marking it as a delicate API. |
From the runBlocking documentation:
Part of this is obsolete now. We have
suspend fun main
to get a suspending context right from the entrypoint. We havekotlinx-coroutines-test
for tests.This leaves the "briding" use case as the remaining valid case for
runBlocking
, but this is quite general/vague andrunBlocking
is still dangerous in many such cases. We have already seen a few nasty cases whererunBlocking
is synonym for trouble:runBlocking
deadlocks when a task depends on the nestedrunBlocking
itself #3982runBlocking
executes other tasks instead of its own #4215My intuition is that we should only use
runBlocking
when we have no other choice, and that there aren't many cases whererunBlocking
is the only choice (and is safe). In that sense, I believe it would be a net positive to markrunBlocking
as@DelicateCoroutinesApi
, with an updated doc to explain more details.It would be worth documenting the cases that are safe for
runBlocking
, and the cases for which we know better alternatives. A bit like what was done forGlobalScope
.The text was updated successfully, but these errors were encountered: