Skip to content

Don't keep a scroll hold that user code already disposed - #194712

Open
fahaddoc wants to merge 4 commits into
flutter:masterfrom
fahaddoc:fix/172174-scrollable-stale-hold
Open

fahaddoc wants to merge 4 commits into
flutter:masterfrom
fahaddoc:fix/172174-scrollable-stale-hold

Conversation

@fahaddoc

@fahaddoc fahaddoc commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

ScrollableState._handleDragDown assigned the result of position.hold() after that call returned:

_hold = position.hold(_disposeHold);

After the hold is installed, beginActivity synchronously notifies isScrollingNotifier listeners, and updateUserScrollDirection(idle) can notify UserScrollNotification listeners. If that user code starts another activity, for example by calling jumpTo, the hold is disposed and _disposeHold clears _hold before hold() returns. The assignment above then puts the disposed hold back into _hold. ScrollEndNotification is dispatched before the hold is installed, so it is not the callback path this fix addresses.

Nothing clears _hold again after that. _handleDragCancel calls _hold!.cancel(), which goes ballistic and disposes whichever activity is current — no longer that hold — so onHoldCanceled never runs a second time and the following assert(_hold == null) fires. The stale field then also fails the assert at the top of the next _handleDragDown, which is why the reporter only ever saw the second assertion after the first one had already happened.

This PR keeps the hold only if it survived its own creation.

The reporter's app is a ListView inside a PageView that snaps itself from an isScrollingNotifier listener. Touching down while the list is still coasting begins the hold, which stops the scroll and runs that listener; its jumpTo disposes the hold. Swiping horizontally then hands the gesture to the PageView, so the list's vertical recognizer is rejected and _handleDragCancel runs against the stale field. The new test in scrollable_test.dart reproduces exactly that sequence and fails on current master with:

'package:flutter/src/widgets/scrollable.dart': Failed assertion: line 908 pos 12: '_hold == null': is not true.

One thing I would like a reviewer's opinion on: _handleDragStart assigns _drag the same way, and ScrollPositionWithSingleContext.drag also calls beginActivity, so the same re-entrancy looks possible for _drag (there it would surface as the assert(_drag == null) at the end of _handleDragEnd). I could not produce a repro for it, so I left it alone rather than change untested code. Happy to extend this PR if you would rather see both handled together.

Fixes #172174

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

ScrollableState._handleDragDown assigned the result of position.hold()
after that call returned. Beginning the hold activity ends whatever
activity was running, and that can synchronously run user code -- a
ScrollEndNotification listener, or an isScrollingNotifier listener. If
that code starts another activity, for example by calling jumpTo, the
hold is disposed and _disposeHold clears _hold before hold() returns.
The assignment then put the disposed hold back into _hold.

Nothing clears _hold again after that: _handleDragCancel calls
_hold.cancel(), which begins a ballistic activity and disposes whatever
activity is current -- no longer that hold -- so onHoldCanceled never
runs a second time and the following assert(_hold == null) fires. The
stale field also fails the assert in the next _handleDragDown, which is
why the reporter only ever saw the second assertion after the first.

Only keep the hold if it survived its own creation.

Fixes flutter#172174
@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: scrolling Viewports, list views, slivers, etc. labels Oct 19, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request resolves a re-entrancy issue in 'ScrollableState._handleDragDown' where starting a hold activity can synchronously trigger user code that disposes of the hold before it is returned, leaving a stale reference in '_hold'. The fix introduces a local tracking variable to ensure a disposed hold is not assigned to '_hold', and a regression test is added to verify this behavior. Feedback suggests applying the same defensive pattern to '_handleDragStart' to prevent a similar potential issue.

Comment on lines +879 to +881
if (!disposed) {
_hold = hold;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Regarding your question in the PR description: yes, _handleDragStart is indeed susceptible to the exact same re-entrancy issue. Since position.drag also triggers beginActivity, ending the previous activity can synchronously run user code (such as scroll listeners) that might immediately dispose the drag before position.drag returns. This would leave a stale, disposed Drag instance in _drag, eventually failing the assert(_drag == null) in subsequent drag events.\n\nIt is highly recommended to apply the same defensive pattern to _handleDragStart in this PR to prevent this potential bug. Since the body of _handleDragStart is not directly modified in this diff, you can update it like this:\n\ndart\n void _handleDragStart(DragStartDetails details) {\n assert(_drag == null);\n var disposed = false;\n final Drag drag = position.drag(details, () {\n disposed = true;\n _disposeDrag();\n });\n if (!disposed) {\n _drag = drag;\n }\n }\n

References
  1. The repository style guide encourages checking for potential regressions and ensuring related areas are robust against similar issues. (link)

@fahaddoc
fahaddoc requested a review from victorsanni October 23, 2026 07:38
@victorsanni victorsanni added the CICD Run CI/CD label Oct 24, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 27, 2026
@victorsanni victorsanni added the CICD Run CI/CD label Oct 27, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 29, 2026
@victorsanni victorsanni added the CICD Run CI/CD label Oct 29, 2026
@fahaddoc

Copy link
Copy Markdown
Contributor Author

@Renzo-Olivares friendly ping when you get a chance. Victor approved this on Oct 27 and CI is green, so it only needs a second review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD f: scrolling Viewports, list views, slivers, etc. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'package:flutter/src/widgets/scrollable.dart': Failed assertion: line 906 pos 12: '_hold == null': is not true.

2 participants