Skip to content

Commit

Permalink
prevent (harmless) race condition in git repo when clearing uncommitt…
Browse files Browse the repository at this point in the history
…ed changes after stopping a plan
  • Loading branch information
danenania committed May 8, 2024
1 parent 1c6c07d commit 435b46d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/server/db/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ func GitClearUncommittedChanges(orgId, planId string) error {
return nil
}

// Not used currently but may be good to handle these errors specifically later if locking can't fully prevent them
// func isLockFileError(output string) bool {
// return strings.Contains(output, "fatal: Unable to create") && strings.Contains(output, ".git/index.lock': File exists")
// }

func gitCheckoutBranch(repoDir, branch string) error {
// get current branch and only checkout if it's not the same
// trying to check out the same branch will result in an error
Expand Down
20 changes: 15 additions & 5 deletions app/server/handlers/plans_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,29 @@ func StopPlanHandler(w http.ResponseWriter, r *http.Request) {
} else {
defer func() {
(*unlockFn)(err)

if err == nil {
err = modelPlan.Stop(planId, branch, auth.User.Id, auth.OrgId)

if err != nil {
log.Printf("Error stopping plan: %v\n", err)
http.Error(w, "Error stopping plan", http.StatusInternalServerError)
return
}

log.Println("Successfully processed request for StopPlanHandler")
}
}()
}

log.Println("Stopping plan")
err = modelPlan.Stop(planId, branch, auth.User.Id, auth.OrgId)
err = modelPlan.StorePartialReply(planId, branch, auth.User.Id, auth.OrgId)

if err != nil {
log.Printf("Error stopping plan: %v\n", err)
http.Error(w, "Error stopping plan", http.StatusInternalServerError)
log.Printf("Error storing partial reply: %v\n", err)
http.Error(w, "Error storing partial reply", http.StatusInternalServerError)
return
}

log.Println("Successfully processed request for StopPlanHandler")
}

func RespondMissingFileHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
10 changes: 10 additions & 0 deletions app/server/model/plan/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ func Stop(planId, branch, currentUserId, currentOrgId string) error {
active.SummaryCancelFn()
active.CancelFn()

return nil
}

func StorePartialReply(planId, branch, currentUserId, currentOrgId string) error {
active := GetActivePlan(planId, branch)

if active == nil {
return fmt.Errorf("no active plan with id %s", planId)
}

if !active.BuildOnly && !active.RepliesFinished {
num := active.MessageNum + 1

Expand Down

0 comments on commit 435b46d

Please sign in to comment.