Repository navigation
Race condition in re-entrant checkoutBranch can cause stash and move operations to fail silently - #9038
Conversation
This will let us avoid a (possibly stale) branch lookup on checkout retries
|
I followed your testing notes and ended up at the expected result outlined above. Here is my result: Subsequently, I resolved the conflict in my editor, got a green checkmark, and attempted to commit the file but ran into a commit error: Logs are This bug reminds me of #7827 |
kuychaco
left a comment
There was a problem hiding this comment.
@niik did a first pass and left a few comments with questions and suggestions. Much appreciation for the thorough and informative writeup! It was super helpful in reviewing this PR, and I found myself feeling delighted as I read it 💖
Co-Authored-By: Katrina Uychaco <[email protected]>
Right, so the fact that you ended up at the expected results means that you didn't hit the race condition. That's not terribly surprising as it's the nature of such problems to be timing dependent. It's possible that on your machine it's going to be very hard to reproduce the problem whereas on my machine it became straightforward to reproduce with the Git 2.23 upgrade.
I agree. We should reopen that. The issue is that you're resolving a (stashed) conflict in such a way that the contents of the file match that of the contents already present in And the diff view shows: |
Spoke too soon. That issue is slightly different but this is still an issue. If you end up resolving the stash conflict in such a way that no changes were made (i.e. |
tierninho
left a comment
There was a problem hiding this comment.
Makes sense. Thanks for the clarification!




Description
I stumbled upon this today as I was creating a new branch and wanted to bring along some changes I had made. After typing in the new branch name and choosing to bring along changes I was left on the branch I started on. The new branch had been created and the stash had been created for it but Desktop hadn't switched to the new branch or popped the transient stash entry.
In digging through this I discovered that this is due to a race condition due to the re-entrant nature of
_checkoutBranch. I've been able to consistently reproduce this recently and I was able to narrow it down to the recent Git/dugite upgrade (#8920). I can't see any reason why the dugite upgrade should affect this behavior so I'm forced to assume that thanks to the performance improvements in Git 2.23 (which I've been able to measure) this race condition is easier to trigger on my machine.In order to reproduce we need to engineer a scenario where #7474 comes into play. I.e. the initial checkout fails because it can't cleanly checkout the branch without overwriting local changes. See the "Steps to reproduce" section below for details on how I chose to do that.
The flow goes something like this.
_createBranchwhich returns a branch model (this will be important later on) that's created by shelling out to Git (i.e. the Git store is not updated)._checkoutBranchis called withUncommittedChangesStrategyKind.MoveToNewBranchand a branch model_checkoutBranchattempts to checkout the branch which fails since it would overwrite local changesdesktop/app/src/lib/stores/app-store.ts
Lines 3076 to 3096 in df6772f
performFailableOperationfrom ☝️ catches theDugiteError.LocalChangesOverwrittenand emits that error and attaches the branch name (important) as extra metadatalocalChangesOverwrittenHandlererror handler catches this specific error and calls the_moveChangesToBranchAndCheckoutmethod with the branch name that it wants to checkoutdesktop/app/src/ui/dispatcher/error-handlers.ts
Line 475 in df6772f
_checkoutBranchmethod where this 👇 hack takes the branch name and attempts to resolve it to a branch modeldesktop/app/src/lib/stores/app-store.ts
Lines 3042 to 3049 in df6772f
checkoutBranchmethod has had time to refresh the repository state since thegetLocalBranchmethod depends on the cached branch state in the repository state cache.The real solution
I was never a big fan of the re-entrant error-handler approach as can be seen in my comment from the initial PR and I think the best solution would be to move this logic inline instead of relying on an error handler.
We filed #7617 to track implementing an inline logic approach and it turns out @probablycorey already did most of the work in #8076 but it was ultimately closed. I think we should resurrect that work and bring it over the finish line. That's a decently sized chunk of work though which is why I'm opening this intermediate PR as a stop-gap measure.
The intermediate solution
This PR doesn't implement the nice solution that I'm proposing in the previous section but rather plugs this hole by passing along the same branch model that was returned from
_createBranchall the way through the error handler and back and removes the ability to call_checkoutBranchwith a branch name.Testing
Steps to reproduce
README.txtwith some random contents in itbranch-amake some changes toREADME.txt, and commit themmasterand make some conflicting changes toREADME.txtbranch-aREADME.txtbut don't commit them.master, call itbranch-b. When asked if you want to bring your changes with you say yesExpected result
branch-bis checked out and one conflicted file (README.txt) exists in your working directoryActual result (sometimes, since this is a race condition)
branch-ais still checked out and you have no changes in your working directory. If you switch tobranch-bthe stashed changes show up.Demo