Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash caused by using ShareableHandle in multiple remote runtimes #6796

Merged
merged 2 commits into from
Dec 9, 2024

Conversation

tomekzaw
Copy link
Member

@tomekzaw tomekzaw commented Dec 8, 2024

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:

EmptyExample.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',
  },
});

@tomekzaw tomekzaw requested review from piaskowyk and tjzel December 8, 2024 21:23
@tomekzaw
Copy link
Member Author

tomekzaw commented Dec 8, 2024

Please confirm that we don't need any additional synchronization involving initializationMutex_ prior to merging.

cc @tjzel who touched this code some time ago

Copy link
Collaborator

@tjzel tjzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tomekzaw tomekzaw added this pull request to the merge queue Dec 9, 2024
Merged via the queue into main with commit 73ca5ea Dec 9, 2024
17 checks passed
@tomekzaw tomekzaw deleted the @tomekzaw/fix-shareable-handle-multiple-runtimes branch December 9, 2024 09:34
tomekzaw added a commit that referenced this pull request Dec 9, 2024
…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>
@tomekzaw tomekzaw mentioned this pull request Dec 9, 2024
tomekzaw added a commit that referenced this pull request Dec 9, 2024
…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>
@piaskowyk
Copy link
Member

Changes look ok, and probably we also need to add that logic to RetainingShareable. What do you think? 🤔

@tomekzaw
Copy link
Member Author

tomekzaw commented Dec 9, 2024

Looks like RetainingShareable already supports multiple runtimes. There's a comparison of &rt with primaryRuntime_ and secondaryRuntime_ and there's a call to BaseClass::toJSValue(rt) that generates a new instance for yet another runtime:

template <typename BaseClass>
jsi::Value RetainingShareable<BaseClass>::toJSValue(jsi::Runtime &rt) {
if (&rt == primaryRuntime_) {
// TODO: it is suboptimal to generate new object every time getJS is
// called on host runtime – the objects we are generating already exists
// and we should possibly just grab a hold of such object and use it here
// instead of creating a new JS representation. As far as I understand the
// only case where it can be realistically called this way is when a
// shared value is created and then accessed on the same runtime
return BaseClass::toJSValue(rt);
}
if (secondaryValue_ == nullptr) {
auto value = BaseClass::toJSValue(rt);
secondaryValue_ = std::make_unique<jsi::Value>(rt, value);
secondaryRuntime_ = &rt;
return value;
}
if (&rt == secondaryRuntime_) {
return jsi::Value(rt, *secondaryValue_);
}
return BaseClass::toJSValue(rt);
}

tjzel pushed a commit that referenced this pull request Dec 13, 2024
…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>
tjzel pushed a commit that referenced this pull request Dec 13, 2024
…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>
tjzel pushed a commit that referenced this pull request Dec 13, 2024
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

App crashes if parser uses a regex that is used in another worklet
3 participants