Hi,
I’m migrating my server-side code from coroutines 0.25.1 to 0.30.2. Excuse my frankness, but I’m absolutely baffled by the new behavior and unable to get the desired behavior.
Here’s the code:
@Test
fun testException() {
runBlocking {
try {
val x = async(context = coroutineContext) {
throw Exception()
10
}
println("Awaiting...")
x.await()
} catch (e: Exception) {
println("Here")
delay(1000)
println("What?!")
}
}
}
In 0.25.1 this prints:
Awaiting...
Here
What?!
as I would intuitively expect.
Going to 0.30.2:
Awaiting...
Here
java.lang.Exception
It looks like the exception inside try is somehow preventing delay inside catch to start.
Some observations:
- Using
GlobalScope.asyncdoes not seem to help (same output). - Using
GlobalScope.asyncwithoutcontextparameter will yield the desired behavior. Why the difference? - Even if I don’t
await, the exception will somehow popup.
What I’m trying to accomplish:
- I want to start IO computation in parallel in the same thread (think Node.JS or Vert.x execution model).
- I want to be able to catch this exception and handle it, which might include calling suspend functions and starting additional parallel work (
asynccalls). - I want exception to propagate on
await. I don’t want exception to popup if I don’t await at all.
How would I do this after the redesign in 0.30.2?