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

Allow Dispatchers.Unconfined to use the event loops as time sources #4185

Open
dkhalanskyjb opened this issue Jul 18, 2024 · 0 comments
Open
Labels

Comments

@dkhalanskyjb
Copy link
Collaborator

What do we have now?

import kotlinx.coroutines.*

fun main() {
    runBlocking {
        launch(Dispatchers.Unconfined) {
            println("Starting in ${Thread.currentThread()}")
            delay(100)
            println("Continuing in ${Thread.currentThread()}")
        }
    }
}

currently outputs

Starting in Thread[main @coroutine#2,5,main]
Continuing in Thread[kotlinx.coroutines.DefaultExecutor @coroutine#2,5,main]

DefaultExecutor is the thread backing the whole kotlinx.coroutines framework, and its availability is important for delays to work correctly. We shouldn't give it arbitrary tasks to execute if we can avoid it.

What should be instead?

Before the delay, the Unconfined dispatcher uses the event loop created by runBlocking to execute its tasks. That event loop is also a delay, so delay(100) could execute in runBlocking as well, which would mean the unconfined task would be resumed in that same event loop, not bothering the DefaultExecutor.

Why?

Improved availability of DefaultExecutor could decrease the latency of resumption of tasks after delay in some scenarios.

Why not?

We haven't actually seen this being a problem in the wild: Dispatchers.Unconfined rarely needs to call delay, just by the nature of the tasks typically executing in it. Also, in the cases where it does happen, this could be a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant