Kris Wong

    Kris Wong

    2 years ago
    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

    2 years ago
    do you have a code sample?
    Kris Wong

    Kris Wong

    2 years ago
    i can distill it down into a basic example
    @Test fun testSomething() = runBlocking {
        val deferred = CompletableDeferred<Unit>()
        CoroutineWorker.execute {
            ...
            deferred.complete(Unit)
        }
        deferred.await()
    }
    it deadlocks on
    deferred.complete
    b

    basher

    2 years ago
    is this using native mt branch or something?
    Kris Wong

    Kris Wong

    2 years ago
    no
    b

    basher

    2 years ago
    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
    Kris Wong

    Kris Wong

    2 years ago
    any problems from that would throw an exception, would it not?
    b

    basher

    2 years ago
    and then your main thread never finishes waiting on the deferred
    yes but exceptions in Worker do not crash the app
    they just log
    Kris Wong

    Kris Wong

    2 years ago
    hmmm
    b

    basher

    2 years ago
    that's why CoroutineWorker has a special mechanism for capturing Worker exceptions so that you can manually crash, if that's what you want
    Kris Wong

    Kris Wong

    2 years ago
    i've always gotten a stack trace when i've had issues with freezing in the past
    b

    basher

    2 years ago
    you won't get the exception when you freeze, you'll get it later in the background when you try to complete the deferred
    Kris Wong

    Kris Wong

    2 years ago
    understood
    logs might show the exception, assuming my guess is correct
    Kris Wong

    Kris Wong

    2 years ago
    i'll give it a shot
    b

    basher

    2 years ago
    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
    Kris Wong

    Kris Wong

    2 years ago
    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
    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

    2 years ago
    What's COROUTINE_SUSPENDED?
    Semaphore also can't be frozen, if you mean the one from coroutines lib
    Kris Wong

    Kris Wong

    2 years ago
    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

    2 years ago
    Huh 👌
    Kris Wong

    Kris Wong

    2 years ago
    ¯_(ツ)_/¯