From f99579adaa1d312ba0ee4a71badba84e57f197e6 Mon Sep 17 00:00:00 2001 From: Andy Street Date: Fri, 26 Feb 2016 10:57:55 -0800 Subject: [PATCH] Try/catch exceptions in JSCExecutor for debugging crash Reviewed By: lexs Differential Revision: D2982534 fb-gh-sync-id: 4eda4116cd60e2e46771ecddefc1d27eee761762 shipit-source-id: 4eda4116cd60e2e46771ecddefc1d27eee761762 --- ReactAndroid/src/main/jni/react/JSCExecutor.cpp | 16 ++++++++++------ ReactAndroid/src/main/jni/react/Platform.cpp | 8 ++++++-- ReactAndroid/src/main/jni/react/Platform.h | 9 +++++++-- ReactAndroid/src/main/jni/react/jni/OnLoad.cpp | 3 +++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp index ad9f3c05a..56af90b9b 100644 --- a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp +++ b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp @@ -126,13 +126,17 @@ JSCExecutor::JSCExecutor( } JSCExecutor::~JSCExecutor() { - *m_isDestroyed = true; - if (m_messageQueueThread->isOnThread()) { - terminateOnJSVMThread(); - } else { - m_messageQueueThread->runOnQueueSync([this] () { + try { + *m_isDestroyed = true; + if (m_messageQueueThread->isOnThread()) { terminateOnJSVMThread(); - }); + } else { + m_messageQueueThread->runOnQueueSync([this] () { + terminateOnJSVMThread(); + }); + } + } catch (...) { + Exceptions::handleUncaughtException(); } } diff --git a/ReactAndroid/src/main/jni/react/Platform.cpp b/ReactAndroid/src/main/jni/react/Platform.cpp index 03edca72c..3e1c700b7 100644 --- a/ReactAndroid/src/main/jni/react/Platform.cpp +++ b/ReactAndroid/src/main/jni/react/Platform.cpp @@ -20,10 +20,14 @@ LoadScriptFromAssets loadScriptFromAssets; namespace PerfLogging { InstallNativeHooks installNativeHooks; -} +}; namespace JSLogging { JSCNativeHook nativeHook = nullptr; -} +}; + +namespace Exceptions { +HandleUncaughtException handleUncaughtException; +}; } } diff --git a/ReactAndroid/src/main/jni/react/Platform.h b/ReactAndroid/src/main/jni/react/Platform.h index c5c1c5043..5ad664dbf 100644 --- a/ReactAndroid/src/main/jni/react/Platform.h +++ b/ReactAndroid/src/main/jni/react/Platform.h @@ -34,7 +34,7 @@ extern LoadScriptFromAssets loadScriptFromAssets; namespace PerfLogging { using InstallNativeHooks = std::function; extern InstallNativeHooks installNativeHooks; -} +}; namespace JSLogging { using JSCNativeHook = JSValueRef (*) ( @@ -44,6 +44,11 @@ namespace JSLogging { size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception); extern JSCNativeHook nativeHook; -} +}; + +namespace Exceptions { + using HandleUncaughtException = std::function; + extern HandleUncaughtException handleUncaughtException; +}; } } diff --git a/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp b/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp index 5081bd67f..fad41e7cc 100644 --- a/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp +++ b/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp @@ -865,6 +865,9 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { return std::unique_ptr( JMessageQueueThread::currentMessageQueueThread().release()); }; + Exceptions::handleUncaughtException = [] () { + translatePendingCppExceptionToJavaException(); + }; PerfLogging::installNativeHooks = addNativePerfLoggingHooks; JSLogging::nativeHook = nativeLoggingHook;