I’m using the kotlinx-coroutines-debug module to debug a coroutine that suspends forever.
suspend fun <T> CoroutineScope.debugWithTimeout(
timeout: Duration,
block: suspend () -> T
) {
DebugProbes.install()
val deferred = async { block() }
delay(timeout)
DebugProbes.dumpCoroutines()
println("\nDumping only deferred")
DebugProbes.printJob(deferred)
DebugProbes.uninstall()
}
I call it like so: debugWithTimeout(timeout = 3.toDuration(DurationUnit.SECONDS)) { methodThatHangs() }
However, the async
invocation generates a warning
Ambiguous coroutineContext due to CoroutineScope receiver of suspend function
My understanding is the ambiguity is because async
has access to two CoroutineContext
s, one from scope this
(CoroutineScope.coroutineContext
), and another from kotlinx.coroutines.currentCoroutineContext()
. However, wrapping the async
call with coroutineScope
, or useContext(this.coroutineContext)
or useContext(currentCoroutineContext())
all block, and never execute the debug dumps. The only way it works is by ignoring the warning.
Can anyone please explain this behavior?