Skip to content

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

Open
@dkhalanskyjb

Description

import kotlinx.coroutines.*
import kotlin.coroutines.*

object MyDispatcher: CoroutineDispatcher() {    
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        println(context)
        block.run()
    }
}

fun main() {
    val newDispatcher = MyDispatcher.limitedParallelism(2)
    newDispatcher.dispatch(CoroutineName("Hi!"), Runnable { })
}

prints

LimitedDispatcher@4459eb14

No CoroutineName in sight.

https://pl.kotl.in/86W-MnSGn

This bug was not reported to us as any production issue, but was found accidentally by reading

override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatchInternal(block) { worker ->
dispatcher.dispatch(this, worker)
}
}
@InternalCoroutinesApi
override fun dispatchYield(context: CoroutineContext, block: Runnable) {
dispatchInternal(block) { worker ->
dispatcher.dispatchYield(this, worker)
}
}

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions