Serialize worktree commands per repo
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful

This commit is contained in:
2026-11-08 20:29:23 +11:00
parent 8a336b62a5
commit 53e78a5f75
3 changed files with 21 additions and 1 deletions
+1
View File
@@ -44,6 +44,7 @@ type Deployer struct {
sem chan struct{}
mu sync.Mutex
flights map[string]*flight
wtLocks sync.Map
}
type flight struct {
+13
View File
@@ -816,3 +816,16 @@ func TestPanicFailsOnlyItsEnv(t *testing.T) {
t.Fatal("GC skipped after a panicking env")
}
}
func TestConcurrentWorktreeAddsOneMirror(t *testing.T) {
f := newFixture(t)
for i := range 40 {
gitT(t, f.control, "checkout", "-q", "-B", fmt.Sprintf("b%d", i))
commit(t, f.control, map[string]string{"n": fmt.Sprint(i)})
}
d := New(f.cfg, 16, 10*time.Minute)
d.now = func() time.Time { return f.clock }
if err := d.Deploy(context.Background(), nil, false); err != nil {
t.Fatal(err)
}
}
+7 -1
View File
@@ -8,6 +8,7 @@ import (
"os/exec"
"slices"
"strings"
"sync"
"syscall"
"time"
)
@@ -29,8 +30,13 @@ func runGit(ctx context.Context, dir string, args ...string) (string, error) {
return strings.TrimSpace(out.String()), nil
}
// git holds a -j slot only for the subprocess itself, so callers never nest slots.
// git holds a -j slot only for the subprocess itself; worktree commands are serialized per repo since concurrent adds read each other's half-written admin dirs.
func (d *Deployer) git(ctx context.Context, dir string, args ...string) (out string, err error) {
if len(args) > 0 && args[0] == "worktree" {
mu, _ := d.wtLocks.LoadOrStore(dir, &sync.Mutex{})
mu.(*sync.Mutex).Lock()
defer mu.(*sync.Mutex).Unlock()
}
err = d.limit(func() error {
ctx, cancel := context.WithTimeout(ctx, d.gitTimeout)
defer cancel()