diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2024-01-28 09:02:01 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2024-01-28 09:20:52 +1100 |
commit | bd7fabef1fec4a6826d7833da7a255bdb93a92d8 (patch) | |
tree | 0100b11e4d2572c202ad068c1b3670e918fb8e18 /pkg | |
parent | 1a38d515d78ceb60c2f88813c2c3ba8708315e48 (diff) |
Reduce the chance of race condition with list cursor
Before this commit, we had pkg/integration/tests/submodule/add.go
failing with a panic. I'm pretty sure the issue is this: we're now
calling quite a few GetDisabledReason calls on each layout() call,
and if a background thread happens to update a model slice while
we're doing this, we can end up with a selection index that's now
out of bounds because it hasn't been clamped to match the new list
length.
Specifically, here we had the selected index being -1 (the list starts
empty and somehow the value is -1 in this case) and then the list
gets a new submodule so the length is now 1, but the list cursor
doesn't know about this so remains on the old value. Then we confirm
the length is greater than zero and try to get the selected submodule
and get an out of bounds error.
This commit fixes the issue by clamping the selected index whenever
we get the length of the list so that it stays in-sync. This is not
a perfect solution because the length can change at any time, but
it seems to reliably fix the test, and using mutexes didn't seem to
make a difference.
Note that we're swapping the order of IFileTree and IListCursor in
the file tree view model to ensure that the list cursor's Len()
method is called (which performs the clamping).
Also, comment from the PR:
This 'trait' pattern we're using is convenient but can lead to awkward
situations. In this case we have both the list view model and the
(embedded) list cursor with a Len() method. The list cursor Len()
method just calls the list view model Len() method. But I wanted
to make it that the list view model now calls ClampSelection() on the
list cursor whenever it obtains the length. This will cause an
infinite loop because ClampSelection() internally calls Len()
(which calls the list view model's Len() method which in turn
calls ClampSelection() again, etc).
The only reason we were passing the list view model into the list
cursor was to supply the length method, so now we're just doing
that directly, and letting the list view model delegate the Len()
call to the list cursor, which now itself calls ClampSelection.
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/context/list_view_model.go | 6 | ||||
-rw-r--r-- | pkg/gui/context/traits/list_cursor.go | 24 | ||||
-rw-r--r-- | pkg/gui/filetree/commit_file_tree_view_model.go | 4 | ||||
-rw-r--r-- | pkg/gui/filetree/file_tree_view_model.go | 4 |
4 files changed, 19 insertions, 19 deletions
diff --git a/pkg/gui/context/list_view_model.go b/pkg/gui/context/list_view_model.go index bf8c80e23..1cb2ee648 100644 --- a/pkg/gui/context/list_view_model.go +++ b/pkg/gui/context/list_view_model.go @@ -20,15 +20,11 @@ func NewListViewModel[T HasID](getModel func() []T) *ListViewModel[T] { getModel: getModel, } - self.ListCursor = traits.NewListCursor(self) + self.ListCursor = traits.NewListCursor(func() int { return len(getModel()) }) return self } -func (self *ListViewModel[T]) Len() int { - return len(self.getModel()) -} - func (self *ListViewModel[T]) GetSelected() T { if self.Len() == 0 { return Zero[T]() diff --git a/pkg/gui/context/traits/list_cursor.go b/pkg/gui/context/traits/list_cursor.go index e42f1f5e5..3b4ab1c3d 100644 --- a/pkg/gui/context/traits/list_cursor.go +++ b/pkg/gui/context/traits/list_cursor.go @@ -5,10 +5,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/utils" ) -type HasLength interface { - Len() int -} - type RangeSelectMode int const ( @@ -27,15 +23,17 @@ type ListCursor struct { rangeSelectMode RangeSelectMode // value is ignored when rangeSelectMode is RangeSelectModeNone rangeStartIdx int - list HasLength + // Get the length of the list. We use this to clamp the selection so that + // the selected index is always valid + getLength func() int } -func NewListCursor(list HasLength) *ListCursor { +func NewListCursor(getLength func() int) *ListCursor { return &ListCursor{ selectedIdx: 0, rangeStartIdx: 0, rangeSelectMode: RangeSelectModeNone, - list: list, + getLength: getLength, } } @@ -81,8 +79,9 @@ func (self *ListCursor) GetSelectionRangeAndMode() (int, int, RangeSelectMode) { func (self *ListCursor) clampValue(value int) int { clampedValue := -1 - if self.list.Len() > 0 { - clampedValue = utils.Clamp(value, 0, self.list.Len()-1) + length := self.getLength() + if length > 0 { + clampedValue = utils.Clamp(value, 0, length-1) } return clampedValue @@ -114,7 +113,12 @@ func (self *ListCursor) ClampSelection() { } func (self *ListCursor) Len() int { - return self.list.Len() + // The length of the model slice can change at any time, so the selection may + // become out of bounds. To reduce the likelihood of this, we clamp the selection + // whenever we obtain the length of the model. + self.ClampSelection() + + return self.getLength() } func (self *ListCursor) GetRangeStartIdx() (int, bool) { diff --git a/pkg/gui/filetree/commit_file_tree_view_model.go b/pkg/gui/filetree/commit_file_tree_view_model.go index f5cba0edd..99ed8d477 100644 --- a/pkg/gui/filetree/commit_file_tree_view_model.go +++ b/pkg/gui/filetree/commit_file_tree_view_model.go @@ -22,8 +22,8 @@ type ICommitFileTreeViewModel interface { type CommitFileTreeViewModel struct { sync.RWMutex - ICommitFileTree types.IListCursor + ICommitFileTree // this is e.g. the commit for which we're viewing the files ref types.Ref @@ -37,7 +37,7 @@ var _ ICommitFileTreeViewModel = &CommitFileTreeViewModel{} func NewCommitFileTreeViewModel(getFiles func() []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileTreeViewModel { fileTree := NewCommitFileTree(getFiles, log, showTree) - listCursor := traits.NewListCursor(fileTree) + listCursor := traits.NewListCursor(fileTree.Len) return &CommitFileTreeViewModel{ ICommitFileTree: fileTree, IListCursor: listCursor, diff --git a/pkg/gui/filetree/file_tree_view_model.go b/pkg/gui/filetree/file_tree_view_model.go index 439471b01..25b3d0edc 100644 --- a/pkg/gui/filetree/file_tree_view_model.go +++ b/pkg/gui/filetree/file_tree_view_model.go @@ -21,15 +21,15 @@ type IFileTreeViewModel interface { // after the files are refreshed type FileTreeViewModel struct { sync.RWMutex - IFileTree types.IListCursor + IFileTree } var _ IFileTreeViewModel = &FileTreeViewModel{} func NewFileTreeViewModel(getFiles func() []*models.File, log *logrus.Entry, showTree bool) *FileTreeViewModel { fileTree := NewFileTree(getFiles, log, showTree) - listCursor := traits.NewListCursor(fileTree) + listCursor := traits.NewListCursor(fileTree.Len) return &FileTreeViewModel{ IFileTree: fileTree, IListCursor: listCursor, |