Skip to content

Race condition in re-entrant checkoutBranch can cause stash and move operations to fail silently - #9038

Merged
kuychaco merged 11 commits into
developmentfrom
reentrant-checkout
Feb 6, 2020
Merged

kuychaco merged 11 commits into
developmentfrom
reentrant-checkout

Conversation

@niik

@niik niik commented Feb 3, 2020

Copy link
Copy Markdown
Member

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.

  1. The branch is created by _createBranch which 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).
  2. _checkoutBranch is called with UncommittedChangesStrategyKind.MoveToNewBranch and a branch model
  3. _checkoutBranch attempts to checkout the branch which fails since it would overwrite local changes
    const checkoutSucceeded =
    (await this.withAuthenticatingUser(repository, (repository, account) =>
    gitStore.performFailableOperation(
    () =>
    checkoutBranch(repository, account, foundBranch, progress => {
    this.updateCheckoutProgress(repository, progress)
    }),
    {
    repository,
    retryAction: {
    type: RetryActionType.Checkout,
    repository,
    branch,
    },
    gitContext: {
    kind: 'checkout',
    branchToCheckout: foundBranch.name,
    },
    }
    )
    )) !== undefined
  4. The performFailableOperation from ☝️ catches the DugiteError.LocalChangesOverwritten and emits that error and attaches the branch name (important) as extra metadata
  5. The localChangesOverwrittenHandler error handler catches this specific error and calls the _moveChangesToBranchAndCheckout method with the branch name that it wants to checkout
    await dispatcher.moveChangesToBranchAndCheckout(repository, branchToCheckout)
  6. We eventually make it back into the _checkoutBranch method where this 👇 hack takes the branch name and attempts to resolve it to a branch model
    const foundBranch =
    typeof branch === 'string'
    ? this.getLocalBranch(repository, branch)
    : branch
    if (foundBranch == null) {
    return repository
    }
  7. Whether or not it's able to resolve that branch model depends entirely on whether the original checkoutBranch method has had time to refresh the repository state since the getLocalBranch method 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 _createBranch all the way through the error handler and back and removes the ability to call _checkoutBranch with a branch name.

Testing

Steps to reproduce

  1. Create a new repository
  2. Create, and commit, a file README.txt with some random contents in it
  3. Create a new branch branch-a make some changes to README.txt, and commit them
  4. Switch back to master and make some conflicting changes to README.txt
  5. Switch back to branch-a
  6. Make some changes to README.txt but don't commit them.
  7. From within desktop choose to create a new branch based on master, call it branch-b. When asked if you want to bring your changes with you say yes

Expected result

branch-b is checked out and one conflicted file (README.txt) exists in your working directory

Actual result (sometimes, since this is a race condition)

branch-a is still checked out and you have no changes in your working directory. If you switch to branch-b the stashed changes show up.

Demo

2020-02-03 18-50-01 2020-02-03 18_50_23

@tierninho
tierninho self-requested a review February 3, 2020 21:51
@tierninho

Copy link
Copy Markdown
Contributor

I followed your testing notes and ended up at the expected result outlined above. Here is my result:

Screen Shot 2020-02-03 at 11 54 16 AM

Subsequently, I resolved the conflict in my editor, got a green checkmark, and attempted to commit the file but ran into a commit error:

Screen Shot 2020-02-03 at 11 54 38 AM

Logs are

install.ts:23 `git commit -F -` exited with an unexpected code: 1.
stdout:
On branch branch-b
nothing to commit, working tree clean

(The error was parsed as 17: There are no changes to commit.)

This bug reminds me of #7827

@kuychaco kuychaco self-assigned this Feb 3, 2020
@tierninho
tierninho removed their request for review February 5, 2020 01:29

@kuychaco kuychaco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@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 💖

Comment thread app/src/lib/stores/app-store.ts Resolved
Comment thread app/src/lib/stores/app-store.ts Resolved
Comment thread app/src/lib/stores/app-store.ts Resolved
Comment thread app/src/lib/stores/app-store.ts Resolved
Co-Authored-By: Katrina Uychaco <[email protected]>
@niik

niik commented Feb 5, 2020

Copy link
Copy Markdown
Member Author

I followed your testing notes and ended up at the expected result outlined above. Here is my result:

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.

This bug reminds me of #7827

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 branch-b and as such there's nothing to commit.

image

And the diff view shows:

image

@niik

niik commented Feb 5, 2020

Copy link
Copy Markdown
Member Author

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 branch-b and as such there's nothing to commit.

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. --ours) then we shouldn't let you commit that file.

@kuychaco kuychaco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! :shipit:

@tierninho
tierninho self-requested a review February 6, 2020 01:09

@tierninho tierninho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Makes sense. Thanks for the clarification!

@nigar9595 nigar9595 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

9109

@DurgadeviAvatarae

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants