From 77bea54fb394153cb68ac1f19102a3724dda805b Mon Sep 17 00:00:00 2001 From: Lucas Rayan Date: Sun, 15 Nov 2026 23:26:39 -0300 Subject: [PATCH] test(probeagent): wait for Run to return before TempDir cleanup Tests started probes with a deferred cancel and never waited for Run, so a final state write could race t.TempDir's RemoveAll ('directory not empty'). startRun now registers a cleanup that cancels and waits. Co-Authored-By: Claude Opus 5.5 --- internal/probeagent/probe_test.go | 42 ++++++++++++++++++------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/internal/probeagent/probe_test.go b/internal/probeagent/probe_test.go index f4068da..52dbbe6 100644 --- a/internal/probeagent/probe_test.go +++ b/internal/probeagent/probe_test.go @@ -212,9 +212,15 @@ func appendFile(t *testing.T, path string, lines ...string) { } } -func startRun(ctx context.Context, c Config) chan error { - done := make(chan error, 1) - go func() { done <- Run(ctx, c, quiet()) }() +// startRun runs the probe in the background. The cleanup cancels it and waits +// for Run to return, so no state write races the TempDir removal (cleanups run +// in reverse order: this one, registered after t.TempDir, runs first). +func startRun(t *testing.T, ctx context.Context, c Config) chan error { + t.Helper() + ctx, cancel := context.WithCancel(ctx) + done, fin := make(chan error, 1), make(chan struct{}) + go func() { defer close(fin); done <- Run(ctx, c, quiet()) }() + t.Cleanup(func() { cancel(); <-fin }) return done } @@ -228,7 +234,7 @@ func TestEndToEnd(t *testing.T) { e.cfg.Location = &wire.Location{Lat: 1, Lon: 2, Label: "x"} appendFile(t, e.cfg.Paths[wire.SrcTraefik], "t1", "t2") ctx, cancel := context.WithCancel(context.Background()) - done := startRun(ctx, e.cfg) + done := startRun(t, ctx, e.cfg) appendFile(t, e.cfg.Paths[wire.SrcAuth], "a1") eventually(t, "3 lines", 5*time.Second, func() bool { return hub.count() == 3 }) appendFile(t, e.cfg.Paths[wire.SrcTraefik], "t3\xff") @@ -281,7 +287,7 @@ func TestHubOutageNoLossNoDuplicates(t *testing.T) { e := newEnv(t, srv.URL) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - startRun(ctx, e.cfg) + startRun(t, ctx, e.cfg) tp := e.cfg.Paths[wire.SrcTraefik] appendFile(t, tp, "first") eventually(t, "first", 5*time.Second, func() bool { return hub.count() == 1 }) @@ -316,14 +322,14 @@ func TestRestartResumesFromState(t *testing.T) { tp := e.cfg.Paths[wire.SrcTraefik] appendFile(t, tp, "a", "b", "c") ctx, cancel := context.WithCancel(context.Background()) - done := startRun(ctx, e.cfg) + done := startRun(t, ctx, e.cfg) eventually(t, "3", 5*time.Second, func() bool { return hub.count() == 3 }) cancel() <-done appendFile(t, tp, "d") ctx2, cancel2 := context.WithCancel(context.Background()) defer cancel2() - startRun(ctx2, e.cfg) + startRun(t, ctx2, e.cfg) eventually(t, "4", 5*time.Second, func() bool { return hub.count() >= 4 }) time.Sleep(200 * time.Millisecond) if got := hub.texts(); len(got) != 4 || got[3] != "traefik:d" { @@ -343,7 +349,7 @@ func TestFreshProbeStartsAtHubOffsets(t *testing.T) { hub.offsets[wire.SrcTraefik] = wire.Pos{Inode: ino, Offset: int64(len("old1\nold2\n"))} ctx, cancel := context.WithCancel(context.Background()) defer cancel() - startRun(ctx, e.cfg) + startRun(t, ctx, e.cfg) eventually(t, "new1", 5*time.Second, func() bool { return hub.count() >= 1 }) time.Sleep(200 * time.Millisecond) if got := hub.texts(); len(got) != 1 || got[0] != "traefik:new1" { @@ -360,7 +366,7 @@ func TestHubAckAheadRaisesSeq(t *testing.T) { appendFile(t, e.cfg.Paths[wire.SrcTraefik], "x") ctx, cancel := context.WithCancel(context.Background()) defer cancel() - startRun(ctx, e.cfg) + startRun(t, ctx, e.cfg) eventually(t, "x", 5*time.Second, func() bool { return hub.count() == 1 }) } @@ -374,7 +380,7 @@ func TestPushBackpressure503RetryAfter(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() start := time.Now() - startRun(ctx, e.cfg) + startRun(t, ctx, e.cfg) eventually(t, "x", 8*time.Second, func() bool { return hub.count() == 1 }) if el := time.Since(start); el < 900*time.Millisecond { t.Errorf("retried after %v, Retry-After: 1 not honored", el) @@ -429,7 +435,7 @@ func TestUnauthorizedRevokedMidSession(t *testing.T) { defer srv.Close() e := newEnv(t, srv.URL) appendFile(t, e.cfg.Paths[wire.SrcTraefik], "x") - done := startRun(context.Background(), e.cfg) + done := startRun(t, context.Background(), e.cfg) eventually(t, "x", 5*time.Second, func() bool { return hub.count() == 1 }) revoked.Lock() rev = true @@ -456,7 +462,7 @@ func TestGzipOverThreshold(t *testing.T) { } ctx, cancel := context.WithCancel(context.Background()) defer cancel() - startRun(ctx, e.cfg) + startRun(t, ctx, e.cfg) eventually(t, "20", 5*time.Second, func() bool { return hub.count() == 20 }) hub.mu.Lock() defer hub.mu.Unlock() @@ -476,7 +482,7 @@ func TestSmallPushNotGzipped(t *testing.T) { appendFile(t, e.cfg.Paths[wire.SrcTraefik], "tiny") ctx, cancel := context.WithCancel(context.Background()) defer cancel() - startRun(ctx, e.cfg) + startRun(t, ctx, e.cfg) eventually(t, "1", 5*time.Second, func() bool { return hub.count() == 1 }) hub.mu.Lock() defer hub.mu.Unlock() @@ -501,7 +507,7 @@ func TestCommandsApplied(t *testing.T) { {Seq: 5, Kind: "other", Line: good}, } ctx, cancel := context.WithCancel(context.Background()) - done := startRun(ctx, e.cfg) + done := startRun(t, ctx, e.cfg) eventually(t, "cmds", 5*time.Second, func() bool { s, err := loadState(e.state) return err == nil && s.s.LastCmd == 5 @@ -518,7 +524,7 @@ func TestCommandsApplied(t *testing.T) { } // restart: the hub redelivers everything, nothing is applied twice ctx2, cancel2 := context.WithCancel(context.Background()) - done2 := startRun(ctx2, e.cfg) + done2 := startRun(t, ctx2, e.cfg) time.Sleep(600 * time.Millisecond) cancel2() <-done2 @@ -582,7 +588,7 @@ func TestOversizeLineCountedNotSent(t *testing.T) { appendFile(t, e.cfg.Paths[wire.SrcTraefik], "ok", strings.Repeat("z", wire.MaxLine+10), "after") ctx, cancel := context.WithCancel(context.Background()) defer cancel() - startRun(ctx, e.cfg) + startRun(t, ctx, e.cfg) eventually(t, "2 lines", 5*time.Second, func() bool { return hub.count() == 2 }) time.Sleep(100 * time.Millisecond) if got := hub.texts(); len(got) != 2 || got[1] != "traefik:after" { @@ -598,7 +604,7 @@ func TestMissingSourceWaitsAndNoPathLeak(t *testing.T) { appendFile(t, e.cfg.Paths[wire.SrcTraefik], "x") // auth.log never created ctx, cancel := context.WithCancel(context.Background()) defer cancel() - startRun(ctx, e.cfg) + startRun(t, ctx, e.cfg) eventually(t, "x", 5*time.Second, func() bool { return hub.count() == 1 }) appendFile(t, e.cfg.Paths[wire.SrcTraefik], "y") eventually(t, "status", 5*time.Second, func() bool { @@ -666,7 +672,7 @@ func TestHelperProbe(t *testing.T) { t.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) - done := startRun(ctx, cfg) + done := startRun(t, ctx, cfg) io.Copy(io.Discard, os.Stdin) // parent closes stdin when done cancel() <-done