Skip to content

Commit a01286f

Browse files
committed
fix(Worklets): shareable deadlock (#9321)
## Summary Fixing the code that would run into a deadlock present in Hermes: - https://github.com/facebook/react-native/issues/56674 Fortunately we can simply remove claiming the HostRuntime in the Shareable as it doesn't need the runtime to be cleaned. ## Test plan The following code no longer deadlocks with Reanimated. Compile and run in Release, sometimes you have to wait a couple of minutes before the deadlock manifests. <details><summary>Code</summary> <p> ```tsx import React from 'react'; import { StyleSheet } from 'react-native'; import { ScrollView } from 'react-native-gesture-handler'; import Animated, { useAnimatedStyle, useSharedValue, withTiming, } from 'react-native-reanimated'; export default function Repro() { return ( <ScrollView contentContainerStyle={styles.container}> {new Array(1024).fill(0).map((_, i) => ( <AnimatorWrapper key={i} /> ))} </ScrollView> ); } function AnimatorWrapper() { const [display, setDisplay] = React.useState(true); React.useEffect(() => { const timeout = setInterval( () => { setDisplay((display) => !display); }, Math.random() * 500 + 300 ); return () => clearInterval(timeout); }, []); if (!display) { return null; } return <Animator />; } function Animator() { const opacitySv = useSharedValue(0); const widthSv = useSharedValue(10); const heightSv = useSharedValue(10); React.useEffect(() => { opacitySv.value = withTiming(1, { duration: 100 }); widthSv.value = withTiming(100, { duration: 100 }); heightSv.value = withTiming(20, { duration: 100 }); }, [opacitySv, widthSv, heightSv]); const animatedStyle = useAnimatedStyle(() => { return { opacity: opacitySv.value, width: widthSv.value, height: heightSv.value, }; }); return <Animated.View style={[styles.box, animatedStyle]} />; } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', gap: 2, }, box: { backgroundColor: 'blue', }, }); ``` </p> </details>
1 parent 90d22a0 commit a01286f

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

  • packages/react-native-worklets/Common/cpp/worklets/SharedItems

packages/react-native-worklets/Common/cpp/worklets/SharedItems/Shareable.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ Shareable::Shareable(
3737
}
3838

3939
Shareable::~Shareable() {
40-
const auto strongHostRuntime = weakHostRuntime_.lock();
41-
if (strongHostRuntime && hostValue_) {
42-
strongHostRuntime->runSync([this](jsi::Runtime &rt) { hostValue_.reset(); });
43-
} else {
44-
hostValue_.release();
40+
if (hostValue_) {
41+
const auto strongHostRuntime = weakHostRuntime_.lock();
42+
if (strongHostRuntime) {
43+
hostValue_.reset();
44+
} else {
45+
hostValue_.release();
46+
}
4547
}
4648
}
4749

0 commit comments

Comments
 (0)