react-native-zoom-toolkit icon indicating copy to clipboard operation
react-native-zoom-toolkit copied to clipboard

Pinch in/out crashes with 'Maximum call stack size exceeded' under Reanimated 4.5 (usePinchCommons reset self-write)

Open ssahil97 opened this issue 5 months ago • 5 comments

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-toolkit 5.1.0
  • react-native-reanimated 4.5.0 (react-native-worklets 0.10.0, New Architecture)
  • react-native 0.86, Expo SDK, iOS

main still carries the unconditional write.

ssahil97 avatar Jul 06 '26 17:07 ssahil97

Facing the same issue. Looking forward for PR merging! Thanks

lambaosk8 avatar Jul 08 '26 03:07 lambaosk8

+1, fix from PR works. Thank you!

V0V45 avatar Jul 10 '26 21:07 V0V45

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",

aadityapaliwal94 avatar Jul 18 '26 13:07 aadityapaliwal94

+1. Facing the same issue.

gromspys avatar Jul 23 '26 13:07 gromspys

@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",

aadityapaliwal94 avatar Jul 24 '26 04:07 aadityapaliwal94