From 8e7c942cfaa518443752b10be73a2321b701f95b Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Mon, 9 Dec 2024 10:33:52 +0100 Subject: [PATCH] Fix crash caused by using `ShareableHandle` in multiple remote runtimes (#6796) ## Summary Fixes https://github.com/Expensify/react-native-live-markdown/issues/574. See root cause analysis in https://github.com/Expensify/react-native-live-markdown/issues/574#issuecomment-2526377216. This PR fixes a crash caused by calling `ShareableHandle::toJSValue` with second remote runtime after initializing `remoteValue_` with a `jsi::Value` belonging to the first remote runtime. I assume that this is a rare scenario so we only memoize the value for the first remote runtime and we recreate the value for all subsequent runtimes. ## Test plan Reproduction:
EmptyExample.tsx ```tsx import { Text, StyleSheet, View } from 'react-native'; import React from 'react'; import { createWorkletRuntime, runOnRuntime, useAnimatedStyle, } from 'react-native-reanimated'; const regex = /\d/; const workletRuntime = createWorkletRuntime('another'); export default function EmptyExample() { useAnimatedStyle(() => { console.log('useAnimatedStyle', String(regex)); return {}; }); runOnRuntime(workletRuntime, () => { 'worklet'; console.log('runOnRuntime', String(regex)); return {}; })(); return ( Hello world! ); } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, }); ```
--- .../Common/cpp/worklets/SharedItems/Shareables.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/react-native-reanimated/Common/cpp/worklets/SharedItems/Shareables.cpp b/packages/react-native-reanimated/Common/cpp/worklets/SharedItems/Shareables.cpp index 12216edfeb4..f8c5bfc1366 100644 --- a/packages/react-native-reanimated/Common/cpp/worklets/SharedItems/Shareables.cpp +++ b/packages/react-native-reanimated/Common/cpp/worklets/SharedItems/Shareables.cpp @@ -289,7 +289,12 @@ jsi::Value ShareableHandle::toJSValue(jsi::Runtime &rt) { remoteRuntime_ = &rt; } } - return jsi::Value(rt, *remoteValue_); + if (&rt == remoteRuntime_) { + return jsi::Value(rt, *remoteValue_); + } + auto initObj = initializer_->toJSValue(rt); + return getValueUnpacker(rt).call( + rt, initObj, jsi::String::createFromAscii(rt, "Handle")); } jsi::Value ShareableString::toJSValue(jsi::Runtime &rt) {