Dias
10/16/2018, 5:06 PMlaunch {
launch {
throw RuntimeException()
}
while (true) {
println("123123")
}
}
I want parent launch to exit with an exception, but not sure how todekans
10/16/2018, 5:14 PMRuckus
10/16/2018, 5:16 PMyield()
in your while
loop.louiscad
10/16/2018, 5:28 PMwhile(isActive)
instead of while(true)
Ruckus
10/16/2018, 5:30 PMisActive
doesn't suspend, so if the `launch`ed coroutine never gets a chance to run, it will never throw and thus the parent will stay active. It all comes down to how the `launch`ed coroutine is dispatched.louiscad
10/16/2018, 5:32 PMwhile(true)
is just plain dangerous here as nothing can stop itRuckus
10/16/2018, 5:34 PMyield()
statement, the while (true)
block will indeed end. while (true)
isn't bad in and of itself, and is used in many places in kotlinx.coroutines. You just have to make sure you're using it correctly.louiscad
10/16/2018, 5:35 PMwhile(isActive)
should still work, and child scope should forward to parent scope, unless it's a CancellationException
while(isActive)
is more efficient than suspending and redispatching everytime with yield()
. That's why it's there on CoroutineScope
and CoroutineContext
Ruckus
10/16/2018, 5:37 PMfun main(args: Array<String>) = runBlocking<Unit> {
launch {
throw RuntimeException()
}
while (isActive) {
println("123123")
}
}
fun main(args: Array<String>) = runBlocking<Unit> {
launch {
throw RuntimeException()
}
while (true) {
println("123123")
yield()
}
}
elizarov
10/16/2018, 5:39 PMRuckus
10/16/2018, 5:40 PMlouiscad
10/16/2018, 6:01 PMThrowable
gildor
10/16/2018, 6:01 PMlouiscad
10/16/2018, 6:04 PMwhile(isActive)
and while(true)
called that have no suspension pointsDias
10/17/2018, 8:49 AMelizarov
10/17/2018, 8:50 AMDias
10/17/2018, 8:51 AM@Test
fun test() {
launch {
launch {
throw RuntimeException()
}
while (true) {
println("123123")
yield()
}
}
}
in a test, but it just passes. I was assuming it should failelizarov
10/17/2018, 8:52 AMlaunch
. If would print exception on the console if you give it time to.runBlocking
, then it would crashDias
10/17/2018, 8:53 AMelizarov
10/17/2018, 8:53 AMlaunch
is fire-and-forget. It cannot “throw exception”. It returns immediately.Dias
10/17/2018, 8:55 AMelizarov
10/17/2018, 8:56 AMrunBlocking
does.runBlocking
in your main thread and do as much other stuff in background. So, runBlocking
waits this background stuff, and if that background stuff fails, then runBlocking
fails.Dias
10/17/2018, 9:00 AMinit {
launch{
while (true) {
delay((expiryTime - BUFFER_SECONDS) * 1000)
refreshToken()
}
}
}
elizarov
10/17/2018, 9:00 AMDias
10/17/2018, 9:03 AMelizarov
10/17/2018, 9:07 AMDias
10/17/2018, 9:09 AMelizarov
10/17/2018, 9:12 AMGlobalScope.launch { ... }
and if that crashes then you’ll get stacktrace printed on console. I don’t see how you can crash main thread. Unfortunately, ktor does not (yet) expose its “application scope”, so there is no way (yet) to make the whole application crash on that errorDias
10/17/2018, 9:14 AMelizarov
10/17/2018, 9:20 AMGlobalScope
seems to be a perfect fit. It is designed for this kind of “global background tasks”.Dias
10/17/2018, 9:21 AMgildor
10/17/2018, 9:22 AMlaunch
with try/catch and handle how you wantelizarov
10/17/2018, 9:24 AMGlobalScope.launch(CoroutineExceptionHandler { /* handle here */ }) { /* code here */ }
. That will not only catch error in the code, but also if you launch any child coroutines from inside that code, then their errors will get handled, too.Dias
10/17/2018, 9:38 AMelizarov
10/17/2018, 9:39 AMDias
10/17/2018, 9:40 AMelizarov
10/17/2018, 9:41 AMGlobalScope
was the defaultDias
10/17/2018, 9:42 AM