From 1efde52bd8572a68743448d860222a88bc850385 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Tue, 1 Oct 2024 07:49:39 -0700 Subject: [PATCH] Throw JS exception when calling a method if bufferedRuntimeExecutor is not initialized (#46735) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46735 We had some crash on Android where we call [`callFunctionOnModule`](https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp#L258) when the `bufferedRuntimeExecutor_` might not be initialized. This can happen when navigating away from RN surface and quickly navigate to another one or across refreshes. There could be a scheduled JS function from the previous surface/instance that might try to call a native module while the new instance is being created. This change prevent the crash and replace it with a soft crash, that should show a RedBox on the screen. ## Changelog [Internal] - Throw JS exception when calling a method if buffereRuntimeExecutor is not initialized ## Facebook Have a look at T201983945 that generated the crash report Reviewed By: cortinico Differential Revision: D63638633 --- .../ReactCommon/react/runtime/ReactInstance.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp index e4cee80023fc..78eccf35f907 100644 --- a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp @@ -255,6 +255,12 @@ void ReactInstance::callFunctionOnModule( const std::string& moduleName, const std::string& methodName, folly::dynamic&& args) { + if (bufferedRuntimeExecutor_ == nullptr) { + LOG(ERROR) + << "Calling callFunctionOnModule with null BufferedRuntimeExecutor"; + return; + } + bufferedRuntimeExecutor_->execute([this, moduleName = moduleName, methodName = methodName,