Hi! A coroutine causes a FATAL EXCEPTION but does ...
# android
m
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
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
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
What you mean is that the app crashes, but you don't have a full stacktrace describing where and why it failed ?
m
exactly or more precisely, the why is not that important, but the where is (at the moment)
t
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
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 👍