Skip to content

Conversation

@stefanhaller
Copy link
Collaborator

Ok, this is a long one. (It took me all weekend to figure out.)

We seem to have a race condition between re-rendering the main view and the layout code that checks whether a view has become smaller and therefore needs to scroll up. When rerendering the main view, we are careful not to invalidate the ViewLines array, so the code first calls Reset on the view, and then starts writing lines to the view until we have written enough for its old scroll position, and only then do we trigger a redraw. This is all well and good, but theoretically it could happen that the above-mentioned layout code runs shortly after the task has started writing lines to the view (say, after the first one has been written), and then it would see that the view's height is only 1, and scroll it all the way to the top.

I have never seen this happen, so it seems that we are being lucky and the race condition is only a theoretical one.

However: we had a very silly and embarrassing bug in the focused-main-view code that triggers the race condition occasionally. The bug is that when the main view is focused, we would refresh it multiple times in quick succession, once for every side panel that is being refreshed (instead of just once for the side panel that it belongs to). So the first task would call Reset, start writing lines to the view, and then the second task would come along, kill the first, call Reset again, and start writing lines again, and so on. Apparently this made it more likely for the layout code to run concurrently with this, and see the view at a moment where it only has one or two lines. I have seen it scroll to the top on its own a few times, which is very annoying when you are in the middle of reviewing a longer commit, for instance.

We can fix this by refreshing the main view only for the side panel that it belongs to, which is what this PR does. I have let lazygit run over night with a refresher.refreshInterval value of 3, and it hadn't scrolled to the top when I came to look in the morning, which makes me pretty confident that we're good now.

It would still be nice if we could fix the race condition for real too, but it's less urgent now, and it also doesn't seem trivial. I guess instead of writing lines directly to the view we would have to buffer them first, and only write them to the view when the original scroll position is reached (with some synchronization, e.g. with a OnUIThread). There are other complications that make this tricky though, and I have no plans right now to tackle this.

@stefanhaller stefanhaller added the bug Something isn't working label May 19, 2025
@codacy-production
Copy link

codacy-production bot commented May 19, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
Report missing for 7a24c561 100.00%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (7a24c56) Report Missing Report Missing Report Missing
Head commit (f3466e2) 56436 49015 86.85%

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#4573) 3 3 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Footnotes

  1. Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.

Copy link
Owner

@jesseduffield jesseduffield left a comment

Choose a reason for hiding this comment

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

Great work finding the cause of the bug, and great that it's such a simple solution

Previously we would re-render the focused main view several times during a
refresh, once for every side panel. While this should usually not be noticeable
for users because we are careful to avoid flicker when refreshing the main view,
this would sometimes lead to the main view scrolling up to the top by itself;
see PR description for more details.
@stefanhaller stefanhaller force-pushed the fix-focused-main-view-render-problem branch from 6bc7a3e to f3466e2 Compare May 22, 2025 06:15
@stefanhaller stefanhaller enabled auto-merge May 22, 2025 06:15
@stefanhaller stefanhaller merged commit f250dfb into master May 22, 2025
14 checks passed
@stefanhaller stefanhaller deleted the fix-focused-main-view-render-problem branch May 22, 2025 06:17
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request May 23, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [jesseduffield/lazygit](https://github.com/jesseduffield/lazygit) | minor | `v0.50.0` -> `v0.51.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>jesseduffield/lazygit (jesseduffield/lazygit)</summary>

### [`v0.51.0`](https://github.com/jesseduffield/lazygit/releases/tag/v0.51.0)

[Compare Source](jesseduffield/lazygit@v0.50.0...v0.51.0)

<!-- Release notes generated using configuration in .github/release.yml at v0.51.0 -->

#### What's Changed

##### Enhancements 🔥

-   Clean up the configuration of where a custom command's output goes by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4525
-   Add custom patch command "Move patch into new commit before the original commit" by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4552
-   Make '>' first jump to the beginning of the branch, and only then to the first commit by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4544
-   Add an alternate keybinding (default <c-s>) for ConfirmInEditor by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4532
-   Print migration changes to the console when migrating config file by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4548

##### Fixes 🔧

-   Migrate deprecated AllBranchesLogCmd to AllBranchesLogCmds by [@&#8203;ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4345
-   Clear preserved commit message when entering CommitEditorPanel by [@&#8203;ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4558
-   Split behavior of rendering allBranchesLogCmd and switching to next cmd by [@&#8203;ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4574
-   Fix possible crash with auto-forwarding branches by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4565
-   Fix main view occasionally scrolling to the top on its own when focused by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4573
-   Fix home and end keys in prompts by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4554
-   Fix crash when clicking in the status view by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4567

##### Maintenance ⚙️

-   Clean up utils package by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4538

##### Docs 📖

-   reword documentation for git.autoForwardBranches by [@&#8203;sean-xyz](https://github.com/sean-xyz) in jesseduffield/lazygit#4545

#### New Contributors

-   [@&#8203;sean-xyz](https://github.com/sean-xyz) made their first contribution in jesseduffield/lazygit#4545

**Full Changelog**: jesseduffield/lazygit@v0.50.0...v0.51.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC4yMi4wIiwidXBkYXRlZEluVmVyIjoiNDAuMjMuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants