Skip to content

Prevent concurrent appStore._checkoutBranch call from error handlers #7617

Closed

Description

@iAmWillShepherd

Original context from @outofambit and @niik in #7474 (comment)

A great shout @outofambit. _checkoutBranch does become re-entrant in this scenario. I think we should still await here though, not awaiting will not alter the re-entrant nature of the logic as performFailableOperation doesn't itself await the result of the error handlers. I think instead we should focus on your second idea and explore that a little bit more but I definitely agree with your assessment of risk in doing so.

I imagine that the fact that we have this disconnect in the awaitable chain will cause other problems such as the StashAndSwitchBranch progress not working properly. If you choose to move your changes to the next branch and hit Ok, the dialog progress will start spinning but if the checkout fails and the error handler takes over I would imagine that the dialog will close and the stash + checkout operation will continue in the background.

My gut feeling here is that a better approach would be to have _checkoutBranch catch this specific error inline and deal with it accordingly rather than waiting for the global error handler to pick it up. That, however, is a much bigger task that I don't think we'll have time to implement and test but definitely something worth investigating post 2.0.

A completely untested rough draft of how inline-management of this would work:

diff --git a/app/src/lib/stores/app-store.ts b/app/src/lib/stores/app-store.ts
index 05d219da5..5472d89d9 100644
--- a/app/src/lib/stores/app-store.ts
+++ b/app/src/lib/stores/app-store.ts
@@ -2918,10 +2918,48 @@ export class AppStore extends TypedBaseStore<IAppState> {
     const checkoutSucceeded =
       (await this.withAuthenticatingUser(repository, (repository, account) =>
         gitStore.performFailableOperation(
-          () =>
-            checkoutBranch(repository, account, foundBranch, progress => {
-              this.updateCheckoutProgress(repository, progress)
-            }),
+          async () => {
+            try {
+              await checkoutBranch(
+                repository,
+                account,
+                foundBranch,
+                progress => {
+                  this.updateCheckoutProgress(repository, progress)
+                }
+              )
+              return true
+            } catch (err) {
+              if (
+                !isLocalChangesWouldBeOverwrittenError(err) ||
+                stashToPop !== null
+              ) {
+                throw err
+              }
+
+              if (
+                !(await gitStore.performFailableOperation(() => {
+                  return createDesktopStashEntry(repository, foundBranch.name)
+                }))
+              ) {
+                return false
+              }
+
+              stashToPop = await getLastDesktopStashEntryForBranch(
+                repository,
+                foundBranch.name
+              )
+
+              return await checkoutBranch(
+                repository,
+                account,
+                foundBranch,
+                progress => {
+                  this.updateCheckoutProgress(repository, progress)
+                }
+              )
+            }
+          },

Originally posted by @niik in #7474

subset of #7607

Activity

  1. added
    epic:stashing
    tech-debt
    on May 23, 2019
  2. changed the title [-]Update logic in _checkoutBranch to exit early if checkout fails[/-] [+]Prevent concurrent appStore._checkoutBranch call from error handlers[/+] on May 29, 2019
  3. probablycorey commented on Aug 13, 2019

    @probablycorey
    Contributor

    Closing in favor of #8099

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

Metadata

Metadata

Assignees

Labels

epic:stashingtech-debt

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions