Closed dkhalanskyjb closed 1 month ago
A slightly different case of the same:
import kotlinx.coroutines.withContext
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
suspend fun x(input: Unit): Int {
withContext(Dispatchers.Default) {
println("Current thread is ${Thread.currentThread()}")
}
return 5
}
object MyContinuation: Continuation<Int> {
override val context: CoroutineContext = Dispatchers.Default
override fun resumeWith(result: Result<Int>) {
println(result)
}
}
fun main() {
::x.createCoroutineUnintercepted(Unit, MyContinuation).resumeWith(Result.success(Unit))
Thread.sleep(200)
}
Current thread is Thread[main,5,main]
Success(5)
I propose to proceed in #286
Describe the bug
Encountered by the IntelliJ team.
withContext
looks at the current coroutine context and decides whether it should perform a dispatch. However, the current coroutine context does not always correctly reflect which thread the coroutine executes on. So,withContext
may believe that it doesn't have to dispatch when it actually does.Provide a Reproducer
(https://pl.kotl.in/POeZD53Uw) prints
which is not a
Dispatchers.Default
thread.