You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@Test
funtimeoutAffectsClosing() = runBlocking {
val disp:CloseableCoroutineDispatcher= newSingleThreadContext("my-test")
val result = withContext(disp) {
// Disabling the inner timeout will make it work correctly
withTimeout(30.seconds) {
"Hello"
}
}
println(result) // Print "Hello"
disp.close()
println("Dispatcher closed: $disp") // takes 30 seconds to get here
}
The text was updated successfully, but these errors were encountered:
Before the change, the Worker is not notified about its work being
cancelled, due to no API being present for that.
We work around the issue by checking every 100 milliseconds whether
cancellation happened.
Fixes#3768
A work-around seems to be to use newFixedThreadPool(1, "my-test") instead of newSingleThreadContext("my-test"). This is working on Coroutines 1.6.4 -> 1.7.1.
After some more testing, it seems that the above fix hits another bug. Trying to use the closed dispatcher doesn't throw an IllegalStateException but rather seems to hang for the timeout that was defined (in another part of the code).
This problematic behavior only occurs on Coroutines 1.6.4. On 1.7.0 and 1.71 an IllegalStateException is correctly thrown immediately.
@Test
fun timeoutAffectsClosing() = runBlocking {
val disp: CloseableCoroutineDispatcher = newSingleThreadContext("my-test")
val result = withContext(disp) {
// Disabling the inner timeout will make it work correctly
withTimeout(30.seconds) {
"Hello"
}
}
println(result)
disp.close()
// Attempt to use the dispatcher result in the same timeout behaviour (i.e. needing to
// wait 30 seconds for `kotlin.IllegalStateException: Worker is already terminated`
// This is only a problem on Coroutines 1.6.4, and seems to be fixed in 1.7.0.
withContext(disp) {
// Do nothing
}
println("Done")
}
Reported in the Kotlin Slack originally.
Simplified reproducer:
The text was updated successfully, but these errors were encountered: