From 067c989b4841d55c521d7b21534e9609d9d2a0a7 Mon Sep 17 00:00:00 2001 From: Blake Friedman Date: Thu, 12 Oct 2023 10:09:55 -0700 Subject: [PATCH] make runtime reference thread safe on tickleJs call Summary: The reference to runtime assumes the queue will ensure references to runtime are valid when invoked. This isn't the case if you create a breakpoint, Hermes hit that breakpoint and your refresh the app. This consistently will crash the app. The fix is to not assument this, similar to ReactCommon/react/runtime/hermes/HermesInstance.cpp Reviewed By: javache Differential Revision: D50225678 fbshipit-source-id: b45cae1f5f687bc8c699fd74b187376a547012c5 --- .../hermes/executor/HermesExecutorFactory.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp b/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp index 8ae8766f5e3e84..58b56e83f0cc45 100644 --- a/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp +++ b/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp @@ -43,13 +43,16 @@ class HermesExecutorRuntimeAdapter } void tickleJs() override { - // The queue will ensure that runtime_ is still valid when this - // gets invoked. - thread_->runOnQueue([&runtime = runtime_]() { - auto func = - runtime->global().getPropertyAsFunction(*runtime, "__tickleJs"); - func.call(*runtime); - }); + thread_->runOnQueue( + [weakRuntime = std::weak_ptr(runtime_)]() { + auto runtime = weakRuntime.lock(); + if (!runtime) { + return; + } + jsi::Function func = + runtime->global().getPropertyAsFunction(*runtime, "__tickleJs"); + func.call(*runtime); + }); } private: