carlos / platform Public

activator: roleEmptyBound, so a test can shrink the role cgroup wait

waitRoleCgroupEmpty's own deadline was actStopTimeout (20 s) inline; it
is now roleEmptyBound, a var initialised to actStopTimeout, so production
waits and words are unchanged. The seed test that must wait the bound
out pins the default and runs on 300 ms (was 20 s).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev eb2cb869f1c818235887be863cb3b9c9aa16a7da parent 8161f71
2 files changed, +19 −8
  • internal/activator/backend_exec.go +9 −4
  • internal/activator/unitproc_test.go +10 −4
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go
index ee0d2ec6c..738ff0ccc 100644
--- a/internal/activator/backend_exec.go
+++ b/internal/activator/backend_exec.go
@@ -1629,7 +1629,7 @@ func (b *ExecBackend) seedUnitFailed(ctx context.Context, rt registry.Route, dbP
}
// waitSeedCgroupEmpty polls the seed unit's cgroup.events until it reports
-// empty or actStopTimeout elapses, honouring ctx and b.ObservePoll like the
+// empty or roleEmptyBound elapses, honouring ctx and b.ObservePoll like the
// exit observer (unitproc.go). A timeout or a read error is returned to the
// caller as a log line, not a failure: it is the deciding result's
// confidence that is at stake here, not the wake itself.
@@ -1637,8 +1637,12 @@ func (b *ExecBackend) waitSeedCgroupEmpty(ctx context.Context, rt registry.Route
return b.waitRoleCgroupEmpty(ctx, rt, instancectl.RoleSeed)
}
+// roleEmptyBound is how long waitRoleCgroupEmpty waits for a role's cgroup
+// to empty: actStopTimeout. A var so tests can shrink it.
+var roleEmptyBound = actStopTimeout
+
// waitRoleCgroupEmpty polls role's unit's cgroup.events until it reports
-// empty or actStopTimeout elapses, honouring ctx and b.ObservePoll.
+// empty or roleEmptyBound elapses, honouring ctx and b.ObservePoll.
func (b *ExecBackend) waitRoleCgroupEmpty(ctx context.Context, rt registry.Route, role instancectl.Role) error {
events := b.CgroupEvents
if events == nil {
@@ -1650,7 +1654,8 @@ func (b *ExecBackend) waitRoleCgroupEmpty(ctx context.Context, rt registry.Route
}
slice := instancectl.SliceName(rt.Host)
unit := instancectl.UnitName(role, rt.Host)
- deadline := time.Now().Add(actStopTimeout)
+ bound := roleEmptyBound
+ deadline := time.Now().Add(bound)
for {
exists, populated, err := events(slice, unit)
if err != nil {
@@ -1660,7 +1665,7 @@ func (b *ExecBackend) waitRoleCgroupEmpty(ctx context.Context, rt registry.Route
return nil
}
if time.Now().After(deadline) {
- return fmt.Errorf("processes remained after %s", actStopTimeout)
+ return fmt.Errorf("processes remained after %s", bound)
}
select {
case <-ctx.Done():
diff --git a/internal/activator/unitproc_test.go b/internal/activator/unitproc_test.go
index cfaca58bb..d65dca226 100644
--- a/internal/activator/unitproc_test.go
+++ b/internal/activator/unitproc_test.go
@@ -514,10 +514,16 @@ func TestStopClosesListenersAfterDoneWithNoSecondStopCall(t *testing.T) {
// TestSeedUnitFailureStopsBeforeCleanupAndKeepsTheDBWhenProcessesRemain
// covers the ordering rule: Control.Stop for the seed unit runs, and is
// proven by an empty cgroup, BEFORE any database cleanup, and when the
-// cgroup never reports empty, nothing is removed at all. The
-// wait is bounded by actStopTimeout, a const this package does not let a
-// test shrink, so this test genuinely waits out that bound.
+// cgroup never reports empty, nothing is removed at all. The wait is
+// bounded by roleEmptyBound (actStopTimeout in production), which this test
+// genuinely waits out, shrunk to a fraction of a second.
func TestSeedUnitFailureStopsBeforeCleanupAndKeepsTheDBWhenProcessesRemain(t *testing.T) {
+ if roleEmptyBound != actStopTimeout {
+ t.Fatalf("roleEmptyBound = %v, want actStopTimeout (%v) outside a test", roleEmptyBound, actStopTimeout)
+ }
+ old := roleEmptyBound
+ roleEmptyBound = 300 * time.Millisecond
+ t.Cleanup(func() { roleEmptyBound = old })
ctl := &fakeControl{seed: instancectl.SeedResponse{InvocationID: "s1", Result: "exit-code", Status: "3", Code: "exited"}}
unit := &fakeUnit{}
b, rt := nestedBackend(t, ctl, unit)
@@ -531,7 +537,7 @@ func TestSeedUnitFailureStopsBeforeCleanupAndKeepsTheDBWhenProcessesRemain(t *te
}
}
// The cgroup stays populated for the whole wait: a process from the
- // seed survived SIGKILL and is still there when actStopTimeout elapses.
+ // seed survived SIGKILL and is still there when the bound elapses.
unit.set("active", "s1", true, true)
err := b.Seed(context.Background(), rt)
if err == nil || !strings.Contains(err.Error(), "processes remain") {