From 32223b4ebcbe62991f2688c0d1fdcf7e3d4139b7 Mon Sep 17 00:00:00 2001 From: Iain Berliner Date: Sun, 12 Feb 2023 21:30:23 -0500 Subject: [PATCH] Workaround Modified the file PyTorchCoreJSI.mm to repeatedly try installing \_\_torchlive\_\_ if RCTCxxBridge.runtime is null, rather than just giving up. Modfied the file install-jsi.ts to run all of it's side-effect logic in a function that does a "return;" if the variable "PlayTorchJSIModule" is null, instead of throwing an error. Also added an extra side effect that adds a function to the global object, that can be used to await the existence of \_\_torchlive\_\_, before calling other PyTorch code. --- .../ios/Jsi/PyTorchCoreJSI.mm | 4 ++ react-native-pytorch-core/src/install-jsi.ts | 67 ++++++++++++------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/react-native-pytorch-core/ios/Jsi/PyTorchCoreJSI.mm b/react-native-pytorch-core/ios/Jsi/PyTorchCoreJSI.mm index d2aa1aa1a..108b70930 100644 --- a/react-native-pytorch-core/ios/Jsi/PyTorchCoreJSI.mm +++ b/react-native-pytorch-core/ios/Jsi/PyTorchCoreJSI.mm @@ -51,6 +51,10 @@ - (void)install { torchlive::install(*(facebook::jsi::Runtime *)cxxBridge.runtime, runtimeExecutor); } + else { + [NSThread sleepForTimeInterval:0.1] + [self install] + } } @end diff --git a/react-native-pytorch-core/src/install-jsi.ts b/react-native-pytorch-core/src/install-jsi.ts index a6aab3a2b..d8e660e10 100644 --- a/react-native-pytorch-core/src/install-jsi.ts +++ b/react-native-pytorch-core/src/install-jsi.ts @@ -20,32 +20,49 @@ declare global { var __torchlive__: object | undefined; } -if (global.__torchlive__ == null) { - const PlayTorchJSIModule = NativeModules.PyTorchCoreJSI; +function TryInstallJSI() { + if (global.__torchlive__ == null) { + const PlayTorchJSIModule = NativeModules.PyTorchCoreJSI; - if (PlayTorchJSIModule == null) { - throw new Error( - 'PlayTorchJSIModule not found. Maybe try rebuilding the app.', - ); - } + if (PlayTorchJSIModule == null) { + /* + throw new Error( + 'PlayTorchJSIModule not found. Maybe try rebuilding the app.', + ); + */ + //return instead of throwing error and crashing + return; + } - // Check if we are running on-device (JSI) - if (global.nativeCallSyncHook == null || PlayTorchJSIModule.install == null) { - throw new Error( - 'Failed to install react-native-pytorch-core: React Native is not running on-device. The PlayTorchJSIModule can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.', - ); - } + // Check if we are running on-device (JSI) + if (global.nativeCallSyncHook == null || PlayTorchJSIModule.install == null) { + throw new Error( + 'Failed to install react-native-pytorch-core: React Native is not running on-device. The PlayTorchJSIModule can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.', + ); + } - // Call the synchronous blocking install() function - const result = PlayTorchJSIModule.install(); - if (result !== true) - throw new Error( - `Failed to install react-native-pytorch-core: The native PlayTorchJSIModule could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`, - ); - - // Check again if the constructor now exists. If not, throw an error. - if (global.__torchlive__ == null) - throw new Error( - 'Failed to install react-native-pytorch-core, the native initializer function does not exist. Are you trying to use PlayTorchJSIModule from different JS Runtimes?', - ); + // Call the synchronous blocking install() function + const result = PlayTorchJSIModule.install(); + if (result !== true) + throw new Error( + `Failed to install react-native-pytorch-core: The native PlayTorchJSIModule could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`, + ); + + // Check again if the constructor now exists. If not, throw an error. + if (global.__torchlive__ == null) + throw new Error( + 'Failed to install react-native-pytorch-core, the native initializer function does not exist. Are you trying to use PlayTorchJSIModule from different JS Runtimes?', + ); + } } +TryInstallJSI() + +//Use this function to ensure __torchlive__ exists before calling other PyTorch code +global.WaitForTorchLive = async () => { return new Promise( async (resolve, reject) => { + if (global.__torchlive__ != null) { + resolve(); + } + else { + setTimeout(async () => {await WaitForTorchLive(); resolve()}, 100) + } +})}