From 63ffa7c2f6035efb4fa3c6f5437372965be31fb0 Mon Sep 17 00:00:00 2001 From: Dan Caspi Date: Tue, 6 Jun 2017 11:30:40 -0700 Subject: [PATCH] Fixing a race condition that may be caused by the reloadable executor. Reviewed By: javache Differential Revision: D5139379 fbshipit-source-id: 24aa820caacfe3780d0e5a2f5868cdc46cefc3fb --- ReactCommon/cxxreact/JSCExecutor.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index be014ff4d..b1044d7cd 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -79,11 +79,13 @@ inline JSObjectCallAsFunctionCallback exceptionWrapMethod() { JSValueRef *exception) { try { auto executor = Object::getGlobalObject(ctx).getPrivate(); - return (executor->*method)(argumentCount, arguments); + if (executor && executor->getJavaScriptContext()) { // Executor not invalidated + return (executor->*method)(argumentCount, arguments); + } } catch (...) { *exception = translatePendingCppExceptionToJSError(ctx, function); - return Value::makeUndefined(ctx); } + return Value::makeUndefined(ctx); } }; @@ -100,11 +102,13 @@ inline JSObjectGetPropertyCallback exceptionWrapMethod() { JSValueRef *exception) { try { auto executor = Object::getGlobalObject(ctx).getPrivate(); - return (executor->*method)(object, propertyName); + if (executor && executor->getJavaScriptContext()) { // Executor not invalidated + return (executor->*method)(object, propertyName); + } } catch (...) { *exception = translatePendingCppExceptionToJSError(ctx, object); - return Value::makeUndefined(ctx); } + return Value::makeUndefined(ctx); } }; @@ -256,17 +260,19 @@ void JSCExecutor::initOnJSVMThread() throw(JSException) { } void JSCExecutor::terminateOnJSVMThread() { + JSGlobalContextRef context = m_context; + m_context = nullptr; + Object::getGlobalObject(context).setPrivate(nullptr); m_nativeModules.reset(); #ifdef WITH_INSPECTOR - if (canUseInspector(m_context)) { + if (canUseInspector(context)) { IInspector* pInspector = JSC_JSInspectorGetInstance(true); - pInspector->unregisterGlobalContext(m_context); + pInspector->unregisterGlobalContext(context); } #endif - JSC_JSGlobalContextRelease(m_context); - m_context = nullptr; + JSC_JSGlobalContextRelease(context); } #ifdef WITH_FBJSCEXTENSIONS