From 049fa26fd9c2407bbc138473067450ce205458de Mon Sep 17 00:00:00 2001 From: mvicsokolova Date: Wed, 30 Jun 2021 12:23:44 +0300 Subject: [PATCH] Terminate the process in case of an unhandled exception Fixes #2398 --- .../native/src/CoroutineExceptionHandlerImpl.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/kotlinx-coroutines-core/native/src/CoroutineExceptionHandlerImpl.kt b/kotlinx-coroutines-core/native/src/CoroutineExceptionHandlerImpl.kt index 389c724423..ed2856a829 100644 --- a/kotlinx-coroutines-core/native/src/CoroutineExceptionHandlerImpl.kt +++ b/kotlinx-coroutines-core/native/src/CoroutineExceptionHandlerImpl.kt @@ -5,11 +5,14 @@ package kotlinx.coroutines import kotlin.coroutines.* +import kotlin.system.exitProcess +import kotlin.native.* +import kotlin.native.concurrent.* internal actual fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable) { - // log exception -// println("Exception in \"${Worker.current}\"") -// exception.printStackTrace() -// todo: printing exception does not make it easy to debug (no source location), so let it crash instead - throw exception + // Use unhandled exception hook if it is set, otherwise print the stacktrace + val hook = setUnhandledExceptionHook({ _: Throwable -> }.freeze()) + if (hook != null) hook(exception) else exception.printStackTrace() + // Terminate the process + exitProcess(-1) }