Repository navigation
Conversation
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
There was a problem hiding this comment.
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.
| if (!disposed) { | ||
| _hold = hold; | ||
| } |
There was a problem hiding this comment.
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
- The repository style guide encourages checking for potential regressions and ensuring related areas are robust against similar issues. (link)
|
@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. |
ScrollableState._handleDragDownassigned the result ofposition.hold()after that call returned:After the hold is installed,
beginActivitysynchronously notifiesisScrollingNotifierlisteners, andupdateUserScrollDirection(idle)can notifyUserScrollNotificationlisteners. If that user code starts another activity, for example by callingjumpTo, the hold is disposed and_disposeHoldclears_holdbeforehold()returns. The assignment above then puts the disposed hold back into_hold.ScrollEndNotificationis dispatched before the hold is installed, so it is not the callback path this fix addresses.Nothing clears
_holdagain after that._handleDragCancelcalls_hold!.cancel(), which goes ballistic and disposes whichever activity is current — no longer that hold — soonHoldCancelednever runs a second time and the followingassert(_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
ListViewinside aPageViewthat snaps itself from anisScrollingNotifierlistener. Touching down while the list is still coasting begins the hold, which stops the scroll and runs that listener; itsjumpTodisposes the hold. Swiping horizontally then hands the gesture to thePageView, so the list's vertical recognizer is rejected and_handleDragCancelruns against the stale field. The new test inscrollable_test.dartreproduces exactly that sequence and fails on current master with:One thing I would like a reviewer's opinion on:
_handleDragStartassigns_dragthe same way, andScrollPositionWithSingleContext.dragalso callsbeginActivity, so the same re-entrancy looks possible for_drag(there it would surface as theassert(_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.