carlos / platform Public

activator: a rootfs admission holds for the row it judged: the shared gate ask is keyed by the row, and a refreshed or re-read row is admitted again before Acquire (Task 7 round 1, P1 1)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 07419af8a3c0a365bc1643ea760dde418a992934 parent 8a63adf
3 files changed, +255 −27
  • internal/activator/activator.go +24 −13
  • internal/activator/rootfs_gate_test.go +204 −3
  • internal/activator/rootfs_service.go +27 −11
diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index 2e76f7893..70c9212dd 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -468,11 +468,12 @@ const (
type actEntry struct {
claimResult *wokenInstance
- // rootfsAdmitted: begin made this entry right after the backend's
- // rootfs gates passed for it (rootfsStartRefusal), so doWake asks only
- // the cheap locked half again (rootfsWakeRefusal). Set at construction,
- // before the wake goroutine starts; never written again.
- rootfsAdmitted bool
+ // rootfsAdmittedKey: begin made this entry right after the backend's
+ // rootfs gates passed for the row with this key (rootfsGateKey), so
+ // doWake asks only the cheap locked half again for that same row and
+ // the whole admission for any other (rootfsWakeRefusal). Set at
+ // construction, before the wake goroutine starts; never written again.
+ rootfsAdmittedKey string
// probe is set for an entry ProbeWake made (moves Plan 3 Task 5): the
// claim path runs with the probe's policy, and the entry is held for
// the move while its registry row is.
@@ -1533,10 +1534,10 @@ func (a *Service) begin(ctx context.Context, rt registry.Route, stamp bool, req
return nil, err
}
extended := false
- // gated: this request asked the backend's rootfs gates for rt (off
- // a.mu) and they passed; the next pass of the loop may make the entry.
- // Spent on that pass, whatever it finds.
- gated := false
+ // gated: the key of rt when this request asked the backend's rootfs
+ // gates for it (off a.mu) and they passed; the next pass of the loop
+ // may make the entry. Spent on that pass, whatever it finds.
+ gated := ""
for {
a.mu.Lock()
if a.fences[rt.Host] != 0 {
@@ -1556,7 +1557,7 @@ func (a *Service) begin(ctx context.Context, rt registry.Route, stamp bool, req
}
e := a.entries[rt.Host]
admitted := gated
- gated = false
+ gated = ""
if e == nil {
// The rootfs admission (Plan 3 Task 7, INV-3), before an entry
// is made, so a refused row never reaches Acquire, Restore or
@@ -1571,7 +1572,7 @@ func (a *Service) begin(ctx context.Context, rt registry.Route, stamp bool, req
a.noteRootfsRefusal(rt, err)
return nil, err
}
- if rt.Rootfs() && !admitted {
+ if rt.Rootfs() && admitted != rootfsGateKey(rt) {
a.mu.Unlock()
// Within this request's own wait bound, like the wait on a
// wake: a gate that outlasts it answers errWakeTimeout.
@@ -1586,7 +1587,7 @@ func (a *Service) begin(ctx context.Context, rt registry.Route, stamp bool, req
}
return nil, err
}
- gated = true
+ gated = rootfsGateKey(rt)
continue
}
// Asleep → kick off ONE wake; this request and any latecomers all
@@ -1601,7 +1602,7 @@ func (a *Service) begin(ctx context.Context, rt registry.Route, stamp bool, req
// found nothing and started a second wake — two lease acquires
// and two restore attempts per request (#184, seen only as a
// loaded-suite test flake because the window is that narrow).
- e = &actEntry{host: rt.Host, route: rt, state: actWaking, ready: make(chan struct{}), rootfsAdmitted: admitted}
+ e = &actEntry{host: rt.Host, route: rt, state: actWaking, ready: make(chan struct{}), rootfsAdmittedKey: admitted}
a.entries[rt.Host] = e
go a.wake(e)
edge.MarkWoke(req)
@@ -2264,6 +2265,7 @@ func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, erro
if err := a.rootfsWakeRefusal(ctx, e, rt); err != nil {
return w, err
}
+ admittedRow := rootfsGateKey(rt)
// 0.5. Refuse a fenced or conflicting row before it ever reaches Acquire
// or Restore (design §6.8). The in-Start/in-Seed refusal below still
@@ -2367,6 +2369,15 @@ func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, erro
}
}
+ // 0.9. The row the admission judged is the row the lease and restore
+ // act on (INV-3): a re-read since step 0.4 (the held wake's, 0.65) that
+ // changed any gate field is admitted again, in full, before Acquire.
+ if rootfsGateKey(rt) != admittedRow {
+ if err := a.rootfsStartRefusal(ctx, rt); err != nil {
+ return w, err
+ }
+ }
+
// 1. The lease first: everything after this line is done as the prefix's
// single writer. Contention is not retried here — on one box it means a
// previous incarnation hasn't released; tell the client to come back after
diff --git a/internal/activator/rootfs_gate_test.go b/internal/activator/rootfs_gate_test.go
index dc294be42..02041d725 100644
--- a/internal/activator/rootfs_gate_test.go
+++ b/internal/activator/rootfs_gate_test.go
@@ -578,10 +578,13 @@ func TestRootfsGatesRunOffTheLock(t *testing.T) {
func (a *Service) rootfsGateWaiters(host string) int {
a.rootfsGateMu.Lock()
defer a.rootfsGateMu.Unlock()
- if r := a.rootfsGateRuns[host]; r != nil {
- return r.waiters
+ n := 0
+ for _, r := range a.rootfsGateRuns {
+ if r.host == host {
+ n += r.waiters
+ }
}
- return 0
+ return n
}
// The first-wake budget is the row's own: a rootfs row's health bound plus
@@ -619,3 +622,201 @@ func TestRootfsPlannedFirstWakeGetsTheRootfsBudget(t *testing.T) {
}
evs.wait(t, GenHealthy)
}
+
+// routeTable is a RouteLookup whose rows the test sets.
+type routeTable struct {
+ mu sync.Mutex
+ rows map[string]registry.Route
+}
+
+func (r *routeTable) Get(host string) (*registry.Route, error) {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+ rt, ok := r.rows[host]
+ if !ok {
+ return nil, nil
+ }
+ return &rt, nil
+}
+
+// [Astra Task 7 R1 1, INV-3] Admission authorizes the row whose storage is
+// touched: begin admits the edge's cached nested rootfs row, but the
+// registry's current row for the host has foreign paths (wakeRouteChanged
+// lets DBPath and Socket move). doWake's refresh swaps in the current row,
+// and the admission judges it afresh before Acquire: refused (it is not
+// nested), with no lease and no restore.
+func TestAdmissionFollowsTheRefreshedRow(t *testing.T) {
+ eb, _ := newRootfsBackend(t)
+ cached := rootfsRoute(eb)
+ current := cached
+ current.Socket = filepath.Join(eb.RunDirOverride, "elsewhere.sock")
+ a, b, _, _ := gateService(t)
+ var mu sync.Mutex
+ judged := map[string]int{}
+ b.startable = func(ctx context.Context, rt registry.Route) error {
+ mu.Lock()
+ judged[rt.Socket]++
+ mu.Unlock()
+ return eb.rootfsStartable(ctx, rt)
+ }
+ a.Routes = &routeTable{rows: map[string]registry.Route{current.Host: current}}
+ _, err := a.beginRequest(context.Background(), cached)
+ if err == nil || !errors.Is(err, errRootfsNotAdmitted) || !strings.Contains(err.Error(), "nested isolation") {
+ t.Fatalf("wake = %v, want the current (foreign) row refused before the lease", err)
+ }
+ if got := b.gateCounts(); got.acquires != 0 || got.restores != 0 {
+ t.Fatalf("the refreshed row reached the lease: %+v", got)
+ }
+ mu.Lock()
+ defer mu.Unlock()
+ if judged[cached.Socket] != 1 || judged[current.Socket] != 1 {
+ t.Fatalf("rows judged %v: want the cached row once (begin) and the current row once (doWake)", judged)
+ }
+}
+
+// Two different rows of one host checked at once are asked apart, each
+// getting its own answer: the shared ask is keyed by the row, not the host.
+func TestRootfsGatesAreSharedPerRowNotPerHost(t *testing.T) {
+ eb, _ := newRootfsBackend(t)
+ nested := rootfsRoute(eb)
+ foreign := nested
+ foreign.Socket = filepath.Join(eb.RunDirOverride, "elsewhere.sock")
+ a, b, _, _ := gateService(t)
+ release := make(chan struct{})
+ var mu sync.Mutex
+ asked := 0
+ b.startable = func(ctx context.Context, rt registry.Route) error {
+ mu.Lock()
+ asked++
+ mu.Unlock()
+ <-release
+ return eb.rootfsStartable(ctx, rt)
+ }
+ res := make(map[string]chan error)
+ for _, rt := range []registry.Route{nested, foreign} {
+ ch := make(chan error, 1)
+ res[rt.Socket] = ch
+ go func(rt registry.Route) { ch <- a.rootfsStartRefusal(context.Background(), rt) }(rt)
+ }
+ waitFor(t, "both rows to be asked", func() bool { mu.Lock(); defer mu.Unlock(); return asked == 2 })
+ close(release)
+ if err := <-res[nested.Socket]; err != nil {
+ t.Fatalf("the nested row: %v", err)
+ }
+ if err := <-res[foreign.Socket]; err == nil || !strings.Contains(err.Error(), "nested isolation") {
+ t.Fatalf("the foreign row took the nested row's answer: %v", err)
+ }
+}
+
+// [Astra Task 7 R1 3, Astra R3 P1 1] Through Service.Start: the sweeper's
+// own due wake of a rootfs row blocks in the backend's gate, yet the
+// sweeper keeps sweeping and an unrelated binary request is served; then
+// Shutdown, with a short deadline, ends the blocked gate (the service's
+// lifetime) and the wake behind it, and nothing took the rootfs row's
+// lease.
+func TestRootfsGateNeverHoldsTheSweeper(t *testing.T) {
+ a, b, clk, reg := testActivator(t, 400*time.Millisecond)
+ a.gens = true
+ armRootfsForTest(a)
+ rt := rootfsGateRoute("a.example.com")
+ if err := reg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.SetNextDue(rt.Host, clk.now().Add(-time.Minute).Unix()); err != nil {
+ t.Fatal(err)
+ }
+ entered := make(chan struct{})
+ ended := make(chan error, 1)
+ var once sync.Once
+ b.startable = func(ctx context.Context, got registry.Route) error {
+ once.Do(func() { close(entered) })
+ <-ctx.Done()
+ ended <- ctx.Err()
+ return ctx.Err()
+ }
+ a.Start()
+ select {
+ case <-entered:
+ case <-time.After(5 * time.Second):
+ t.Fatal("the sweeper never due-woke the rootfs row")
+ }
+ for i := 0; i < 3; i++ {
+ last := a.LastSweep()
+ waitFor(t, "the sweeper to sweep past the blocked gate", func() bool { return a.LastSweep().After(last) })
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ done, err := a.beginRequest(ctx, testRoute("blog.example.com"))
+ cancel()
+ if err != nil {
+ t.Fatalf("an unrelated request while the gate is blocked: %v", err)
+ }
+ done()
+ sctx, scancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer scancel()
+ began := time.Now()
+ a.Shutdown(sctx)
+ if took := time.Since(began); took > time.Second {
+ t.Fatalf("Shutdown took %s behind a blocked rootfs gate", took)
+ }
+ select {
+ case err := <-ended:
+ if err == nil {
+ t.Fatal("the gate ended without its context ending")
+ }
+ case <-time.After(2 * time.Second):
+ t.Fatal("Shutdown did not end the blocked gate")
+ }
+ waitFor(t, "the rootfs wake to end", func() bool {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ return a.entries[rt.Host] == nil
+ })
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ if len(b.startedRoutes) != 1 || b.startedRoutes[0].Host != "blog.example.com" || b.acquires != 1 {
+ t.Fatalf("acquires %d, starts %v: only the binary request may have taken a lease", b.acquires, b.startedRoutes)
+ }
+}
+
+// seqTable answers each Get with the next row of its list (the last one
+// from then on): a registry row that changes between two reads of one
+// wake.
+type seqTable struct {
+ mu sync.Mutex
+ rows []registry.Route
+}
+
+func (s *seqTable) Get(string) (*registry.Route, error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ rt := s.rows[0]
+ if len(s.rows) > 1 {
+ s.rows = s.rows[1:]
+ }
+ return &rt, nil
+}
+
+// The same through the held wake's second read (step 0.65): the row is
+// nested at step 0's refresh, so the cheap re-check passes, and foreign at
+// the re-read after the wait for root. Step 0.9 admits the row the lease
+// acts on, in full, before Acquire.
+func TestAdmissionFollowsTheHeldWakesReread(t *testing.T) {
+ eb, _ := newRootfsBackend(t)
+ nested := rootfsRoute(eb)
+ foreign := nested
+ foreign.Socket = filepath.Join(eb.RunDirOverride, "elsewhere.sock")
+ a, b, _, _ := gateService(t)
+ b.mu.Lock()
+ b.ident.live = "v1"
+ b.mu.Unlock()
+ a.HeldTarget = func(string) HeldWake { return HeldWake{Held: true, Target: "v1"} }
+ b.startable = func(ctx context.Context, rt registry.Route) error { return eb.rootfsStartable(ctx, rt) }
+ a.Routes = &seqTable{rows: []registry.Route{nested, foreign}}
+ _, err := a.beginRequest(context.Background(), nested)
+ if err == nil || !errors.Is(err, errRootfsNotAdmitted) || !strings.Contains(err.Error(), "nested isolation") {
+ t.Fatalf("wake = %v, want the re-read (foreign) row refused before the lease", err)
+ }
+ if got := b.gateCounts(); got.acquires != 0 || got.restores != 0 {
+ t.Fatalf("the re-read row reached the lease: %+v", got)
+ }
+}
diff --git a/internal/activator/rootfs_service.go b/internal/activator/rootfs_service.go
index a3cc36d0f..b975a44d0 100644
--- a/internal/activator/rootfs_service.go
+++ b/internal/activator/rootfs_service.go
@@ -166,19 +166,33 @@ func (a *Service) rootfsStartRefusal(ctx context.Context, rt registry.Route) err
return nil
}
+// rootfsGateKey is the identity of the row an admission judged: every
+// field the early gates read (the runtime; the layout, from the host and
+// both paths; the backing and tunnel; the account, slug and app the
+// placement reads). An admission, and a shared ask of the backend's gates,
+// holds only for a row with the same key: a cached row and the registry's
+// current one that differ in any of them are judged apart [Astra Task 7
+// R1 1, INV-3: admission authorizes the row whose storage is touched].
+func rootfsGateKey(rt registry.Route) string {
+ return fmt.Sprintf("%q", []string{rt.Host, rt.Kind, rt.Runtime, rt.Account, rt.Slug, rt.App,
+ rt.DBPath, rt.Socket, rt.Backing, rt.Tunnel, fmt.Sprint(rt.Hibernate)})
+}
+
// rootfsGateRun is one ask of the backend's gates for a host: its answer
// is published by closing done. waiters counts the callers that took it
// (the logs' and the tests' view of the coalescing), under rootfsGateMu.
type rootfsGateRun struct {
+ host string
done chan struct{}
err error
waiters int
}
// rootfsBackendGates asks the backend's rootfsStartable for rt, one ask per
-// host at a time: a caller that arrives while an ask is in flight waits
-// for it and takes its answer, so a request storm on a cold host asks the
-// gates once. The ask runs on its own goroutine under the service's
+// row (rootfsGateKey) at a time: a caller that arrives while an ask of the
+// same row is in flight waits for it and takes its answer, so a request
+// storm on a cold host asks the gates once; a different row of the host is
+// asked on its own. The ask runs on its own goroutine under the service's
// lifetime and rootfsGateBound, never under one caller's ctx (a caller
// that hangs up must not fail the others), and Shutdown's cancellation of
// the lifetime ends it. A caller stops waiting when its own ctx ends.
@@ -187,14 +201,15 @@ func (a *Service) rootfsBackendGates(ctx context.Context, rt registry.Route) err
if !ok {
return refuseRootfs(fmt.Errorf("%s: %w", rt.Host, errNoRootfsControl))
}
+ key := rootfsGateKey(rt)
a.rootfsGateMu.Lock()
- run := a.rootfsGateRuns[rt.Host]
+ run := a.rootfsGateRuns[key]
if run == nil {
- run = &rootfsGateRun{done: make(chan struct{})}
+ run = &rootfsGateRun{host: rt.Host, done: make(chan struct{})}
if a.rootfsGateRuns == nil {
a.rootfsGateRuns = map[string]*rootfsGateRun{}
}
- a.rootfsGateRuns[rt.Host] = run
+ a.rootfsGateRuns[key] = run
parent := a.lifeCtx
if parent == nil {
parent = context.Background()
@@ -204,7 +219,7 @@ func (a *Service) rootfsBackendGates(ctx context.Context, rt registry.Route) err
defer cancel()
err := s.rootfsStartable(gctx, rt)
a.rootfsGateMu.Lock()
- delete(a.rootfsGateRuns, rt.Host)
+ delete(a.rootfsGateRuns, key)
run.err = err
a.rootfsGateMu.Unlock()
close(run.done)
@@ -260,11 +275,12 @@ func rootfsUnarmedEvent() string {
// rootfsWakeRefusal is doWake's ask of the admission, off a.mu, before the
// lease: the whole of it, unless begin asked the backend's gates for this
-// very entry an instant before (e.rootfsAdmitted), when only the locked
-// half is asked again (it is cheap, and a ruling that changed since is
-// seen).
+// very row an instant before (e.rootfsAdmittedKey is rt's key), when only
+// the locked half is asked again (it is cheap, and a ruling that changed
+// since is seen). A row the refresh changed in any gate field is asked in
+// full.
func (a *Service) rootfsWakeRefusal(ctx context.Context, e *actEntry, rt registry.Route) error {
- if e.rootfsAdmitted {
+ if e.rootfsAdmittedKey != "" && e.rootfsAdmittedKey == rootfsGateKey(rt) {
a.mu.Lock()
err := a.rootfsRefusalLocked(rt)
a.mu.Unlock()