https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
k

Kris Wong

02/13/2020, 5:13 PM
so the above issue is actually rooted in using
CoroutineWorker.execute
with
CompletableDeferred
. anyone have an idea why completing the deferred might deadlock when used from
execute
?
b

basher

02/13/2020, 5:24 PM
do you have a code sample?
k

Kris Wong

02/13/2020, 5:27 PM
i can distill it down into a basic example
Copy code
@Test fun testSomething() = runBlocking {
    val deferred = CompletableDeferred<Unit>()
    CoroutineWorker.execute {
        ...
        deferred.complete(Unit)
    }
    deferred.await()
}
it deadlocks on
deferred.complete
b

basher

02/13/2020, 5:33 PM
is this using native mt branch or something?
k

Kris Wong

02/13/2020, 5:33 PM
no
b

basher

02/13/2020, 5:33 PM
that's going to freeze
deferred
then, which could do bad things
there's probably internal state you're freezing
it may not be deadlocking as much as crashing on a bg thread and dying silently
k

Kris Wong

02/13/2020, 5:34 PM
any problems from that would throw an exception, would it not?
b

basher

02/13/2020, 5:34 PM
and then your main thread never finishes waiting on the deferred
yes but exceptions in Worker do not crash the app
they just log
k

Kris Wong

02/13/2020, 5:34 PM
hmmm
b

basher

02/13/2020, 5:35 PM
that's why CoroutineWorker has a special mechanism for capturing Worker exceptions so that you can manually crash, if that's what you want
k

Kris Wong

02/13/2020, 5:35 PM
i've always gotten a stack trace when i've had issues with freezing in the past
b

basher

02/13/2020, 5:36 PM
you won't get the exception when you freeze, you'll get it later in the background when you try to complete the deferred
k

Kris Wong

02/13/2020, 5:36 PM
understood
logs might show the exception, assuming my guess is correct
k

Kris Wong

02/13/2020, 5:40 PM
i'll give it a shot
👍 1
b

basher

02/13/2020, 5:41 PM
Best documentation I can find about worker uncaught exceptions is just the param for Worker.start: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/-worker/start.html
IIRC, it was just something we observed when testing things for our first App Store release
k

Kris Wong

02/13/2020, 5:44 PM
the handler is not receiving an exception in this case
well, whatever the actual root cause is, seems like I am going to be rolling my own kind of Deferred to fix this, so I might as well just do that
👍 1
so strange, now that's fixed (something about
COROUTINE_SUSPENDED
seems to cause the dead lock), but now I have the same behavior with
Semaphore
b

basher

02/13/2020, 8:50 PM
What's COROUTINE_SUSPENDED?
Semaphore also can't be frozen, if you mean the one from coroutines lib
k

Kris Wong

02/13/2020, 8:53 PM
it's a special value used with
suspendCoroutine
i replaced semaphore with the same class I used in place of deferred
now it's all working
b

basher

02/13/2020, 8:54 PM
Huh 👌
k

Kris Wong

02/13/2020, 8:55 PM
¯\_(ツ)_/¯