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

limitedParallelism does not propagate the full CoroutineContext to the underlying dispatcher #4357

Conversation

DmitryTsyvtsyn
Copy link

No description provided.

@dkhalanskyjb
Copy link
Collaborator

Issue: #4305

This approach doesn't work, and the issue is far from being this easy to solve. See this code:

val limited = object : CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        Dispatchers.Default.dispatch(EmptyCoroutineContext, Runnable {
            println("${Thread.currentThread()}: Ensuring the context $context")
            block.run()
            println("${Thread.currentThread()}: Finished executing in the context $context")
        })
    }
}.limitedParallelism(2)

limited.dispatch(CoroutineName("Hi!")) {
    Thread.sleep(100)
    println("${Thread.currentThread()}: Hello!")
}
limited.dispatch(CoroutineName("Servus!")) {
    Thread.sleep(100)
    println("${Thread.currentThread()}: Hallo!")
}
limited.dispatch(CoroutineName("Hola!")) {
    Thread.sleep(100)
    println("${Thread.currentThread()}: ¡Hola!")
}
limited.dispatch(CoroutineName("Привет!")) {
    Thread.sleep(100)
    println("${Thread.currentThread()}: Здравствуйте!")
}

Output with the proposed change:

Thread[DefaultDispatcher-worker-2,5,main]: Ensuring the context CoroutineName(Servus!)
Thread[DefaultDispatcher-worker-1,5,main]: Ensuring the context CoroutineName(Hi!)
Thread[DefaultDispatcher-worker-2,5,main]: Hallo!
Thread[DefaultDispatcher-worker-1,5,main]: Hello!
Thread[DefaultDispatcher-worker-2,5,main]: ¡Hola!
Thread[DefaultDispatcher-worker-1,5,main]: Здравствуйте!
Thread[DefaultDispatcher-worker-2,5,main]: Finished executing in the context CoroutineName(Servus!)
Thread[DefaultDispatcher-worker-1,5,main]: Finished executing in the context CoroutineName(Hi!)

We see that "Hola" and "Здравствуйте" run in the wrong context anyway. What's needed here if we decide to fix the issue (which may well not be worth the effort) is a more complex setup that checks which workers run in which contexts and ensures that each task runs in the context appropriate for it, possibly shutting workers down and creating new ones to allow that.

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

Successfully merging this pull request may close these issues.

2 participants