From d7369850db8cb3b3627518bbf1dcb8d9b23035f2 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sun, 8 Nov 2026 21:40:33 +1100 Subject: [PATCH] Keep in-flight work across concurrent Deploys --- internal/deploy/deploy.go | 9 ++++++++- internal/deploy/deploy_test.go | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 467ee3f..5b73773 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -229,7 +229,14 @@ func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) erro want[e] = true } d.mu.Lock() - clear(d.flights) + maps.DeleteFunc(d.flights, func(_ string, f *flight) bool { + select { + case <-f.done: + return true + default: + return false + } + }) clear(d.skips) clear(d.counts) d.mu.Unlock() diff --git a/internal/deploy/deploy_test.go b/internal/deploy/deploy_test.go index c9e8ef1..fd43dc6 100644 --- a/internal/deploy/deploy_test.go +++ b/internal/deploy/deploy_test.go @@ -15,9 +15,11 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "slices" "strings" "sync" + "sync/atomic" "testing" "time" ) @@ -1364,3 +1366,38 @@ func TestCorrectedBranchCollisionSkipsAll(t *testing.T) { t.Fatal("collision must not block other envs") } } + +func TestConcurrentDeploySharesInFlightFetch(t *testing.T) { + f := newFixture(t) + commit(t, f.control, map[string]string{"Puppetfile": f.puppetfile()}) + d := f.deployer() + var srcFetches, modFetches atomic.Int32 + bStarted := make(chan struct{}) + var once sync.Once + d.runGit = func(ctx context.Context, dir string, args ...string) (string, error) { + if slices.Contains(args, "fetch") { + switch { + case slices.Contains(args, f.control): + if srcFetches.Add(1) == 2 { + once.Do(func() { close(bStarted) }) + } + case slices.Contains(args, f.mod): + if modFetches.Add(1) == 1 { + <-bStarted + } + } + } + return runGit(ctx, dir, args...) + } + errc := make(chan error, 2) + go func() { errc <- d.Deploy(context.Background(), nil, false) }() + for modFetches.Load() == 0 { + runtime.Gosched() + } + go func() { errc <- d.Deploy(context.Background(), nil, false) }() + <-errc + <-errc + if n := modFetches.Load(); n != 1 { + t.Fatalf("module mirror fetched %d times; the second Deploy dropped the first's in-flight fetch", n) + } +}