activator: one place installs a process on an entry and puts a resident under its lease watch; the guard refuses every other install; ownership refusals reclassified for runtime rows only (Task 8 round 3)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
13 files changed,
+512
−61
internal/activator/activator.go+59 −17internal/activator/recoveryprobe.go+4 −4internal/activator/rootfs_install_guard_test.go+197 −0internal/activator/rootfs_resident_lease_test.go+115 −4internal/activator/rootfs_service.go+70 −22internal/activator/swap.go+3 −14internal/activator/testdata/rootfs_verb_guard/install_addr.go+7 −0internal/activator/testdata/rootfs_verb_guard/install_clean.go+14 −0internal/activator/testdata/rootfs_verb_guard/install_direct.go+6 −0internal/activator/testdata/rootfs_verb_guard/install_genfield.go+7 −0internal/activator/testdata/rootfs_verb_guard/install_literal.go+6 −0internal/activator/testdata/rootfs_verb_guard/install_tier.go+18 −0internal/activator/testdata/rootfs_verb_guard/install_tuple.go+6 −0
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index e699e2623..c07310720 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -484,6 +484,9 @@ type actEntry struct { |
| // Astra Task 8 R1 1): made at the awake transition, closed by |
| // finishStop as the entry is forgotten. Under a.mu. |
| ownerDone chan struct{} |
| + // ownerKilled: the tick's second-line lease check (rootfsTick) has |
| + // already killed this unwatched resident's generations. Under a.mu. |
| + ownerKilled bool |
| // 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. |
| @@ -1988,7 +1991,6 @@ func (a *Service) wake(e *actEntry) { |
| delete(a.entries, e.host) // next request retries from scratch |
| } else { |
| e.state = actAwake |
| - e.rep, e.proc = w.rep, w.proc |
| e.sidecar, e.sidecarWanted = w.sidecar, w.sidecarWanted |
| e.sidecarUncertain = w.sidecarUncertain |
| e.sidecarGen = w.sidecarGen |
| @@ -2020,25 +2022,30 @@ func (a *Service) wake(e *actEntry) { |
| // StartPolicy.Gen it carries the handle the provider bound (Task |
| // 10); otherwise it gets the canonical handle and a number of its |
| // own. |
| - e.gen = w.gen |
| + g := w.gen |
| e.launchLayout = w.launchLayout |
| - if e.gen.N == 0 && !e.worker { |
| - ran := e.gen |
| - e.gen = canonicalGen(e.route, nextGen(), w.spawnVersion) |
| + if g.N == 0 && !e.worker { |
| + ran := g |
| + g = canonicalGen(e.route, nextGen(), w.spawnVersion) |
| if ran.StagedPath != "" && ran.Build == w.spawnVersion { |
| // A claimed start with no number names the file it ran |
| // (startAdmitted): the handle keeps it, so a legacy |
| // sidecar restart runs that file too (Task 5d). |
| - e.gen.StagedPath, e.gen.Digest, e.gen.Demand = ran.StagedPath, ran.Digest, ran.Demand |
| + g.StagedPath, g.Digest, g.Demand = ran.StagedPath, ran.Digest, ran.Demand |
| } |
| } |
| - if a.gens && e.worker && e.gen.N == 0 { |
| + if a.gens && e.worker && g.N == 0 { |
| // A worker woken some way that carried no number (doWake gives |
| // every worker spawn its own): numbered here, with the build it |
| // runs, so the barrier and the generations file see it. Worker: |
| // its termination is proven on its host cgroup (Task 11). |
| - e.gen = Gen{N: nextGen(), Build: w.spawnVersion, Worker: true} |
| + g = Gen{N: nextGen(), Build: w.spawnVersion, Worker: true} |
| } |
| + // The process, its generation and the lease's replica are installed |
| + // in one place (installProcLocked), which also puts a resident under |
| + // its lease watch before the fence, shutdown and queued-restart |
| + // transitions below can stop it (Plan 3 Task 8, C7). |
| + a.installProcLocked(e, w.rep, w.proc, g) |
| if e.gen.N != 0 { |
| a.noteCurrent(e.host, e.gen.N) |
| } |
| @@ -2079,11 +2086,6 @@ func (a *Service) wake(e *actEntry) { |
| e.bounceAfterWake = false |
| a.bounceLocked(e) |
| } |
| - if e.state == actAwake && e.route.Resident() { |
| - // A resident's lease is watched on its own signal from here, |
| - // not only by the idle sweep (Astra Task 8 R1 1). |
| - a.watchResidentLeaseLocked(e) |
| - } |
| if a.OnWake != nil && e.state == actAwake { |
| // Fire-and-forget, off this goroutine: e.route is read once here, |
| // before this goroutine can be reused for anything else — the |
| @@ -2450,6 +2452,10 @@ func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, erro |
| w.steps.mark(a.now(), "checks") |
| *w.stopOutcome = stopObservation{} |
| lease, err := a.backend.Acquire(ctx, rt) |
| + // A row with a runtime that meets a held lease or refused config is |
| + // refused, never failed (markOwnershipRefusal); a binary row's answer |
| + // is main's. |
| + err = markOwnershipRefusal(rt, err) |
| if err != nil { |
| if lease != nil { |
| w.stopOutcome.uncertain = true |
| @@ -2923,6 +2929,41 @@ func (a *Service) beginStopLocked(e *actEntry, why string) { |
| go a.stop(e) |
| } |
| +// installProcLocked is the one place a process is installed on an entry: |
| +// its process, its generation and, when rep is non-nil, the lease's |
| +// replica (nil keeps the entry's own: a swap's successor and a worker's |
| +// respawn run under the lease the wake took). Every install path comes |
| +// through here: a wake (wake), a hold's pinned wake and a resident's |
| +// restart (both wakes), a swap's successor (swapOverlap) and a worker's |
| +// respawn and a recovery probe's install. For a resident row the lease |
| +// watch is installed here too, before the function returns, so no fence, |
| +// shutdown or queued-restart transition the caller runs after it can leave |
| +// a resident process unwatched (Plan 3 Task 8, C7: one invariant in one |
| +// place). The watch is held until finishStop, after the stop's termination |
| +// proof, closes it. TestResidentProcessInstallsGoThroughOnePlace refuses |
| +// any other write of these fields. The caller holds a.mu. |
| +func (a *Service) installProcLocked(e *actEntry, rep Rep, proc Proc, g Gen) { |
| + if rep != nil { |
| + e.rep = rep |
| + } |
| + e.proc = proc |
| + e.gen = g |
| + if e.route.Resident() { |
| + a.watchResidentLeaseLocked(e) |
| + } |
| +} |
| + |
| +// recordCurrentTierLocked records the tier the entry's current generation |
| +// runs at, when it is still generation n (a swap's captured tier). It is |
| +// the one write of an entry's generation besides installProcLocked, and |
| +// it installs nothing: it changes only Gen.Tier (the install guard holds it |
| +// to that). The caller holds a.mu. |
| +func recordCurrentTierLocked(e *actEntry, n uint64, tier cgroup.Limit) { |
| + if e.gen.N == n { |
| + e.gen.Tier = tier |
| + } |
| +} |
| + |
| // Owns reports whether host has a live entry in ANY state — waking, |
| // awake, or stopping. This is the external-socket advisory's predicate |
| // (issue #17): while an entry exists, whatever is serving the host's |
| @@ -3982,7 +4023,11 @@ func (a *Service) respawnWorkerLocked(e *actEntry, stopOld Proc, why string, ont |
| log.Printf("activator: WORKER %s failed to respawn (%s): %v — the sweep's crash restart takes it from here", host, why, err) |
| emitEvent(a.Logs, rt, "error", "sidecar could not be restarted. retrying") |
| default: |
| - e.proc = proc |
| + g := e.gen |
| + if m != nil { |
| + g = Gen{N: m.gen.N, Build: ident.Version, Worker: true} |
| + } |
| + a.installProcLocked(e, nil, proc, g) |
| e.workerToken = tok |
| e.spawnVersion = ident.Version |
| e.workerServing = ident |
| @@ -3997,9 +4042,6 @@ func (a *Service) respawnWorkerLocked(e *actEntry, stopOld Proc, why string, ont |
| if hold { |
| e.heldServing = &heldServing{id: ident, inc: inc} |
| } |
| - if m != nil { |
| - e.gen = Gen{N: m.gen.N, Build: ident.Version, Worker: true} |
| - } |
| // The captured build replaces the one published before the exec. |
| a.dropPendingGenerationLocked(pend) |
| // Only an exec from the live link starts a fresh drift clock. A |
diff --git a/internal/activator/recoveryprobe.go b/internal/activator/recoveryprobe.go| index 01ee80330..8594846d6 100644 |
| --- a/internal/activator/recoveryprobe.go |
| +++ b/internal/activator/recoveryprobe.go |
| @@ -227,14 +227,14 @@ func (a *Service) finishRecoveryProbe(e *actEntry, fence uint64, w *wokenInstanc |
| now := a.now() |
| e.state = actAwake |
| e.probeFence = fence |
| - e.rep, e.proc = w.rep, w.proc |
| e.spawnVersion = w.spawnVersion |
| // Retain the launcher's socket; legacy claim launchers use the same |
| // canonical fallback as an ordinary wake. Publish only when opened. |
| - e.gen = w.gen |
| - if e.gen.N == 0 && !e.worker { |
| - e.gen = canonicalGen(e.route, nextGen(), w.spawnVersion) |
| + g := w.gen |
| + if g.N == 0 && !e.worker { |
| + g = canonicalGen(e.route, nextGen(), w.spawnVersion) |
| } |
| + a.installProcLocked(e, w.rep, w.proc, g) |
| e.lastActive, e.spawnedAt = now, now |
| e.bounceAfterWake = false |
| if e.probeObsolete { |
diff --git a/internal/activator/rootfs_install_guard_test.go b/internal/activator/rootfs_install_guard_test.go| new file mode 100644 |
| index 000000000..72e8427fb |
| --- /dev/null |
| +++ b/internal/activator/rootfs_install_guard_test.go |
| @@ -0,0 +1,197 @@ |
| +package activator |
| + |
| +// Rootfs apps Plan 3 Task 8, round 3 (stopping rule 2, Paul's ruling): one |
| +// invariant in one place. A process is installed on an entry only through |
| +// installProcLocked, which puts a resident under its lease watch before any |
| +// later transition (a fence, a shutdown, a queued restart) can stop it. The |
| +// guard refuses every other write of an entry's process, generation or |
| +// lease replica: an assignment to actEntry.proc, .gen or .rep (or into any |
| +// of their fields), their address taken, or an actEntry literal that sets |
| +// one. Resolution is by type (go/types), so a member's or a wake's fields |
| +// of the same names are not an entry's. |
| + |
| +import ( |
| + "go/ast" |
| + "go/token" |
| + "go/types" |
| + "slices" |
| + "strings" |
| + "testing" |
| + |
| + "golang.org/x/tools/go/packages" |
| +) |
| + |
| +// installGuardFields are the entry fields only installProcLocked writes. |
| +var installGuardFields = []string{"proc", "gen", "rep"} |
| + |
| +// installGuardOwner is the one function that may write them. |
| +const installGuardOwner = "installProcLocked" |
| + |
| +// installGuardTierOwner may write exactly X.gen.Tier and nothing else of |
| +// the three: a swap's captured tier for the current generation, which |
| +// installs nothing (recordCurrentTierLocked). |
| +const installGuardTierOwner = "recordCurrentTierLocked" |
| + |
| +// isGenTier reports whether x is exactly <entry>.gen.Tier. |
| +func isGenTier(info *types.Info, x ast.Expr) bool { |
| + se, ok := x.(*ast.SelectorExpr) |
| + if !ok || se.Sel.Name != "Tier" { |
| + return false |
| + } |
| + inner, ok := se.X.(*ast.SelectorExpr) |
| + return ok && entryField(info, inner) == "gen" |
| +} |
| + |
| +// entryField reports the guarded actEntry field a selector names, or "". |
| +func entryField(info *types.Info, se *ast.SelectorExpr) string { |
| + sel := info.Selections[se] |
| + if sel == nil || sel.Kind() != types.FieldVal || !slices.Contains(installGuardFields, se.Sel.Name) { |
| + return "" |
| + } |
| + t := sel.Recv() |
| + if p, ok := t.(*types.Pointer); ok { |
| + t = p.Elem() |
| + } |
| + if n, ok := t.(*types.Named); ok && n.Obj().Name() == "actEntry" { |
| + return se.Sel.Name |
| + } |
| + return "" |
| +} |
| + |
| +// guardedField is the guarded entry field x writes through: x itself, or |
| +// any selector, index or star on the way down to it (e.gen.StagedPath). |
| +func guardedField(info *types.Info, x ast.Expr) string { |
| + for { |
| + switch v := x.(type) { |
| + case *ast.SelectorExpr: |
| + if f := entryField(info, v); f != "" { |
| + return f |
| + } |
| + x = v.X |
| + case *ast.IndexExpr: |
| + x = v.X |
| + case *ast.StarExpr: |
| + x = v.X |
| + case *ast.ParenExpr: |
| + x = v.X |
| + default: |
| + return "" |
| + } |
| + } |
| +} |
| + |
| +// installGuardViolations is every guarded write in f outside |
| +// installGuardOwner, as "<func>: <what>"; owned counts the writes inside it. |
| +func installGuardViolations(pkg *packages.Package, f *ast.File) (violations []string, owned int) { |
| + info := pkg.TypesInfo |
| + for _, decl := range f.Decls { |
| + fd, ok := decl.(*ast.FuncDecl) |
| + if !ok || fd.Body == nil { |
| + continue |
| + } |
| + name := fd.Name.Name |
| + if fd.Recv != nil && len(fd.Recv.List) == 1 { |
| + name = receiverTypeName(fd.Recv.List[0].Type) + "." + name |
| + } |
| + report := func(what string, x ast.Expr) { |
| + if name == "Service."+installGuardOwner { |
| + owned++ |
| + return |
| + } |
| + if name == installGuardTierOwner && x != nil && isGenTier(info, x) { |
| + return |
| + } |
| + violations = append(violations, name+": "+what+" outside "+installGuardOwner) |
| + } |
| + ast.Inspect(fd.Body, func(n ast.Node) bool { |
| + switch v := n.(type) { |
| + case *ast.AssignStmt: |
| + if v.Tok == token.DEFINE { |
| + return true |
| + } |
| + for _, lhs := range v.Lhs { |
| + if field := guardedField(info, lhs); field != "" { |
| + report("sets actEntry."+field, lhs) |
| + } |
| + } |
| + case *ast.IncDecStmt: |
| + if field := guardedField(info, v.X); field != "" { |
| + report("sets actEntry."+field, nil) |
| + } |
| + case *ast.UnaryExpr: |
| + if v.Op == token.AND { |
| + if field := guardedField(info, v.X); field != "" { |
| + report("takes the address of actEntry."+field, nil) |
| + } |
| + } |
| + case *ast.CompositeLit: |
| + if n, ok := info.TypeOf(v).(*types.Named); ok && n.Obj().Name() == "actEntry" { |
| + for _, elt := range v.Elts { |
| + if kv, ok := elt.(*ast.KeyValueExpr); ok { |
| + if id, ok := kv.Key.(*ast.Ident); ok && slices.Contains(installGuardFields, id.Name) { |
| + report("builds an actEntry with "+id.Name, nil) |
| + } |
| + } |
| + } |
| + } |
| + } |
| + return true |
| + }) |
| + } |
| + return violations, owned |
| +} |
| + |
| +// installGuardFixtures: fixture -> the violations it must report (none for |
| +// the clean one). Rows are only added. |
| +var installGuardFixtures = map[string][]string{ |
| + "install_clean.go": nil, |
| + "install_direct.go": {"Service.installGuardDirect: sets actEntry.proc outside installProcLocked"}, |
| + "install_tuple.go": {"Service.installGuardTuple: sets actEntry.rep outside", "Service.installGuardTuple: sets actEntry.proc outside"}, |
| + "install_genfield.go": {"Service.installGuardGenField: sets actEntry.gen outside"}, |
| + "install_literal.go": {"Service.installGuardLiteral: builds an actEntry with proc", "Service.installGuardLiteral: builds an actEntry with gen"}, |
| + "install_addr.go": {"Service.installGuardAddr: takes the address of actEntry.proc"}, |
| + "install_tier.go": {"Service.installGuardTier: sets actEntry.gen outside", |
| + "genMember.recordCurrentTierLocked: sets actEntry.gen outside", "genMember.recordCurrentTierLocked: sets actEntry.proc outside", |
| + "shedChild.installProcLocked: sets actEntry.proc outside"}, |
| +} |
| + |
| +func TestResidentProcessInstallsGoThroughOnePlace(t *testing.T) { |
| + pkg, err := loadVerbGuardPackage() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + files := packageFiles(pkg) |
| + owned := 0 |
| + for base, f := range files { |
| + if strings.HasPrefix(base, verbGuardFixturePrefix) || strings.HasSuffix(base, "_test.go") { |
| + continue |
| + } |
| + v, n := installGuardViolations(pkg, f) |
| + owned += n |
| + for _, p := range v { |
| + t.Errorf("%s: %s", base, p) |
| + } |
| + } |
| + // A guard that resolved nothing proves nothing: installProcLocked's own |
| + // three writes (rep, proc, gen) must be seen. |
| + if owned < 3 { |
| + t.Fatalf("the guard saw %d writes inside %s, want its 3: it resolves nothing", owned, installGuardOwner) |
| + } |
| + for fixture, want := range installGuardFixtures { |
| + t.Run(fixture, func(t *testing.T) { |
| + f := files[verbGuardFixturePrefix+fixture] |
| + if f == nil { |
| + t.Fatalf("fixture %s was not loaded", fixture) |
| + } |
| + got, _ := installGuardViolations(pkg, f) |
| + for _, w := range want { |
| + if !slices.ContainsFunc(got, func(p string) bool { return strings.Contains(p, w) }) { |
| + t.Errorf("the guard did not report %q; it reported:\n%s", w, strings.Join(got, "\n")) |
| + } |
| + } |
| + if len(want) == 0 && len(got) > 0 { |
| + t.Errorf("the guard refused a clean source:\n%s", strings.Join(got, "\n")) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/activator/rootfs_resident_lease_test.go b/internal/activator/rootfs_resident_lease_test.go| index ecc25fd49..239cabfd7 100644 |
| --- a/internal/activator/rootfs_resident_lease_test.go |
| +++ b/internal/activator/rootfs_resident_lease_test.go |
| @@ -361,11 +361,12 @@ func TestResidentJobHonoursABackoffSetMeanwhile(t *testing.T) { |
| // counts nothing, and a request is not held back by that pacing. |
| func TestResidentOwnershipRefusalsArePacedNotCounted(t *testing.T) { |
| for _, err := range []error{&errLeaseHeld{holder: "box-b", retryAfter: 10 * time.Second}, &errConfigRefused{err: errors.New("no key")}} { |
| - if v := verdictOf(fmt.Errorf("x: %w", err)); v != gateNoOwnership { |
| - t.Errorf("%T: verdictOf = %d, want gateNoOwnership", err, v) |
| + marked := markOwnershipRefusal(residentRoute("shop.example.com"), err) |
| + if v := verdictOf(fmt.Errorf("x: %w", marked)); v != gateNoOwnership { |
| + t.Errorf("%T on a rootfs row: verdictOf = %d, want gateNoOwnership", err, v) |
| } |
| - if residentFailure(fmt.Errorf("x: %w", err)) { |
| - t.Errorf("%T: residentFailure = true", err) |
| + if residentFailure(fmt.Errorf("x: %w", marked)) { |
| + t.Errorf("%T on a rootfs row: residentFailure = true", err) |
| } |
| } |
| host := "shop.example.com" |
| @@ -387,3 +388,113 @@ func TestResidentOwnershipRefusalsArePacedNotCounted(t *testing.T) { |
| b.mu.Unlock() |
| startResident(t, a, host) |
| } |
| + |
| +// [Astra R2 2, INV-6] The ownership reclassification is a runtime row's |
| +// only: on a binary row a held lease and refused config keep main's |
| +// classification (verdictOf gateProceed, classifyStep stepFailed), through |
| +// doWake's own Acquire and directly; on a rootfs row they are refusals. |
| +func TestOwnershipRefusalIsARuntimeRowsOnly(t *testing.T) { |
| + for _, cause := range []error{&errLeaseHeld{holder: "box-b", retryAfter: time.Minute}, &errConfigRefused{err: errors.New("no key")}} { |
| + binary := testRoute("blog.example.com") |
| + if got := markOwnershipRefusal(binary, cause); got != cause { |
| + t.Errorf("%T: a binary row's error was marked: %v", cause, got) |
| + } |
| + if v, s := verdictOf(cause), classifyStep(nil, cause); v != gateProceed || s != stepFailed { |
| + t.Errorf("%T unmarked: verdictOf %d, classifyStep %d; want main's gateProceed and stepFailed", cause, v, s) |
| + } |
| + for _, row := range []struct { |
| + name string |
| + rt registry.Route |
| + want gateVerdict |
| + }{{"binary", binary, gateProceed}, {"rootfs", rootfsGateRoute("shop.example.com"), gateNoOwnership}} { |
| + t.Run(fmt.Sprintf("%T/%s", cause, row.name), func(t *testing.T) { |
| + a, b, _, _ := gateService(t) |
| + b.mu.Lock() |
| + b.acquireErr = cause |
| + b.mu.Unlock() |
| + err := dueWake(a, row.rt) |
| + if !errors.As(err, new(*errLeaseHeld)) && !errors.As(err, new(*errConfigRefused)) { |
| + t.Fatalf("the wake's error %v lost its typed cause", err) |
| + } |
| + if v := verdictOf(err); v != row.want { |
| + t.Fatalf("verdictOf = %d, want %d", v, row.want) |
| + } |
| + }) |
| + } |
| + } |
| +} |
| + |
| +// [Astra R2 1, round 3's ruling] A `carlos restart` queued while the wake |
| +// was under way stops the resident the instant its wake installs it. The |
| +// lease watch is installed by the install itself (installProcLocked), so |
| +// the stopping entry is watched: its drain hangs, the lease is lost, the |
| +// generation is killed at once, and the entry and its lease are held until |
| +// that kill's proof has returned. |
| +func TestResidentQueuedRestartDuringWakeIsWatched(t *testing.T) { |
| + host := "shop.example.com" |
| + a, kb, _, _ := residentKillService(t, residentRoute(host)) |
| + var rep *ownedRep |
| + kb.mu.Lock() |
| + kb.repWrap = func(r *fakeRep) Rep { |
| + r.setOnClose(func() { kb.record("close") }) |
| + rep = newOwnedRep(r, time.Now().Add(time.Hour)) |
| + return rep |
| + } |
| + gate := make(chan struct{}) |
| + kb.startGate = gate |
| + kb.mu.Unlock() |
| + a.rootfsTick() |
| + waitFor(t, "the wake's start", func() bool { return kb.startCount() == 1 }) |
| + if a.Bounce(host) { |
| + t.Fatal("Bounce stopped a waking entry; it must queue") |
| + } |
| + proc := kb.lastProc() |
| + drain := make(chan struct{}) |
| + proc.setStopGate(drain) // the bounce's SIGTERM drain hangs |
| + proved := make(chan struct{}) |
| + var killed sync.Once |
| + endAll := func() { killed.Do(func() { proc.exit(); close(drain) }) } |
| + t.Cleanup(func() { |
| + endAll() |
| + select { |
| + case <-proved: |
| + default: |
| + close(proved) |
| + } |
| + }) |
| + var heldAtKill, closedAtKill bool |
| + kb.kmu.Lock() |
| + kb.onKill = func(uint64) { |
| + killed.Do(func() { |
| + heldAtKill, closedAtKill = a.hasEntry(host), rep.isClosed() |
| + <-proved // the kill's proof is still outstanding |
| + proc.exit() |
| + close(drain) |
| + }) |
| + } |
| + kb.kmu.Unlock() |
| + close(gate) |
| + waitFor(t, "the queued restart's stop to begin", func() bool { return proc.stopEntered() == 1 }) |
| + a.mu.Lock() |
| + e := a.entries[host] |
| + watched := e != nil && e.ownerDone != nil && e.state == actStopping |
| + a.mu.Unlock() |
| + if !watched { |
| + t.Fatal("the resident went into stopping without its lease watch") |
| + } |
| + rep.lose(errors.New("renew: the lock was taken")) |
| + waitFor(t, "the kill at loss", func() bool { return len(kb.killTimes()) > 0 }) |
| + time.Sleep(50 * time.Millisecond) |
| + if !a.hasEntry(host) || rep.isClosed() { |
| + t.Fatal("the entry or the lease was released while the kill's proof was outstanding") |
| + } |
| + close(proved) |
| + waitFor(t, "the stop to finish", func() bool { return !a.hasEntry(host) }) |
| + if !heldAtKill || closedAtKill { |
| + t.Fatalf("at the kill: entry held %v, lease closed %v; want held and not closed", heldAtKill, closedAtKill) |
| + } |
| + ev := kb.events() |
| + if k, c := indexWithPrefix(ev, "kill:"), indexWithPrefix(ev, "close"); k < 0 || c < k { |
| + t.Fatalf("order %v: the lease went before the kill", ev) |
| + } |
| +} |
diff --git a/internal/activator/rootfs_service.go b/internal/activator/rootfs_service.go| index 409e6c7fd..e14b2932b 100644 |
| --- a/internal/activator/rootfs_service.go |
| +++ b/internal/activator/rootfs_service.go |
| @@ -437,8 +437,9 @@ func retryAfterSeconds(d time.Duration) int { |
| // unclassified (an Acquire failing on a store outage) count, so a store |
| // outage backs off instead of retrying every tick. A lease another holder |
| // has (errLeaseHeld) and refused config (errConfigRefused) are refusals |
| -// through verdictOf; their retry pacing is residentOwnerWait's, never a |
| -// failure. Group R extends it through classifyRelease. |
| +// through verdictOf once doWake has marked them for the row's runtime |
| +// (markOwnershipRefusal); their retry pacing is residentOwnerWait's, never |
| +// a failure. Group R extends it through classifyRelease. |
| func residentFailure(err error) bool { |
| switch { |
| case err == nil: |
| @@ -454,6 +455,38 @@ func residentFailure(err error) bool { |
| return true |
| } |
| +// errOwnershipRefused marks a lease another holder still has |
| +// (*errLeaseHeld) or config this box cannot provide yet (*errConfigRefused) |
| +// met by a row with a runtime (markOwnershipRefusal): a refusal before |
| +// anything of the build ran (INV-4; Astra Task 8 R1 5), which verdictOf |
| +// reads as gateNoOwnership. A binary row's same errors stay unmarked and |
| +// keep main's classification (INV-6; Astra Task 8 R2 2). |
| +var errOwnershipRefused = errors.New("activator: ownership refused before the start") |
| + |
| +// ownershipRefusal wraps one: errors.Is matches errOwnershipRefused, and |
| +// errors.As still reaches the typed cause (residentOwnerWait reads its |
| +// retry-after). The text is the cause's. |
| +type ownershipRefusal struct{ err error } |
| + |
| +func (e ownershipRefusal) Error() string { return e.err.Error() } |
| +func (e ownershipRefusal) Unwrap() error { return e.err } |
| +func (e ownershipRefusal) Is(target error) bool { return target == errOwnershipRefused } |
| + |
| +// markOwnershipRefusal marks err for rt when rt has a runtime and err is a |
| +// held lease or refused config; anything else, and any binary row's error, |
| +// is returned as it is. |
| +func markOwnershipRefusal(rt registry.Route, err error) error { |
| + if err == nil || rt.Runtime == "" { |
| + return err |
| + } |
| + var held *errLeaseHeld |
| + var refused *errConfigRefused |
| + if errors.As(err, &held) || errors.As(err, &refused) { |
| + return ownershipRefusal{err: err} |
| + } |
| + return err |
| +} |
| + |
| // residentOwnerWait is how long the tick waits before it asks again after |
| // an ownership refusal: a held lease's own retry-after, or the config |
| // guard's; 0 for anything else. |
| @@ -596,11 +629,12 @@ func (a *Service) residentOrderLocked(routes []registry.Route) []registry.Route |
| // residents", so nothing is stopped and nothing forgotten. |
| // 2. Every awake rootfs entry whose process exited is stopped as the |
| // sweep's crash arm stops one (StopExited), so a crash is seen within |
| -// residentPoll rather than the sweep's 30 s. An awake resident whose |
| -// replica reports its lease lost is stopped and its generations killed |
| -// (residentLeaseLostLocked), the second line under the lease watch for |
| -// a replica without the lease's own signal. Then superviseRootfsLocked |
| -// for every other awake rootfs entry. |
| +// residentPoll rather than the sweep's 30 s. A resident with no lease |
| +// watch (its replica has no lease signal), awake or stopping, whose |
| +// replica reports its lease lost has its generations killed once and |
| +// its stop begun (residentLeaseLostLocked): the second line under the |
| +// watch installProcLocked installs. Then superviseRootfsLocked for |
| +// every other awake rootfs entry. |
| // 3. A host no longer listed is forgotten. Each listed row, from just |
| // after the last host reserved (residentOrderLocked), not held |
| // (residentHeldLocked), not starting, not waiting (residentWaitingLocked), |
| @@ -621,7 +655,24 @@ func (a *Service) rootfsTick() { |
| return |
| } |
| for _, e := range a.entries { |
| - if !e.route.Rootfs() || e.state != actAwake || e.probeHeld || e.workerRespawning || a.fences[e.host] != 0 { |
| + if !e.route.Rootfs() { |
| + continue |
| + } |
| + // The second line under the lease watch, for a resident whose |
| + // replica has no lease signal (no watch was installed): awake or |
| + // already stopping, fenced or not, a lost lease kills its |
| + // generations once (residentLeaseLostLocked). |
| + if e.route.Resident() && e.ownerDone == nil && !e.ownerKilled && e.rep != nil && |
| + (e.state == actAwake || e.state == actStopping) { |
| + if err := e.rep.Err(); err != nil { |
| + e.ownerKilled = true |
| + if gens := a.residentLeaseLostLocked(e, err); gens != nil { |
| + go a.killResidentGens(e.route, gens) |
| + } |
| + continue |
| + } |
| + } |
| + if e.state != actAwake || e.probeHeld || e.workerRespawning || a.fences[e.host] != 0 { |
| continue |
| } |
| if e.proc != nil && e.proc.Exited() { |
| @@ -632,14 +683,6 @@ func (a *Service) rootfsTick() { |
| a.beginStopLocked(e, StopExited) |
| continue |
| } |
| - if e.route.Resident() && e.rep != nil { |
| - if err := e.rep.Err(); err != nil { |
| - if gens := a.residentLeaseLostLocked(e, err); gens != nil { |
| - go a.killResidentGens(e.route, gens) |
| - } |
| - continue |
| - } |
| - } |
| a.superviseRootfsLocked(e) |
| } |
| listed := make(map[string]bool, len(routes)) |
| @@ -764,10 +807,13 @@ var _ residentOwnership = (*lease.Replicator)(nil) |
| // once; a var so tests can shrink it. |
| var residentDrainBudget = 30 * time.Second |
| -// watchResidentLeaseLocked starts e's lease watch, at its awake transition |
| -// (wake). A replica without the lease's signal is left to the tick's |
| -// rep.Err check. The watch ends when the entry is forgotten (finishStop |
| -// closes ownerDone). The caller holds a.mu. |
| +// watchResidentLeaseLocked starts e's lease watch. Only installProcLocked |
| +// calls it, as a resident process is installed, so the watch exists before |
| +// any transition after the install can stop the entry. Idempotent: a swap's |
| +// successor keeps the entry's one watch. A replica without the lease's |
| +// signal is left to the tick's rep.Err check. The watch is held through |
| +// stopping and ends only when finishStop, after the termination proof, |
| +// closes ownerDone. The caller holds a.mu. |
| func (a *Service) watchResidentLeaseLocked(e *actEntry) { |
| sig, ok := e.rep.(residentOwnership) |
| if !ok || e.ownerDone != nil { |
| @@ -780,8 +826,10 @@ func (a *Service) watchResidentLeaseLocked(e *actEntry) { |
| // watchResidentLease is one resident's lease watch: the safe deadline is read |
| // again whenever it was due to fall inside the drain budget (a renewal may |
| -// have moved it); once it does, a graceful stop begins (StopLeaseLost); at |
| -// loss the entry's generations are killed. |
| +// have moved it); once it does, a graceful stop begins (StopLeaseLost) if |
| +// the entry is still awake; at loss, whatever the entry's state (awake, |
| +// stopping, a drain stalled), its generations are killed at once, and the |
| +// stop path's proof still decides when the lease goes. |
| func (a *Service) watchResidentLease(e *actEntry, sig residentOwnership, done <-chan struct{}) { |
| draining := false |
| for { |
diff --git a/internal/activator/swap.go b/internal/activator/swap.go| index 7b7dc17b4..a5c9a88af 100644 |
| --- a/internal/activator/swap.go |
| +++ b/internal/activator/swap.go |
| @@ -246,16 +246,7 @@ func verdictOf(err error) gateVerdict { |
| errors.Is(err, errUnitNoGens), errors.Is(err, errSwapNoGens), errors.Is(err, instancectl.ErrStagedExe), |
| errors.Is(err, instancectl.ErrStagedImage), errors.Is(err, errRootfsRefused), |
| errors.Is(err, errRootfsBinaryLaunch), errors.Is(err, errNoRootfsControl), |
| - errors.Is(err, errBuildStateUnwritten): |
| - return gateNoOwnership |
| - } |
| - // A lease another holder still has (errLeaseHeld) and config this box |
| - // cannot provide yet (errConfigRefused) are typed answers given before |
| - // anything of the build ran: refusals, never its failure (INV-4; Astra |
| - // rootfs Plan 3 Task 8 R1 5). |
| - var held *errLeaseHeld |
| - var refused *errConfigRefused |
| - if errors.As(err, &held) || errors.As(err, &refused) { |
| + errors.Is(err, errBuildStateUnwritten), errors.Is(err, errOwnershipRefused): |
| return gateNoOwnership |
| } |
| return gateProceed |
| @@ -539,9 +530,7 @@ func (a *Service) swapOverlap(ctx context.Context, gb SwapBackend, at Attempt, n |
| succTier = cgroup.MaxLimit(succTier, incumbent.Tier) |
| } |
| a.mu.Lock() |
| - if e.gen.N == incumbent.N { |
| - e.gen.Tier = incumbent.Tier |
| - } |
| + recordCurrentTierLocked(e, incumbent.N, incumbent.Tier) |
| m.gen.Tier = succTier |
| a.mu.Unlock() |
| @@ -674,7 +663,7 @@ func (a *Service) swapOverlap(ctx context.Context, gb SwapBackend, at Attempt, n |
| fence.retire(retiring.gen.N) // its schedule writes are refused from here, for good |
| delete(e.ops.members, m.gen.N) |
| a.addMemberLocked(e, retiring) // swapsim:M2 |
| - e.gen, e.proc = m.gen, m.proc |
| + a.installProcLocked(e, nil, m.proc, m.gen) |
| e.spawnVersion, e.staleSince, e.spawnedAt = m.gen.Build, time.Time{}, a.now() |
| a.noteCurrent(e.host, m.gen.N) // plan 1's cache generation (C4) |
| // Runs admitted against the retiring generation are interrupted |
diff --git a/internal/activator/testdata/rootfs_verb_guard/install_addr.go b/internal/activator/testdata/rootfs_verb_guard/install_addr.go| new file mode 100644 |
| index 000000000..63862dd38 |
| --- /dev/null |
| +++ b/internal/activator/testdata/rootfs_verb_guard/install_addr.go |
| @@ -0,0 +1,7 @@ |
| +// The address of an entry's process taken, so a write needs no selector. |
| +package activator |
| + |
| +func (a *Service) installGuardAddr(e *actEntry, p Proc) { |
| + q := &e.proc |
| + *q = p |
| +} |
diff --git a/internal/activator/testdata/rootfs_verb_guard/install_clean.go b/internal/activator/testdata/rootfs_verb_guard/install_clean.go| new file mode 100644 |
| index 000000000..ee6372b92 |
| --- /dev/null |
| +++ b/internal/activator/testdata/rootfs_verb_guard/install_clean.go |
| @@ -0,0 +1,14 @@ |
| +// The resident install guard's clean fixture (Plan 3 Task 8): an install |
| +// through installProcLocked, writes to a member's and a wake's own fields |
| +// of the same names, and reads of the entry's. |
| +package activator |
| + |
| +func (a *Service) installGuardClean(e *actEntry, m *genMember, w *wokenInstance, p Proc, g Gen) Gen { |
| + a.installProcLocked(e, nil, p, g) |
| + m.proc, m.gen = p, g |
| + w.proc, w.gen = p, g |
| + if e.proc != nil && e.rep != nil { |
| + return e.gen |
| + } |
| + return Gen{N: e.gen.N} |
| +} |
diff --git a/internal/activator/testdata/rootfs_verb_guard/install_direct.go b/internal/activator/testdata/rootfs_verb_guard/install_direct.go| new file mode 100644 |
| index 000000000..616191baf |
| --- /dev/null |
| +++ b/internal/activator/testdata/rootfs_verb_guard/install_direct.go |
| @@ -0,0 +1,6 @@ |
| +// A direct write of an entry's process, around installProcLocked. |
| +package activator |
| + |
| +func (a *Service) installGuardDirect(e *actEntry, p Proc) { |
| + e.proc = p |
| +} |
diff --git a/internal/activator/testdata/rootfs_verb_guard/install_genfield.go b/internal/activator/testdata/rootfs_verb_guard/install_genfield.go| new file mode 100644 |
| index 000000000..d8bdcb3f8 |
| --- /dev/null |
| +++ b/internal/activator/testdata/rootfs_verb_guard/install_genfield.go |
| @@ -0,0 +1,7 @@ |
| +// A write into the entry's generation through one of its fields, inside a |
| +// closure. |
| +package activator |
| + |
| +func (a *Service) installGuardGenField(e *actEntry) { |
| + func() { e.gen.StagedPath = "/opt/x" }() |
| +} |
diff --git a/internal/activator/testdata/rootfs_verb_guard/install_literal.go b/internal/activator/testdata/rootfs_verb_guard/install_literal.go| new file mode 100644 |
| index 000000000..df8d2aa88 |
| --- /dev/null |
| +++ b/internal/activator/testdata/rootfs_verb_guard/install_literal.go |
| @@ -0,0 +1,6 @@ |
| +// An entry built with a process already on it. |
| +package activator |
| + |
| +func (a *Service) installGuardLiteral(p Proc, g Gen) *actEntry { |
| + return &actEntry{host: "shop.example.com", proc: p, gen: g} |
| +} |
diff --git a/internal/activator/testdata/rootfs_verb_guard/install_tier.go b/internal/activator/testdata/rootfs_verb_guard/install_tier.go| new file mode 100644 |
| index 000000000..7616cee0e |
| --- /dev/null |
| +++ b/internal/activator/testdata/rootfs_verb_guard/install_tier.go |
| @@ -0,0 +1,18 @@ |
| +// The two owners are named with their receivers: a Tier write anywhere |
| +// else is refused, a namesake of the tier helper on another receiver is |
| +// not the helper (neither its Tier write nor any other is licensed), and |
| +// neither is a namesake of installProcLocked on another receiver. |
| +package activator |
| + |
| +func (a *Service) installGuardTier(e *actEntry, g Gen) { |
| + e.gen.Tier = g.Tier |
| +} |
| + |
| +func (m *genMember) recordCurrentTierLocked(e *actEntry, p Proc) { |
| + e.gen.Tier = m.gen.Tier |
| + e.proc = p |
| +} |
| + |
| +func (s *shedChild) installProcLocked(e *actEntry, p Proc) { |
| + e.proc = p |
| +} |
diff --git a/internal/activator/testdata/rootfs_verb_guard/install_tuple.go b/internal/activator/testdata/rootfs_verb_guard/install_tuple.go| new file mode 100644 |
| index 000000000..bfa48a563 |
| --- /dev/null |
| +++ b/internal/activator/testdata/rootfs_verb_guard/install_tuple.go |
| @@ -0,0 +1,6 @@ |
| +// The wake's old shape: replica and process in one assignment. |
| +package activator |
| + |
| +func (a *Service) installGuardTuple(e *actEntry, r Rep, p Proc) { |
| + e.rep, e.proc = r, p |
| +} |