Skip to content

Commit 8d8d7ab

Browse files
authored
Fix multiple exception handler invocation in Android (#3056)
The AndroidExceptionPreHandler was changed in commit 5ea9339 to not invoke the uncaught exception pre-handler on Android Pie and above, and to instead invoke the thread's uncaught exception handler directly on those versions, as Android Pie had added logic in the default uncaught exception handler to ensure that the pre-handler would be invoked. However, the uncaught exception handler is already being invoked for all unhandled coroutine exceptions, so this change in the AndroidExceptionPreHandler caused the uncaught exception handler to be invoked twice in Android Pie and above. Since the default uncaught exception handler implementation in Android kills the app immediately, this normally wouldn't make a difference. However, if an app sets a custom uncaught exception handler (either at the thread or global level) that doesn't always kill the app, then the duplicate invocations might cause undesired or wrong behaviour. This is now fixed by removing the uncaught exception handler invocation from AndroidExceptionPreHandler. Since the pre-handler was introduced in Android Oreo, I have also added a minimum SDK check for Android Oreo as well. Also edited the documentation in the comments to add more details and context.
1 parent ec9be92 commit 8d8d7ab

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

ui/kotlinx-coroutines-android/src/AndroidExceptionPreHandler.kt

+12-11
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,21 @@ internal class AndroidExceptionPreHandler :
3434

3535
override fun handleException(context: CoroutineContext, exception: Throwable) {
3636
/*
37-
* If we are on old SDK, then use Android's `Thread.getUncaughtExceptionPreHandler()` that ensures that
38-
* an exception is logged before crashing the application.
37+
* Android Oreo introduced private API for a global pre-handler for uncaught exceptions, to ensure that the
38+
* exceptions are logged even if the default uncaught exception handler is replaced by the app. The pre-handler
39+
* is invoked from the Thread's private dispatchUncaughtException() method, so our manual invocation of the
40+
* Thread's uncaught exception handler bypasses the pre-handler in Android Oreo, and uncaught coroutine
41+
* exceptions are not logged. This issue was addressed in Android Pie, which added a check in the default
42+
* uncaught exception handler to invoke the pre-handler if it was not invoked already (see
43+
* https://android-review.googlesource.com/c/platform/frameworks/base/+/654578/). So the issue is present only
44+
* in Android Oreo.
3945
*
40-
* Since Android Pie default uncaught exception handler always ensures that exception is logged without interfering with
41-
* pre-handler, so reflection hack is no longer needed.
42-
*
43-
* See https://android-review.googlesource.com/c/platform/frameworks/base/+/654578/
46+
* We're fixing this by manually invoking the pre-handler using reflection, if running on an Android Oreo SDK
47+
* version (26 and 27).
4448
*/
45-
val thread = Thread.currentThread()
46-
if (Build.VERSION.SDK_INT >= 28) {
47-
thread.uncaughtExceptionHandler.uncaughtException(thread, exception)
48-
} else {
49+
if (Build.VERSION.SDK_INT in 26..27) {
4950
(preHandler()?.invoke(null) as? Thread.UncaughtExceptionHandler)
50-
?.uncaughtException(thread, exception)
51+
?.uncaughtException(Thread.currentThread(), exception)
5152
}
5253
}
5354
}

0 commit comments

Comments
 (0)