Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition with reselecting the focused branch and rendering #4268

Merged
merged 1 commit into from
Feb 22, 2025

Conversation

stefanhaller
Copy link
Collaborator

In ff4ae4a we changed the order of the calls to render before selecting the branch. This was done only to save an extra call to ReApplyFilter, which is done by refreshView; I claimed that the order of refreshView vs. SetSelectedLineIdx doesn't matter here. I guess I was wrong about that, it makes the integration test custom_commands/suggestions_preset.go flaky. To fix this, put the refreshView call back to where it was (after the SetSelectedLineIdx call), and instead insert an extra call to ReApplyFilter where necessary to fix the bug that ff4ae4a was trying to fix.

I still don't think there was any user facing problem caused by this (@ChrisMcD1 correct me if I'm wrong), so I don't think we need to cut a 0.46.1 release with the fix (otherwise it would have been a regression in 0.46), and I only label it as maintenance because it only fixes CI.

Fixes #4267.

@stefanhaller stefanhaller added the maintenance For refactorings, CI changes, tests, version bumping, etc label Feb 15, 2025
Copy link

codacy-production bot commented Feb 15, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
Report missing for daf1fd31 100.00%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (daf1fd3) Report Missing Report Missing Report Missing
Head commit (3e11e34) 53095 45995 86.63%

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 (#4268) 4 4 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

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more

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.

LGTM

@ChrisMcD1
Copy link
Contributor

@stefanhaller There could be a user facing problem caused by this, but I imagine it would be small. I normally only switch branches by hovering over them and hitting <space>, so I never need the focusing behavior following a branch re-ordering to return me to a non-zero index in the branches. I imagine others are similar.

I just tried setting up my local build with the equivalent config to the flaky integration test:

customCommands:
  - key:     "a"
    context: "localBranches"
    command: "git checkout {{.Form.Branch}}"
    prompts:
      - key:   "Branch"
        type:  "input"
        title: "Enter a branch name"
        suggestions: 
          preset: "branches"

and was unable to reproduce it right now on the lazygit repository, or a small test repository. But I'm also not sitting here and re-launching lazygit 100x like the integration tests did. The bug was inconsistent enough that I had to bump the attempts from 25x after my first git bisect run landed me on the wrong commit.

If we do know a non-contrived flow that relies on this focusing behavior, it might be worth giving it a few reps to see if it gets a more unlucky roll of the dice for some reason.

@stefanhaller
Copy link
Collaborator Author

I suppose you are right. And the focussing behavior that we are talking about is not only needed when running a custom command that checks out a branch (I guess you could also checkout the branch on the command line in another terminal and then come back to lazygit), but also when the sort order of the branches list changes for some other reason. The commit message of 2c9b477 describes one such scenario that I actually find pretty important.

I still don't understand what exactly went wrong, and I don't have time right now to dig deeper (will try tomorrow), but if the issue is that sometimes the call to SetSelectedLineIdx happened before the call to FocusLine, then that's only a visual problem, not a behavior problem. We are selecting the right branch after all, we might just not render it as selected. But invoking any command on the selected branch (including arrow up/down to select the prev/next) will work correctly because these are using the model selection.

The reason why the test failed is because integration tests don't know anything about the model selection, they can only look at the view highlight.

Ok, I did some testing with the following custom command:

customCommands:
  - key: a
    command: git checkout -
    context: localBranches

Then, check out some branch, check out some other branch, and then keep pressing a to switch between the two. It should always keep the currently selected one selected.

I observed that sometimes it keeps the same index selected, but only for a brief moment (like half a second or so), then the highlight jumps to the right branch. And I can see this both with this branch and with master, which confuses me quite a bit.

I have to stop now, will continue tomorrow.

In ff4ae4a we changed the order of the calls to render before selecting the
branch. This was done only to save an extra call to ReApplyFilter, which is done
by refreshView; I claimed that the order of refreshView vs. SetSelectedLineIdx
doesn't matter here. I guess I was wrong about that, it makes the integration
test custom_commands/suggestions_preset.go flaky. To fix this, put the
refreshView call back to where it was (after the SetSelectedLineIdx call), and
instead insert an extra call to ReApplyFilter where necessary to fix the bug
that ff4ae4a was trying to fix.
@stefanhaller
Copy link
Collaborator Author

I didn't get around to do any deeper investigations, but I guess the fix is good anyway, so I'm just going to merge it now.

@stefanhaller stefanhaller merged commit 101bbb0 into master Feb 22, 2025
15 checks passed
@stefanhaller stefanhaller deleted the fix-4267 branch February 22, 2025 09:10
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Feb 25, 2025
This MR contains the following updates:

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

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.47.1`](https://github.com/jesseduffield/lazygit/releases/tag/v0.47.1)

[Compare Source](jesseduffield/lazygit@v0.46.0...v0.47.1)

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

#### What's Changed

##### Maintenance ⚙️

-   Fix race condition with reselecting the focused branch and rendering by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4268

**Full Changelog**: jesseduffield/lazygit@v0.47.0...v0.47.1

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

[Compare Source](jesseduffield/lazygit@v0.45.2...v0.46.0)

It's time for another Lazygit release! Thanks to all who contributed to this release.

#### What's Changed

##### Enhancements 🔥

-   Preserve pending commit message when closing/re-opening by [@&#8203;AzraelSec](https://github.com/AzraelSec) in jesseduffield/lazygit#4191
-   Swap position of checkout-commit and checkout-branch menu items by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4238
    -   In a previous release we had made it so that when you press space on a commit, the first option in the menu was to check out the corresponding branch, rather than the commit itself. We've made it so that now the first option is to check out the commit itself, for the sake of everybody's muscle memory.
-   Allow user to filter the files view to only show untracked files by [@&#8203;jesseduffield](https://github.com/jesseduffield) in jesseduffield/lazygit#4226
-   Expose {{.SelectedCommitRange}} to custom commands by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4204
-   Show background fetch status in bottom line by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4215
-   Support copying tags to clipboard by [@&#8203;brunofjesus](https://github.com/brunofjesus) in jesseduffield/lazygit#4218
-   Add option to delete local and remote tag by [@&#8203;AnvarU](https://github.com/AnvarU) in jesseduffield/lazygit#4217
-   Show confirmation menu when trying to amend changes while there are conflicts by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4222
-   Improve error reporting on config migration by [@&#8203;kas2020-commits](https://github.com/kas2020-commits) in jesseduffield/lazygit#4210
-   Show files filter status by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4230
-   Improve contrast of highlighted search results by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4242

##### Fixes 🔧

-   Don't try killing processes if we already know the command finished by [@&#8203;brandondong](https://github.com/brandondong) in jesseduffield/lazygit#4231
    -   We believe this was causing all sorts of problems for windows users.
-   Add '--' to 'git rev-list' to disambiguate branch name from path by [@&#8203;efussi](https://github.com/efussi) in jesseduffield/lazygit#4185
-   Fix adding blank line at end of commit message by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4182
-   Fix checking out a different branch while pushing a branch for the first time by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4214
-   Fix tag being truncated when copying to clipboard by [@&#8203;brunofjesus](https://github.com/brunofjesus) in jesseduffield/lazygit#4232
-   Disable staging and unstaging lines or hunks when the diff context size is 0 by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4235
-   Fix incorrect stash diff after rename by [@&#8203;brandondong](https://github.com/brandondong) in jesseduffield/lazygit#4213
-   Fix possible crash when deleting a branch while filtering is active by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4195
-   Fix pasting multi-line text into commit message panel by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4234
-   fix: properly detect icon for BAZEL and WORKSPACE files by [@&#8203;PeterCardenas](https://github.com/PeterCardenas) in jesseduffield/lazygit#4252
-   Fix json schema for context of CustomCommand by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4255

##### Maintenance ⚙️

-   Bump go-deadlock version to fix crash with go 1.23 in debug mode by [@&#8203;ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4248
-   Fix auto-release workflow by [@&#8203;jesseduffield](https://github.com/jesseduffield) in jesseduffield/lazygit#4265

##### I18n 🌎

-   Update translations; includes a new Portuguese translation by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4250

#### New Contributors

-   [@&#8203;efussi](https://github.com/efussi) made their first contribution in jesseduffield/lazygit#4185
-   [@&#8203;brunofjesus](https://github.com/brunofjesus) made their first contribution in jesseduffield/lazygit#4218
-   [@&#8203;AnvarU](https://github.com/AnvarU) made their first contribution in jesseduffield/lazygit#4217
-   [@&#8203;kas2020-commits](https://github.com/kas2020-commits) made their first contribution in jesseduffield/lazygit#4210
-   [@&#8203;ChrisMcD1](https://github.com/ChrisMcD1) made their first contribution in jesseduffield/lazygit#4248
-   [@&#8203;PeterCardenas](https://github.com/PeterCardenas) made their first contribution in jesseduffield/lazygit#4252

**Full Changelog**: jesseduffield/lazygit@v0.45.2...v0.46.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:eyJjcmVhdGVkSW5WZXIiOiIzOS4xNzAuMSIsInVwZGF0ZWRJblZlciI6IjM5LjE3Ni40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance For refactorings, CI changes, tests, version bumping, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race Condition in Branch Focus Rendering
3 participants