Skip to content

Commit

Permalink
Fix crash caused by using ShareableHandle in multiple remote runtim…
Browse files Browse the repository at this point in the history
…es (#6796)

## Summary
Fixes
Expensify/react-native-live-markdown#574. See
root cause analysis in
Expensify/react-native-live-markdown#574 (comment).

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:

<details>
<summary>EmptyExample.tsx</summary>

```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 (
    <View style={styles.container}>
      <Text>Hello world!</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});
```

</details>
  • Loading branch information
tomekzaw authored and tjzel committed Dec 13, 2024
1 parent 8b30dbf commit 5d7305a
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5d7305a

Please sign in to comment.