Pinch in/out crashes with 'Maximum call stack size exceeded' under Reanimated 4.5 (usePinchCommons reset self-write)
Summary
Pinching in/out repeatedly on a Gallery (or any pinch surface) crashes with Maximum call stack size exceeded (native stack depth) on the UI thread under Reanimated 4.5. The recursion originates in usePinchCommons' reset():
[UI]: set (react-native-reanimated/src/mutables.ts)
[UI]: <anonymous> (react-native-zoom-toolkit/.../usePinchCommons.js:132) // gestureEnd.value = 0
[UI]: valueSetter (react-native-reanimated/src/valueSetter.ts)
[UI]: set (react-native-reanimated/src/mutables.ts)
[UI]: <anonymous> (react-native-zoom-toolkit/.../usePinchCommons.js:132)
... repeats until native stack overflows
Root cause
In src/commons/hooks/usePinchCommons.ts, reset() writes the gestureEnd gimmick value inside its own withTiming callback, unconditionally:
gestureEnd.value = withTiming(toValue, undefined, (finished) => {
gestureEnd.value = 0; // <-- unconditional self-write
if (finished && userCallbacks.onGestureEnd !== undefined) {
scheduleOnRN(userCallbacks.onGestureEnd);
}
});
Under Reanimated 4.x, valueSetter invokes a still-live animation's callback(false) when that animation is overwritten/cancelled, before clearing mutable._animation. Because the callback writes gestureEnd.value again while _animation is still set, valueSetter re-enters the same callback, which writes again, and so on — the "prevent setting again to the same value" / cancel path never gets to null _animation. Rapid pinch in/out overlaps successive gestureEnd timings, so the cancel of a live one re-enters its own callback → unbounded recursion.
Note that the sibling usePanCommons does not hit this: its gestureEnd/decay callbacks never self-write the shared value — they only if (finished) scheduleOnRN(onGestureEnd).
Fix
Gate the self-write on finished, matching usePanCommons' pattern:
gestureEnd.value = withTiming(toValue, undefined, (finished) => {
if (finished) {
gestureEnd.value = 0;
if (userCallbacks.onGestureEnd !== undefined) {
scheduleOnRN(userCallbacks.onGestureEnd);
}
}
});
On a genuine finish this writes once, re-enters the callback at finished === false, and the guard makes that a single no-op level deep. On cancel (rapid re-pinch) it never writes → no re-entry. gestureEnd is an internal gimmick (never read in a style), so gating it is behavior-preserving.
PR incoming.
Environment
react-native-zoom-toolkit5.1.0react-native-reanimated4.5.0 (react-native-worklets0.10.0, New Architecture)react-native0.86, Expo SDK, iOS
main still carries the unconditional write.
Facing the same issue. Looking forward for PR merging! Thanks
+1, fix from PR works. Thank you!
Not work for me even after applying patch for the PR. It's happening in prod app. I am using below packages.
"react-native-zoom-toolkit": "^5.1.0", "react-native-reanimated": "4.5.1", "react-native-worklets": "^0.10.0",
+1. Facing the same issue.
@gromspys It worked for me after updating reanimated and worklets libs. I didn't apply this patch. Try with below configuration.
"react-native-reanimated": "4.5.2", "react-native-worklets": "0.10.2",