-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Coroutines do not log uncaught exceptions in Android #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You correctly describe the current behavior of What is your question or suggestion? |
My suggestion is that, for Android, I believe it's more beneficial to have the uncaught exception pre-handler be invoked as well as the Thread's While it is possible for developers to manually fix this, I believe it would be better for coroutines, on Android, to either rethrow the exception so the system will log it, or reimplement Android's pre-handler so that the exception is easier to track down. |
@wardellbagby How this can be implemented, given the constraint that We can provide some ready-to-use implementation as a part of |
The pre-handler concept is completely hidden by the Android system, and it can't be invoked directly. You can take a look at where it's set by Thread: https://android.googlesource.com/platform/libcore/+/oreo-release/ojluni/src/main/java/java/lang/Thread.java#1893 and https://android.googlesource.com/platform/libcore/+/oreo-release/ojluni/src/main/java/java/lang/Thread.java#1943 I think in a perfect world, the default coroutine contexts (e.g., |
@wardellbagby Would you suggest as to how this can be implement? How do we know what logging framework to use given there are so many different logging frameworks out there? Actually, the variety of logging frameworks was the chief reason to support |
Hmm. That's a good point; I didn't think much of the different logging frameworks. I think in this case, and only this case specifically where the However, the downsides would be that developers would have to manually specify that coroutine context and might still be confused if they aren't aware of it. So while it's a solution, it'll have similar pitfalls to what is currently happening. I would say it could be made so that the In light of all of this information, I'm changing my mind. The best solution might just be to add this quirk into the documentation so that fellow developers are aware of it. There are at least two ways to fix this developer side, and neither of them are difficult to implement. For the sake of any future web crawlers who come across this looking for a code sample, I'll provide the two I'm aware of.
val currentUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler({ thread, exception ->
Log.println(Log.ERROR, thread.name, Log.getStackTraceString(exception))
currentUncaughtExceptionHandler.uncaughtException(thread, exception)
})
val handler = CoroutineExceptionHandler { _, ex ->
Log.println(Log.ERROR, LOG_TAG, Log.getStackTraceString(ex))
} or val handler = CoroutineExceptionHandler { _, ex ->
throw ex
} launch(CommonPool + handler) {
//your code here
} |
An alternative solution is to include an Android-specific hack into It can be made safer by using services framework to register additional exception handler, with |
If you believe that's a good idea, I'm definitely 100% for it! |
|
I've just spent hours debugging a simple issue. Whats the current best practice for getting uncaught exceptions on android? |
@saied89 Do you have |
@elizarov |
https://github.com/wardellbagby/KotlinExceptions
I created a sample app to show what I mean.
When a coroutine has an uncaught exception, it's manually calling the Thread
UncaughtExceptionHandler
. A consequence of that is Android's uncaught exception pre-handler that handles logging isn't invoked.The README of the linked project goes more in depth.
The text was updated successfully, but these errors were encountered: