jesseduffield.lazygit/pkg/gui
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
..
context Reduce the chance of race condition with list cursor 2024-01-28 09:20:52 +11:00
controllers Display more keybindings on-screen 2024-01-28 08:33:13 +11:00
filetree Reduce the chance of race condition with list cursor 2024-01-28 09:20:52 +11:00
keybindings Add range selection ability on list contexts 2024-01-19 10:47:21 +11:00
mergeconflicts Standardise display of range selection across views 2024-01-19 10:47:21 +11:00
modes Add a "Mark commit as base commit for rebase" command 2023-07-31 08:41:41 +02:00
patch_exploring Make range selections created with the mouse non-sticky 2024-01-24 08:22:55 +01:00
popup Add ErrorToast function 2024-01-14 17:45:35 +01:00
presentation Use inline status for fetching remotes 2024-01-26 08:29:04 +01:00
services/custom_commands Standardise on using lo for slice functions 2023-07-30 18:51:23 +10:00
status Double the duration of error toasts 2024-01-14 17:45:35 +01:00
style Stop hiding underscores for VSCode 2023-06-01 20:12:20 +10:00
types Show mode-specific keybinding suggestions 2024-01-28 08:33:13 +11:00
background.go Ensure background refreshes don't bunch up 2023-07-10 17:30:44 +10:00
command_log_panel.go Introduce filtered list view model 2023-07-03 12:54:13 +10:00
context.go Show mode-specific keybinding suggestions 2024-01-28 08:33:13 +11:00
context_config.go move some methods 2023-04-30 13:19:53 +10:00
controllers.go Improve keybinding descriptions 2024-01-28 08:12:01 +11:00
dummies.go Make it possible to handle toasts in integration tests 2024-01-14 17:42:03 +01:00
editors.go Add emacs-keybinds for word navigation 2023-08-15 12:22:17 +02:00
extras_panel.go Extract a SaveAppStateAndLogError function 2023-09-04 17:50:49 +02:00
global_handlers.go Enforce single-item selection in various actions 2024-01-19 10:50:49 +11:00
gui.go Add SetSelection function for list contexts and use it in most places 2024-01-19 10:47:21 +11:00
gui_common.go Add WithInlineStatus helper function 2023-10-08 18:45:36 +02:00
gui_driver.go Show unacknowledged toast message upon integration test failure 2024-01-19 10:50:49 +11:00
information_panel.go If OpenLink errors, show a dialog instead 2023-08-09 13:12:40 +01:00
keybindings.go Improve keybinding descriptions 2024-01-28 08:12:01 +11:00
layout.go Show mode-specific keybinding suggestions 2024-01-28 08:33:13 +11:00
main_panels.go Return only visible views from TopViewInWindow 2023-12-10 15:57:51 +01:00
menu_panel.go Add SetSelection function for list contexts and use it in most places 2024-01-19 10:47:21 +11:00
options_map.go Show mode-specific keybinding suggestions 2024-01-28 08:33:13 +11:00
pty.go Use a PTY when calling external diff command 2023-11-22 12:08:05 +01:00
pty_windows.go Format code to pass lint 2021-09-16 21:38:43 +08:00
recent_repos_panel.go Use forward-slashes on windows 2023-07-30 18:35:24 +10:00
tasks_adapter.go Use an interface for tasks instead of a concrete struct 2023-07-10 17:12:21 +10:00
test_mode.go Add ErrorToast function 2024-01-14 17:45:35 +01:00
view_helpers.go Log duration of post-refresh-update call 2023-07-29 09:39:22 +10:00
views.go Show mode-specific keybinding suggestions 2024-01-28 08:33:13 +11:00