fix: prevent unbounded scroll-event recursion while scrollable is locked#2717
Open
OxMarco wants to merge 1 commit into
Open
fix: prevent unbounded scroll-event recursion while scrollable is locked#2717OxMarco wants to merge 1 commit into
OxMarco wants to merge 1 commit into
Conversation
`handleOnScroll` issued `scrollTo(scrollableRef, 0, lockPosition, false)` on every scroll event while the scrollable was LOCKED, including when the scrollable was already resting at `lockPosition`. `scrollTo` still drives the underlying scroll view, which emits another scroll event, which re-enters `handleOnScroll`, which calls `scrollTo` again. Once the offset has settled there is nothing left to break the cycle, so it recurses until the JS stack overflows with `RangeError: Maximum call stack size exceeded`, with the stack alternating between Reanimated's `useAnimatedScrollHandler` and this handler. Skip the `scrollTo` when the scrollable is already at the lock position, within a small epsilon to tolerate the sub-pixel offsets reported during scrolling. `scrollableContentOffsetY.value` is still assigned unconditionally, so the shared value is unchanged. `handleOnEndDrag` and `handleOnMomentumEnd` contain the same unconditional `scrollTo`, but they fire once per gesture rather than on every scroll event, so they cannot feed themselves. Left untouched to keep this fix minimal.
cc65db4 to
8fea848
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
useScrollEventsHandlersDefault'shandleOnScrollworklet callsscrollTo(...)on every scroll event while the scrollable isLOCKED, including when the scrollable is already resting at the target offset.scrollTostill drives the underlying scroll view, which emits another scroll event, which re-entershandleOnScroll, which callsscrollToagain. Once the offset has settled atlockPositionthere is nothing left to break the cycle: the guard that would normally stop it ("the offset didn't change, so no new event") doesn't hold. The recursion is bounded only by the JS stack, and it crashes withRangeError: Maximum call stack size exceeded, with the stack alternating between Reanimated'suseAnimatedScrollHandlerand this handler.We see this in production on iOS: 4 occurrences across 3 users.
The fix
Skip the
scrollTowhen the scrollable is already at the lock position, within a small epsilon to tolerate the sub-pixel offsets reported during scrolling.scrollableContentOffsetY.valueis still assigned unconditionally, so the shared value behaves exactly as before. The only behavioural change is that a redundant scroll command is no longer issued from inside the scroll event it would itself provoke.if (animatedScrollableStatus.value === SCROLLABLE_STATUS.LOCKED) { const lockPosition = context.shouldLockInitialPosition ? (context.initialContentOffsetY ?? 0) : 0; - // @ts-expect-error - scrollTo(scrollableRef, 0, lockPosition, false); + if (Math.abs(y - lockPosition) > SCROLL_LOCK_EPSILON) { + // @ts-expect-error + scrollTo(scrollableRef, 0, lockPosition, false); + } scrollableContentOffsetY.value = lockPosition; return; }ywas already destructured from the event, so no new state is needed.Scope
handleOnEndDragandhandleOnMomentumEndcontain the same unconditionalscrollTo, but they fire once per gesture rather than on every scroll event, so they cannot feed themselves the wayhandleOnScrollcan. I left them untouched to keep the change minimal. Happy to extend the guard to them if you would prefer the consistency.Testing
yarn typescriptpasses.yarn lint: the changed file lints clean. Note thatyarn lintalready fails onmasteras-is, due to a pre-existinglint/complexity/useOptionalChainwarning insrc/hooks/useBoundingClientRect.ts:54that is unrelated to this change. I deliberately have not touched it.CONTRIBUTING.mdmentionsyarn test, but there is notestscript or test suite in the repo, so I could not add a unit test. If you would like this covered, I am happy to help stand up a harness for the scroll handlers.Honest caveat
I have not reproduced this in a standalone sample. It was diagnosed from production crash reports plus reading the source. The recursion described above is unconditional and evident from the code, but I want to be upfront that this is not a "repro, fix, verify" PR. We have been running exactly this patch (via
patch-package) in our app and the crash has stopped occurring for us.Happy to adjust the approach or the epsilon value.