Serialize worktree commands per repo
This commit is contained in:
@@ -44,6 +44,7 @@ type Deployer struct {
|
||||
sem chan struct{}
|
||||
mu sync.Mutex
|
||||
flights map[string]*flight
|
||||
wtLocks sync.Map
|
||||
}
|
||||
|
||||
type flight struct {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user