Compare commits

...

1 Commits

Author SHA1 Message Date
Garrett Delfosse 57397d27ea fix: handle race condition in lifecycle executor concurrent builds
When multiple lifecycle executors run concurrently (e.g., in HA deployments),
they can race to build the same workspace. While an advisory lock is used to
prevent this, there's a small window where both executors can proceed.

This change treats unique constraint violations on the build number as a
benign race condition (another executor won) rather than an error.

Fixes coder/internal#455
2026-01-30 11:28:17 -05:00
+8
View File
@@ -351,6 +351,14 @@ func (e *Executor) runOnce(t time.Time) Stats {
nextBuild, job, _, err = builder.Build(e.ctx, tx, e.fileCache, nil, audit.WorkspaceBuildBaggage{IP: "127.0.0.1"})
if err != nil {
// If we get a unique constraint violation on the build number,
// it means another lifecycle executor already started a build
// for this workspace. This is a benign race condition - the
// other executor won, so we just skip this workspace.
if database.IsUniqueViolation(err, database.UniqueWorkspaceBuildsWorkspaceIDBuildNumberKey) {
log.Debug(e.ctx, "skipping workspace: build already started by another executor")
return nil
}
return xerrors.Errorf("build workspace with transition %q: %w", nextTransition, err)
}
}