carlos / platform Public

activator: an awake entry's route never changes; a read size column waits as pending for the next swap (final review)

The size pass wrote e.route.Size on an awake entry. A flat sidecar restart
then sized the running instance's shared cgroup from it (a live memory
change, against ruling 12), and afterWake's unlocked route copies raced
the write. The pass now records the column in pendingSize; swap admission
builds the successor's route from it (sizedRouteLocked), SizeState's Want
reads it, and every other spawn on the running host keeps the route it
woke with.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev bca33afa4e4a6e90b2ddc309b4fb9017bff4e27e parent c4c5f2b
4 files changed, +272 −18
  • internal/activator/activator.go +16 −6
  • internal/activator/size.go +21 −8
  • internal/activator/size_pending_test.go +227 −0
  • internal/activator/swap.go +8 −4

CI failed — run details

diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index 1fbe631fd..b28d18143 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -623,12 +623,22 @@ type actEntry struct {
// flight, from its registry compare to its end; unlike ops.resizing it
// holds nothing back (no swap, stop or lease waits on it).
// sizeCheckedAt paces the slow backstop compare.
- sizeSeen string
- sizeSeenSet bool
- sizeDirty bool
- sizeOwed bool
- sizeChecking bool
- sizeCheckedAt time.Time
+ // pendingSize, when pendingSizeSet, is the size column the size pass
+ // last read: the size the next swap's successor starts from
+ // (sizedRouteLocked). It never changes e.route: an awake entry's route
+ // is the one it woke with, so a sidecar start or any other spawn on the
+ // running host sizes memory as the instance started (spec §5.4 rev 10),
+ // and readers that copy e.route off a.mu (afterWake) see a fixed value.
+ // The entry is dropped when the host sleeps; the next wake reads the
+ // registry afresh.
+ sizeSeen string
+ sizeSeenSet bool
+ pendingSize string
+ pendingSizeSet bool
+ sizeDirty bool
+ sizeOwed bool
+ sizeChecking bool
+ sizeCheckedAt time.Time
// genReaping is true while reapCurrentGen is running for this pending
// stop, so finishPendingStops does not start a second one. Written
// under a.mu.
diff --git a/internal/activator/size.go b/internal/activator/size.go
index 7bdcada4c..8d3fcda1b 100644
--- a/internal/activator/size.go
+++ b/internal/activator/size.go
@@ -128,12 +128,24 @@ func (a *Service) SizeState(host string) (SizeState, bool) {
return SizeState{}, false
}
st := SizeState{Gen: genUnitOf(e.route, e.gen), Raised: e.ops.raised}
- rt, build := e.route, e.gen.Build
+ rt, build := a.sizedRouteLocked(e), e.gen.Build
a.mu.Unlock()
st.Want = EffectiveSize(a.tierSource(), rt, build)
return st, true
}
+// sizedRouteLocked is e's route with the size column the size pass last
+// read (pendingSize), or e.route as it woke when no pass has read one: the
+// route a swap's successor starts from and SizeState's Want. It is a copy;
+// e.route is not changed. The caller holds a.mu.
+func (a *Service) sizedRouteLocked(e *actEntry) registry.Route {
+ rt := e.route
+ if e.pendingSizeSet {
+ rt.Size = e.pendingSize
+ }
+ return rt
+}
+
// NoteSize says host's size column may have moved (the agent's
// PolicyChanged): the next sweep compares it at once rather than at the
// backstop. It never blocks on anything but a.mu.
@@ -213,9 +225,10 @@ func (a *Service) endSizeCheck(e *actEntry, redo bool) {
// no write owed it ends after the registry read, with no root call.
// Otherwise it sends the effective size once: root sets the slice's CPU
// and IO shares and never memory (spec §5.4 rev 10). Before the call the
-// size is recorded on the route, whatever the call's outcome, and the next
-// start, an overlap successor's included, sizes its memory from it (Astra
-// plan R6-1). The call is applied when the read-back shows the size's
+// size is recorded as pending, whatever the call's outcome: the next swap's
+// successor sizes its memory from it, and the next wake reads the column
+// afresh (Astra plan R6-1). The awake entry's route, which a sidecar start
+// on the running host spawns from, is never changed. The call is applied when the read-back shows the size's
// shares; anything else leaves the CPU and IO write owed, and the next
// sweep sends it again. A refusal (root under host claims, a host with no
// live change) is not retried.
@@ -253,10 +266,10 @@ func (a *Service) sizePass(e *actEntry, s sizeSnap) {
return
}
// The column is read and the entry is the one the snapshot was taken
- // from: the route records it now, whatever the CPU and IO write below
- // does, so the next start or swap sizes its memory from it. Only the
- // CPU and IO write can be owed.
- e.sizeSeen, e.route.Size = fresh.Size, fresh.Size
+ // from: it is recorded as pending now, whatever the CPU and IO write
+ // below does, so the next swap sizes its memory from it. Only the CPU
+ // and IO write can be owed. e.route is never written: see pendingSize.
+ e.sizeSeen, e.pendingSize, e.pendingSizeSet = fresh.Size, fresh.Size, true
done := make(chan struct{})
e.ops.resizing, e.ops.resizeDone = true, done
a.mu.Unlock()
diff --git a/internal/activator/size_pending_test.go b/internal/activator/size_pending_test.go
new file mode 100644
index 000000000..58b1045fc
--- /dev/null
+++ b/internal/activator/size_pending_test.go
@@ -0,0 +1,227 @@
+package activator
+
+import (
+ "context"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strconv"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/cgroup"
+ "github.com/carlosframework/platform/internal/instancectl"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// cgroupSidecarBackend is the swap fixture's backend whose legacy sidecar
+// starts also go through the real flat spawn seam (ExecBackend.
+// spawnInCgroupAt with no child: the host cgroup, sized from the route the
+// activator hands it) onto a real cgroup.Manager over a temp-dir fixture,
+// so applyLimit's memory.max for the host is what the test reads. The
+// sidecar process itself is the fixture's fakeProc, so the test can crash
+// it.
+type cgroupSidecarBackend struct {
+ *fakeSwapBackend
+ eb *ExecBackend
+ root string
+ t *testing.T
+}
+
+func newCgroupSidecarBackend(t *testing.T, fb *fakeSwapBackend) *cgroupSidecarBackend {
+ t.Helper()
+ root := t.TempDir()
+ for _, f := range []string{"cgroup.procs", "cgroup.subtree_control"} {
+ if err := os.WriteFile(filepath.Join(root, f), nil, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ m, err := cgroup.SetupAt(root, t.Logf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return &cgroupSidecarBackend{fakeSwapBackend: fb, root: root, t: t,
+ eb: &ExecBackend{Cgroups: m, Tier: func(registry.Route) cgroup.Limit { return cgroup.TierDefault }}}
+}
+
+func (b *cgroupSidecarBackend) spawn(rt registry.Route) {
+ cmd, settle, err := b.eb.spawnInCgroupAt(rt, "", false, func() *exec.Cmd { return exec.Command("/bin/true") })
+ if err != nil {
+ b.t.Errorf("flat sidecar spawn: %v", err)
+ return
+ }
+ _ = cmd.Wait()
+ settle()
+}
+
+func (b *cgroupSidecarBackend) StartSidecar(ctx context.Context, rt registry.Route) (Proc, error) {
+ b.spawn(rt)
+ return b.fakeSwapBackend.StartSidecar(ctx, rt)
+}
+
+func (b *cgroupSidecarBackend) StartSidecarGen(ctx context.Context, rt registry.Route, g Gen) (Proc, error) {
+ b.spawn(rt)
+ return b.fakeSwapBackend.StartSidecarGen(ctx, rt, g)
+}
+
+// memoryMax is the host cgroup's memory.max as applyLimit last wrote it.
+func (b *cgroupSidecarBackend) memoryMax(host string) string {
+ raw, err := os.ReadFile(filepath.Join(b.root, "carlos-exec@"+host, "memory.max"))
+ if err != nil {
+ return err.Error()
+ }
+ return string(raw)
+}
+
+// Final review finding 1 (ruling 12): a flat host with generations off has
+// no live size (the pass is refused), and a size change must not reach the
+// running instance's shared cgroup through a sidecar restart either. Awake
+// at one size, the column moves to another and the pass runs; the sidecar
+// crashes and the supervisor restarts it: the host cgroup's memory.max
+// stays what the instance woke at, for a shrink and for a growth.
+func TestASidecarRestartKeepsTheRunningHostsMemory(t *testing.T) {
+ t.Parallel()
+ for _, tc := range []struct {
+ name string
+ from, to string
+ want int64
+ }{
+ {"shrink", "large", "small", gib},
+ {"growth", "small", "large", int64(1 << 28)},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+ f := newSizeFixture(t, tc.from, tc.want)
+ f.a.gens = false
+ f.b.sidecarStaged = true
+ f.b.setSizeHook(func(cgroup.Size) (instancectl.SizeReadBack, error) {
+ return instancectl.SizeReadBack{}, errSizeNotLive // a flat row has no live size
+ })
+ cb := newCgroupSidecarBackend(t, f.b)
+ f.a.Bounce(f.host)
+ waitFor(t, "the stop", func() bool { return f.a.entryCount() == 0 })
+ f.a.mu.Lock()
+ f.a.backend = cb
+ f.a.mu.Unlock()
+ rt := testRoute(f.host)
+ rt.Size = tc.from
+ f.routes.put(rt)
+ arrangeAwake(t, f.a, rt)
+ want := ""
+ if s, ok := cgroup.SizeByName(tc.from); ok {
+ want = strconv.FormatInt(s.Limit.MemoryMax, 10)
+ }
+ if got := cb.memoryMax(f.host); got != want || f.b.sidecarStartCount() != 1 {
+ t.Fatalf("after the wake: memory.max %q, %d sidecar starts; want %s, 1", got, f.b.sidecarStartCount(), want)
+ }
+
+ f.operatorSets(tc.to)
+ f.sweep(t)
+ if got := f.sizeEvents(); len(got) != 1 {
+ t.Fatalf("size calls %v, want the one refused call", got)
+ }
+ if st := f.state(t); st.Want.Name != tc.to {
+ t.Fatalf("SizeState = %+v, want %s pending", st, tc.to)
+ }
+
+ f.b.lastSidecar().kill()
+ f.clk.advance(3 * time.Second)
+ f.a.sweepOnce()
+ waitFor(t, "the supervised restart", func() bool { return f.a.sidecarSettled(f.host) && f.b.sidecarStartCount() == 2 })
+ if got := cb.memoryMax(f.host); got != want {
+ t.Fatalf("after the sidecar restart: host memory.max %q, want %s as the instance woke (no live memory change)", got, want)
+ }
+ })
+ }
+}
+
+// sizingPlanBackend is the planned-wake fixture's backend with the live
+// size seam: every call is applied, and its tier reads the size column.
+type sizingPlanBackend struct {
+ *planGenBackend
+ mu sync.Mutex
+ calls int
+}
+
+func (b *sizingPlanBackend) ResizeLive(_ context.Context, _ registry.Route, size cgroup.Size) (instancectl.SizeReadBack, error) {
+ b.mu.Lock()
+ b.calls++
+ b.mu.Unlock()
+ return sizeApplied(size, half), nil
+}
+
+// GenTier reads the size column, as ExecBackend's does.
+func (b *sizingPlanBackend) GenTier(rt registry.Route, _ string) cgroup.Limit {
+ return SizedLimit(rt, cgroup.TierDefault)
+}
+
+func (b *sizingPlanBackend) count() int {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ return b.calls
+}
+
+// Final review finding 2: a planned wake reports its outcome (afterWake)
+// off a.mu with copies of the entry's route, after the entry is awake. A
+// size change and its sweep landing then must not write the route under
+// it. The wake is held inside OnGen, after afterWake has copied the route
+// into its event and before it takes a.mu again, while the column moves
+// and a size pass runs to its call. The test learns nothing from the wake
+// goroutine past its last unlock (no channel from OnGen): it sweeps until
+// the pass has made its call, after seeing the entry awake under a.mu. So a write of e.route by the pass is
+// unordered with afterWake's read, and -race reports it; the route check
+// at the end catches it without -race.
+func TestASizePassDuringPlannedWakeCompletionLeavesTheRouteAlone(t *testing.T) {
+ t.Parallel()
+ dir := t.TempDir()
+ next := stagedID(t, dir, "v2")
+ a, pb, _ := planActivator(t, WakePlan{Boot: next, Mode: ModeOverlap})
+ b := &sizingPlanBackend{planGenBackend: pb}
+ a.backend = b
+ host := "one.example.com"
+ rt := testRoute(host)
+ routes := &liveRoutes{rows: map[string]registry.Route{host: rt}}
+ a.Routes = routes
+ release := make(chan struct{})
+ var once sync.Once
+ a.OnGen = func(ev GenEvent) {
+ if ev.Kind == GenHealthy {
+ once.Do(func() { <-release })
+ }
+ }
+ woke := make(chan error, 1)
+ go func() {
+ done, err := a.beginRequest(context.Background(), rt)
+ if err == nil {
+ done()
+ }
+ woke <- err
+ }()
+ // Awake is published under a.mu before afterWake runs: reading it
+ // orders the test after that unlock only.
+ waitFor(t, "the entry awake", func() bool { _, ok := a.SizeState(host); return ok })
+ routes.setSize(host, "xlarge")
+ for deadline := time.Now().Add(10 * time.Second); b.count() == 0; time.Sleep(5 * time.Millisecond) {
+ if time.Now().After(deadline) {
+ t.Fatal("no size pass made its call while the wake completed")
+ }
+ a.NoteSize(host)
+ a.sweepOnce()
+ }
+ waitFor(t, "the size pass to finish", func() bool { return a.sizeIdleForTest(host) })
+ close(release)
+ if err := <-woke; err != nil {
+ t.Fatalf("the wake: %v", err)
+ }
+ st, ok := a.SizeState(host)
+ if !ok || st.Want.Name != "xlarge" {
+ t.Fatalf("SizeState = %+v, %v; want xlarge pending", st, ok)
+ }
+ a.mu.Lock()
+ got := a.entries[host].route.Size
+ a.mu.Unlock()
+ if got != "" {
+ t.Fatalf("the awake entry's route size = %q, want the wake's \"\" unchanged", got)
+ }
+}
diff --git a/internal/activator/swap.go b/internal/activator/swap.go
index 21a54f199..9d1b04632 100644
--- a/internal/activator/swap.go
+++ b/internal/activator/swap.go
@@ -437,8 +437,9 @@ func (a *Service) swapOverlap(ctx context.Context, gb SwapBackend, at Attempt, n
}
var (
e *actEntry
- rt registry.Route
- incumbent Gen // the current generation at admission, for its tier
+ rt registry.Route // the successor's: the size column the size pass last read
+ wakeRt registry.Route // the entry's own, as it woke
+ incumbent Gen // the current generation at admission, for its tier
)
// Step 1: the checks and joining the set, inside the selected work. Only
// the registration runs there. A claims-on spawn re-enters the work
@@ -482,7 +483,10 @@ func (a *Service) swapOverlap(ctx context.Context, gb SwapBackend, at Attempt, n
return errSwapBusy
}
a.addMemberLocked(e, m)
- rt, incumbent = e.route, e.gen
+ // The successor starts from the size column the size pass last
+ // read (sizedRouteLocked), so a deploy lands a new size's memory.
+ // The entry's own route is never changed while it is awake.
+ rt, wakeRt, incumbent = a.sizedRouteLocked(e), e.route, e.gen
return nil
})
if err != nil {
@@ -499,7 +503,7 @@ func (a *Service) swapOverlap(ctx context.Context, gb SwapBackend, at Attempt, n
// the overlap parent, the successor's child, and the restore (leaveSet
// restores to the current generation's Tier).
if incumbent.Tier == (cgroup.Limit{}) {
- incumbent.Tier = gb.GenTier(rt, incumbent.Build)
+ incumbent.Tier = gb.GenTier(wakeRt, incumbent.Build)
}
succTier := gb.GenTier(rt, at.Build)
// With an operator size the successor starts at the larger of its own