https://kotlinlang.org logo
#android
Title
# android
m

Martin Nordholts

01/14/2020, 1:01 PM
Hi! A coroutine causes a FATAL EXCEPTION but does not give any stack trace or clue about what coroutine it is:
Copy code
01-14 11:40:10.990  4508  4508 E AndroidRuntime: FATAL EXCEPTION: main @coroutine#4
01-14 11:40:10.990  4508  4508 E AndroidRuntime: Process: com.readly.client, PID: 4508
I am running with
Copy code
System.setProperty(kotlinx.coroutines.DebugKt.DEBUG_PROPERTY_NAME, kotlinx.coroutines.DebugKt.DEBUG_PROPERTY_VALUE_ON)
but still get the above silent fail. What is the best way to find what coroutine this is? (Without using “brute force”)
t

tseisel

01/14/2020, 1:15 PM
That's rather strange, because on Android the default behavior is to crash the app when a top level coroutine fails. Begin by checking all coroutines that are launched with
GlobalScope
, those are the most susceptible to fail silently.
m

Martin Nordholts

01/14/2020, 1:16 PM
The app does crash 💡
Begin by checking all coroutines that are launched with
GlobalScope
, those are the most susceptible to fail silently.
Thanks for the tip which certainly would work, but I would like to find a way that does not use brute force, because that doesn’t scale well (e.g. to a codebase that has a ton of coroutines)
t

tseisel

01/14/2020, 1:22 PM
What you mean is that the app crashes, but you don't have a full stacktrace describing where and why it failed ?
m

Martin Nordholts

01/14/2020, 1:37 PM
exactly or more precisely, the why is not that important, but the where is (at the moment)
t

tseisel

01/14/2020, 2:20 PM
One possibility for fast debugging is to install a
CoroutineExceptionHandler
that will perform the necessary logging (printing a stacktrace) : https://kotlinlang.org/docs/reference/coroutines/exception-handling.html#coroutineexceptionhandler
m

Martin Nordholts

01/14/2020, 2:33 PM
Thanks for the tip, I tried
Copy code
Thread.setDefaultUncaughtExceptionHandler { t: Thread, e: Throwable ->
            Log.e("foo", "bar", e)
        }
and now I can get a stack trace 👍
51 Views