Skip to content

Commit

Permalink
Fix bug in GetAndLockUnreplicatedObject query
Browse files Browse the repository at this point in the history
  • Loading branch information
ua741 committed Aug 12, 2024
1 parent f38d9de commit ff72590
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion server/pkg/controller/filedata/replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Controller) tryReplicate() error {
row, err := c.Repo.GetPendingSyncDataAndExtendLock(ctx, newLockTime, false)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
log.Errorf("Could not fetch row for deletion: %s", err)
log.Errorf("Could not fetch row for replication: %s", err)
}
return err
}
Expand Down
23 changes: 10 additions & 13 deletions server/pkg/repo/object_copies.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ func (repo *ObjectCopiesRepository) GetAndLockUnreplicatedObject() (*ente.Object
}
}

commit := func() {
cerr := tx.Commit()
if cerr != nil {
log.Errorf("Ignoring error when committing transaction: %s", cerr)
}
}

row := tx.QueryRow(`
SELECT object_key, want_b2, b2, want_wasabi, wasabi, want_scw, scw
FROM object_copies
Expand All @@ -59,20 +52,24 @@ func (repo *ObjectCopiesRepository) GetAndLockUnreplicatedObject() (*ente.Object
err = row.Scan(&r.ObjectKey, &r.WantB2, &r.B2, &r.WantWasabi, &r.Wasabi,
&r.WantSCW, &r.SCW)

if err != nil && errors.Is(err, sql.ErrNoRows) {
commit()
return nil, err
}

if err != nil {
rollback()
rollback() // Rollback transaction on any error
if errors.Is(err, sql.ErrNoRows) {
return nil, err // Return sql.ErrNoRows without committing the transaction
}
return nil, stacktrace.Propagate(err, "")
}

err = repo.RegisterReplicationAttempt(tx, r.ObjectKey)
if err != nil {
rollback()
return nil, stacktrace.Propagate(err, "failed to register replication attempt")
}

err = tx.Commit()
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
return &r, nil
}

Expand Down

0 comments on commit ff72590

Please sign in to comment.