carlos / platform Public

activator: tests wait for wakes and settles that write after answering

Activator tests failed now and then on CI with "TempDir RemoveAll
cleanup: directory not empty" (TestStatelessMovedBeforeSpawn,
TestReplicaReleaseProofBeforeSuccess,
TestReplicaReconcileMemberWakeVerifiesGeneration,
TestNoWorkerAfterAFailedRelease). Goroutine dumps at cleanup show two
writers still in flight after the test had its answer, both by
design:
- a wake closes e.ready, which answers the request, and then records
  its outcome and writes the generations file (Service.wake);
- a release whose stop was not proven settles later, and its proof
  writes the file (cmdHold.settle -> onProven -> releaseProven) after
  the stop returned.
Shutdown waits for neither, so t.TempDir's RemoveAll raced the write.

Service.background now counts both (wake from its first statement,
before e.ready closes; each settle attempt until its callbacks ran).
It is bookkeeping only: nothing in the agent waits on it. The
release and wake/release rigs wait for it in a cleanup that runs
before their temp dirs are removed (waitBackground).

On DOCS-T1 with 24 spinners on 16 cores, the four tests failed 17
times in 240 runs before and 0 in 240 after.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev edbf1fd026371053b72efa62665a34c121412971 parent a178a75
6 files changed, +62 −6
  • internal/activator/activator.go +9 −0
  • internal/activator/background_test.go +27 −0
  • internal/activator/cmd.go +23 −6
  • internal/activator/rootfs_wake_release_test.go +1 −0
  • internal/activator/rootfsrelease.go +1 −0
  • internal/activator/rootfsrelease_test.go +1 −0

CI failed — run details

diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index d332d4f96..9588a46f5 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -1617,6 +1617,13 @@ type Service struct {
// OnWake.
GenerationsFile string
+ // background counts work that can still write the box's files after
+ // its caller has its answer: wake goroutines (a wake closes e.ready and
+ // then records the outcome and the generations file) and command settle
+ // attempts with their proof's callbacks (cmdHold.settle). Nothing in
+ // the agent waits on it; tests do, before their files go (#897).
+ background atomic.Int64
+
// schedMu guards schedFences, each host's schedule write fence
// (schedfence.go). It is never held with a.mu or a fence's own lock.
schedMu sync.Mutex
@@ -2083,6 +2090,8 @@ func (a *Service) refuseWakeLocked(e *actEntry, cancel context.CancelFunc) {
// requests still need the wake to finish even if the requester who started it
// hangs up.
func (a *Service) wake(e *actEntry) {
+ a.background.Add(1)
+ defer a.background.Add(-1)
if a.wakeEnded != nil {
defer a.wakeEnded(e.host)
}
diff --git a/internal/activator/background_test.go b/internal/activator/background_test.go
new file mode 100644
index 000000000..a52ff26d4
--- /dev/null
+++ b/internal/activator/background_test.go
@@ -0,0 +1,27 @@
+package activator
+
+import (
+ "testing"
+ "time"
+)
+
+// waitBackground waits until a has no background work in flight
+// (Service.background): no wake goroutine and no command settle attempt.
+// Both can write the generations file after their caller has its answer:
+// a wake records its outcome after it closes e.ready, and a release whose
+// stop was not proven settles later, its proof writing the file
+// (releaseProven) after the stop returned. A rig calls this
+// from a cleanup registered after its t.TempDir calls, so it runs before
+// they are removed; without it RemoveAll raced the write (#897: "TempDir
+// RemoveAll cleanup: directory not empty").
+func waitBackground(t *testing.T, a *Service) {
+ t.Helper()
+ deadline := time.Now().Add(10 * time.Second)
+ for a.background.Load() > 0 {
+ if time.Now().After(deadline) {
+ t.Errorf("%d wakes or settle attempts still in flight after 10s", a.background.Load())
+ return
+ }
+ time.Sleep(time.Millisecond)
+ }
+}
diff --git a/internal/activator/cmd.go b/internal/activator/cmd.go
index da1a3dd75..7a93634ec 100644
--- a/internal/activator/cmd.go
+++ b/internal/activator/cmd.go
@@ -6,6 +6,7 @@ import (
"fmt"
"log"
"sync"
+ "sync/atomic"
"time"
"github.com/carlosframework/platform/internal/instancectl"
@@ -130,12 +131,15 @@ type cmdHold struct {
// it, under a.mu, so a stop interrupts a download.
cancelPrepare context.CancelFunc
started chan struct{} // closed once StartCmd returned, either way
- unit CmdUnit // written before started is closed; nil when no unit can be running
- now func() time.Time
- mu sync.Mutex
- asked bool // a stop has been asked for; under mu
- reason string
- askedAt time.Time // when the first stop was asked for; under mu
+ // settles is its Service's background count (Service.background): each
+ // settle attempt counts until its proof's callbacks have run.
+ settles *atomic.Int64
+ unit CmdUnit // written before started is closed; nil when no unit can be running
+ now func() time.Time
+ mu sync.Mutex
+ asked bool // a stop has been asked for; under mu
+ reason string
+ askedAt time.Time // when the first stop was asked for; under mu
// settling is the settle attempt in flight, nil when none is. A stop
// joins it rather than starting a second one. Under mu.
settling *cmdSettle
@@ -177,6 +181,13 @@ func (h *cmdHold) settled() {
}
}
+// settleStarted counts a settle attempt about to start (see settles).
+func (h *cmdHold) settleStarted() {
+ if h.settles != nil {
+ h.settles.Add(1)
+ }
+}
+
// cmdSettle is one settle attempt: done closes when it has ended, and err
// is its result, written before done closes.
type cmdSettle struct {
@@ -227,6 +238,7 @@ func (h *cmdHold) stop(ctx context.Context, reason string) error {
if at == nil {
at = &cmdSettle{done: make(chan struct{})}
h.settling = at
+ h.settleStarted()
go h.settle(at)
}
h.mu.Unlock()
@@ -245,6 +257,9 @@ func (h *cmdHold) stop(ctx context.Context, reason string) error {
// goroutine, and no stop waits for them (fix round 2: proof and the
// callback's completion are different states, CmdSpec.AfterSettled).
func (h *cmdHold) settle(at *cmdSettle) {
+ if h.settles != nil {
+ defer h.settles.Add(-1)
+ }
err := h.unit.StopAndSettle(context.Background())
h.mu.Lock()
at.err = err
@@ -286,6 +301,7 @@ func (h *cmdHold) retrySettle(released bool, every time.Duration) {
}
at := &cmdSettle{done: make(chan struct{})}
h.settling = at
+ h.settleStarted()
go h.settle(at)
}
@@ -768,6 +784,7 @@ func (a *Service) RunCmd(ctx context.Context, rt registry.Route, spec CmdSpec) (
pctx, pcancel := context.WithCancel(ctx)
defer pcancel()
h := newCmdHold(spec.RunID, role, pcancel, a.now)
+ h.settles = &a.background
h.afterSettled = spec.AfterSettled
a.mu.Lock()
e := a.entries[rt.Host]
diff --git a/internal/activator/rootfs_wake_release_test.go b/internal/activator/rootfs_wake_release_test.go
index ef5968c3c..cb4a20a93 100644
--- a/internal/activator/rootfs_wake_release_test.go
+++ b/internal/activator/rootfs_wake_release_test.go
@@ -296,6 +296,7 @@ func (r *wakeRelRig) service(t *testing.T) *Service {
r.b.setStuck(false)
r.clk.advance(actStopTimeout + time.Second)
a.Shutdown(context.Background())
+ waitBackground(t, a)
})
return a
}
diff --git a/internal/activator/rootfsrelease.go b/internal/activator/rootfsrelease.go
index 737b80520..ae8280aeb 100644
--- a/internal/activator/rootfsrelease.go
+++ b/internal/activator/rootfsrelease.go
@@ -425,6 +425,7 @@ func (a *Service) runReleaseAdmitted(ctx context.Context, e *actEntry, rt regist
defer cancel(nil)
op := &releaseOp{host: host, build: id, cancel: cancel, owner: e, state: releaseRunning, done: make(chan struct{})}
op.hold = newCmdHold("release", instancectl.RoleCmd, func() { cancel(context.Canceled) }, a.now)
+ op.hold.settles = &a.background
op.hold.starting = true
op.hold.onProven = func() { a.releaseProven(op) }
startedClosed := false
diff --git a/internal/activator/rootfsrelease_test.go b/internal/activator/rootfsrelease_test.go
index 2a39a13cb..bdac13487 100644
--- a/internal/activator/rootfsrelease_test.go
+++ b/internal/activator/rootfsrelease_test.go
@@ -626,6 +626,7 @@ func newReleaseRig(t *testing.T) *releaseRig {
a.entries[rt.Host] = e
a.mu.Unlock()
id := release.StagedIdentity{Version: "v2", StagedPath: filepath.Join(t.TempDir(), "v2", "rootfs.squashfs"), Digest: "bb22"}
+ t.Cleanup(func() { waitBackground(t, a) })
return &releaseRig{a: a, b: b, clk: clk, e: e, rt: rt, id: id, sink: sink, held: newOwnedLease(time.Now().Add(time.Hour), false)}
}