From 024b474ce92ba66744af602ee67691cd11498485 Mon Sep 17 00:00:00 2001 From: generatedunixname1608173377072046 Date: Fri, 2 Oct 2026 17:35:44 -0700 Subject: [PATCH] Fix use-after-free race in RCTInstance callFunctionOnJSModule (#58816) Summary: Pull Request resolved: https://github.com/react/react-native/pull/58816 ## Changelog: [Internal] Reviewed By: zeyap Differential Revision: D122831570 fbshipit-source-id: 924ffa228cadc454069b268a0fd1c1126953ba33 --- .../runtime/platform/ios/ReactCommon/RCTInstance.mm | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm index d79f9029bb7..f4de8848613 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm @@ -171,10 +171,14 @@ using namespace facebook::react; - (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args { - if (_valid) { - _reactInstance->callFunctionOnModule( - [moduleName UTF8String], [method UTF8String], convertIdToFollyDynamic(args ? args : @[])); + // This is called from arbitrary threads, while -invalidate destroys _reactInstance on + // the JS thread, so checking _valid and dereferencing have to happen as one step. + std::lock_guard lock(_invalidationMutex); + if (!_valid || !_reactInstance) { + return; } + _reactInstance->callFunctionOnModule( + [moduleName UTF8String] ?: "", [method UTF8String] ?: "", convertIdToFollyDynamic(args ? args : @[])); } - (void)invalidate