Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

transaction handling

Dane Schneider4bb3c639287c
diff --git a/app/server/db/branch_helpers.go b/app/server/db/branch_helpers.go
index 5b1d8c18..2ea08eb5 100644
--- a/app/server/db/branch_helpers.go
+++ b/app/server/db/branch_helpers.go
@@ -168,8 +168,14 @@ func DeleteBranch(orgId, planId, branch string) error {
return fmt.Errorf("error starting transaction: %v", err)
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
diff --git a/app/server/db/invite_helpers.go b/app/server/db/invite_helpers.go
index a9a6c04e..e5abcebe 100644
--- a/app/server/db/invite_helpers.go
+++ b/app/server/db/invite_helpers.go
@@ -121,8 +121,14 @@ func AcceptInvite(invite *Invite, inviteeId string) error {
return fmt.Errorf("error starting transaction: %v", err)
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
diff --git a/app/server/db/locks.go b/app/server/db/locks.go
index 752fda60..30e04c94 100644
--- a/app/server/db/locks.go
+++ b/app/server/db/locks.go
@@ -137,8 +137,14 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {
return "", fmt.Errorf("error starting transaction: %v", err)
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
@@ -166,11 +172,10 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {
lockablePlanIdQuery += " FOR SHARE"
}
- planIdRows, err := tx.Query(lockablePlanIdQuery, planId)
+ _, err = tx.Exec(lockablePlanIdQuery, planId)
if err != nil {
return handleDeadlockError(err)
}
- defer planIdRows.Close()
query := "SELECT id, org_id, user_id, plan_id, plan_build_id, scope, branch, last_heartbeat_at, created_at FROM repo_locks WHERE plan_id = $1"
if forUpdate {
@@ -354,6 +359,8 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {
return "", fmt.Errorf("error committing transaction: %v", err)
}
+ committed = true
+
log.Println()
log.Printf("LOCK_ACQUIRED | id: %s | params: %+v | stack:\n%s", newLock.Id, params, stackTrace)
log.Println()
@@ -449,19 +456,25 @@ func deleteRepoLock(id, planId string, numRetry int) error {
return deleteRepoLock(id, planId, numRetry+1)
}
+ var committed bool
+
// Start a new transaction for the delete
tx, err := Conn.BeginTx(context.Background(), &sql.TxOptions{Isolation: sql.LevelRepeatableRead})
if err != nil {
log.Printf("[Delete][%d] Error starting delete transaction: %v", goroutineID, err)
return fmt.Errorf("error starting delete transaction: %v", err)
}
- defer tx.Rollback()
+ defer func() {
+ if !committed {
+ tx.Rollback()
+ }
+ }()
deleteStart := time.Now()
log.Printf("[Delete][%d] Starting DELETE at %v", goroutineID, deleteStart)
lockablePlanIdQuery := "SELECT * FROM lockable_plan_ids WHERE plan_id = $1 FOR UPDATE"
- lockablePlanIdRows, err := tx.Query(lockablePlanIdQuery, planId)
+ _, err = tx.Exec(lockablePlanIdQuery, planId)
if err != nil {
if isDeadlockError(err) {
log.Printf("[Delete][%d] Deadlock error, retrying delete after %v: %v",
@@ -471,7 +484,6 @@ func deleteRepoLock(id, planId string, numRetry int) error {
return fmt.Errorf("error getting lockable plan id: %v", err)
}
- defer lockablePlanIdRows.Close()
query := "DELETE FROM repo_locks WHERE id = $1"
result, err := tx.Exec(query, id)
@@ -500,6 +512,8 @@ func deleteRepoLock(id, planId string, numRetry int) error {
return fmt.Errorf("error committing delete: %v", err)
}
+ committed = true
+
log.Printf("[Delete][%d] DELETE completed in %v",
goroutineID, time.Since(deleteStart))
diff --git a/app/server/db/plan_helpers.go b/app/server/db/plan_helpers.go
index 40a9a5a6..198cbc60 100644
--- a/app/server/db/plan_helpers.go
+++ b/app/server/db/plan_helpers.go
@@ -25,8 +25,14 @@ func CreatePlan(orgId, projectId, userId, name string) (*Plan, error) {
return nil, fmt.Errorf("error starting transaction: %v", err)
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
diff --git a/app/server/handlers/accounts.go b/app/server/handlers/accounts.go
index 9382340e..cf42ef17 100644
--- a/app/server/handlers/accounts.go
+++ b/app/server/handlers/accounts.go
@@ -66,8 +66,14 @@ func CreateAccountHandler(w http.ResponseWriter, r *http.Request) {
var apiErr *shared.ApiError
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
@@ -111,6 +117,8 @@ func CreateAccountHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ committed = true
+
// get orgs
orgs, err := db.GetAccessibleOrgsForUser(user)
diff --git a/app/server/handlers/auth_helpers.go b/app/server/handlers/auth_helpers.go
index ea9db723..cafc9311 100644
--- a/app/server/handlers/auth_helpers.go
+++ b/app/server/handlers/auth_helpers.go
@@ -353,8 +353,14 @@ func ValidateAndSignIn(w http.ResponseWriter, r *http.Request, req shared.SignIn
return nil, fmt.Errorf("error starting transaction: %v", err)
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
diff --git a/app/server/handlers/branches.go b/app/server/handlers/branches.go
index cb3904c3..5a616b48 100644
--- a/app/server/handlers/branches.go
+++ b/app/server/handlers/branches.go
@@ -126,8 +126,14 @@ func CreateBranchHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
diff --git a/app/server/handlers/invites.go b/app/server/handlers/invites.go
index 0b9a3e9f..0e4c5b18 100644
--- a/app/server/handlers/invites.go
+++ b/app/server/handlers/invites.go
@@ -132,8 +132,14 @@ func InviteUserHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
@@ -175,6 +181,8 @@ func InviteUserHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ committed = true
+
log.Println("Successfully created invite")
}
diff --git a/app/server/handlers/orgs.go b/app/server/handlers/orgs.go
index 98fff406..4074da40 100644
--- a/app/server/handlers/orgs.go
+++ b/app/server/handlers/orgs.go
@@ -93,8 +93,14 @@ func CreateOrgHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
diff --git a/app/server/handlers/projects.go b/app/server/handlers/projects.go
index cc815056..5b316d85 100644
--- a/app/server/handlers/projects.go
+++ b/app/server/handlers/projects.go
@@ -51,8 +51,14 @@ func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
diff --git a/app/server/handlers/settings.go b/app/server/handlers/settings.go
index f53c4416..a0d9919a 100644
--- a/app/server/handlers/settings.go
+++ b/app/server/handlers/settings.go
@@ -208,8 +208,14 @@ func UpdateDefaultSettingsHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error starting transaction", http.StatusInternalServerError)
return
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")
diff --git a/app/server/handlers/users.go b/app/server/handlers/users.go
index 6dcbc907..d836b2d2 100644
--- a/app/server/handlers/users.go
+++ b/app/server/handlers/users.go
@@ -192,8 +192,14 @@ func DeleteOrgUserHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ var committed bool
+
// Ensure that rollback is attempted in case of failure
defer func() {
+ if committed {
+ return
+ }
+
if rbErr := tx.Rollback(); rbErr != nil {
if rbErr == sql.ErrTxDone {
log.Println("attempted to roll back transaction, but it was already committed")