1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-02 09:21:40 +02:00
lazygit/pkg/gui/context
Jesse Duffield bd7fabef1f 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.
2024-01-28 09:20:52 +11:00
..
traits Reduce the chance of race condition with list cursor 2024-01-28 09:20:52 +11:00
base_context.go Rerender certain views when their width changes 2023-10-16 09:03:07 +02:00
branches_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
commit_files_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
commit_message_context.go Mention ctrl+o binding in commit message sub title 2023-08-21 10:03:34 +02:00
confirmation_context.go split context common from helper common 2023-04-30 13:19:53 +10:00
context_common.go split context common from helper common 2023-04-30 13:19:53 +10:00
context.go Layout the bottom line view using spacer views 2023-12-09 11:18:28 +01:00
dynamic_title_builder.go show namesake for child views 2022-03-26 18:00:46 +11:00
filtered_list_view_model.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
filtered_list.go When refreshing models, re-apply active filter for the corresponding view 2023-10-10 08:37:30 +02:00
history_trait.go Add search history 2023-09-20 08:35:41 +03:00
list_context_trait.go Disable range-select in menu and suggestions view 2024-01-19 10:50:49 +11:00
list_renderer_test.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
list_renderer.go Add a mechanism to insert non-model items into list contexts 2023-08-29 08:04:47 +02:00
list_view_model.go Reduce the chance of race condition with list cursor 2024-01-28 09:20:52 +11:00
local_commits_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
menu_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
merge_conflicts_context.go Standardise display of range selection across views 2024-01-19 10:47:21 +11:00
parent_context_mgr.go
patch_explorer_context.go Standardise display of range selection across views 2024-01-19 10:47:21 +11:00
reflog_commits_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
remote_branches_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
remotes_context.go Use inline status for fetching remotes 2024-01-26 08:29:04 +01:00
search_trait.go Don't select current search result when showing search status 2023-09-25 16:37:59 +10:00
setup.go Layout the bottom line view using spacer views 2023-12-09 11:18:28 +01:00
simple_context.go remove context callback opts 2023-04-30 13:19:53 +10:00
stash_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
sub_commits_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
submodules_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
suggestions_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
tags_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
view_trait.go Add range selection ability on list contexts 2024-01-19 10:47:21 +11:00
working_tree_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00
worktrees_context.go Set groundwork for better disabled reasons with range select 2024-01-23 13:03:37 +11:00