activator: never-started workers need no proof; the doWake guard follows interfaces and the control launch (W1 fix round 3, P2s)
Astra's W1 round 3 was READY AFTER FIXES, with no P1. This applies both P2s. P2: dropUnlaunchedLocked forgets, on an entry no longer awake, every worker with no start in flight, no handle and no stray of its name. That covers a pending declaration never started, a start refused before root or one that ran nothing, and a start already proven gone. It asks root nothing. The stop (before and after its starts end), the lease-loss stop and releasable's re-proof call it first. Every launch that may have run is a handle or a stray, and keeps its proof. P2: the doWake guard's reference graph now joins each interface method named to its implementations declared in the package. That covers Backend, ProcessReader, RootfsWorkerBackend and RootfsControl. StartRootfsWorker, the control-level launch, joins the forbidden list. Two negative fixtures, an interface-routed launch and a direct StartRootfsWorker call, must be refused. A control checks that doWake reaches ExecBackend.Processes through ProcessReader. The comment now names where the graph under-approximates.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+265
−26
internal/activator/rootfs_install_guard_test.go+128 −25internal/activator/rootfsworkers.go+29 −1internal/activator/rootfsworkers_test.go+66 −0internal/activator/testdata/rootfs_verb_guard/wake_guard_control.go+16 −0internal/activator/testdata/rootfs_verb_guard/wake_guard_interface.go+26 −0
diff --git a/internal/activator/rootfs_install_guard_test.go b/internal/activator/rootfs_install_guard_test.go| index f43fc13bb..bcaf75598 100644 |
| --- a/internal/activator/rootfs_install_guard_test.go |
| +++ b/internal/activator/rootfs_install_guard_test.go |
| @@ -276,18 +276,33 @@ func TestResidentProcessInstallsGoThroughOnePlace(t *testing.T) { |
| // workerStartFuncs is every function through which a rootfs worker is |
| // started or installed: none may be reachable from doWake (Paul's ruling, |
| // W1 round 2: no worker starts inside doWake; the tick starts them). |
| +// StartRootfsWorker is the control-level launch (RootfsControl's, root's |
| +// own door), so a launch that skips LaunchRootfsWorker is refused too. |
| var workerStartFuncs = []string{"superviseRootfsLocked", "restartRootfsWorkerLocked", "runWorkerStart", |
| - "installRootfsWorkerLocked", "LaunchRootfsWorker", "rootfsTick"} |
| + "installRootfsWorkerLocked", "LaunchRootfsWorker", "StartRootfsWorker", "rootfsTick"} |
| -// funcGraph is the package's static reference graph: for each function or |
| -// method declared in a non-test file, every package function or method |
| -// (an interface's included, by its abstract method) its body names, called |
| -// or not, function literals and goroutines included. It over-approximates |
| -// what a call may reach, never under. |
| -func funcGraph(pkg *packages.Package) (edges map[*types.Func][]*types.Func, byName map[string]*types.Func) { |
| +// funcGraph is the package's static reference graph over the files it |
| +// reads (the package's own non-test files; its guard fixtures too when |
| +// withFixtures): for each function or method declared there, every |
| +// package function or method its body names, called or not, function |
| +// literals and goroutines included. An interface method it names is |
| +// joined to every implementation declared in those files (each named type |
| +// T or *T that implements the interface), so a call routed through |
| +// Backend, ProcessReader, RootfsWorkerBackend or RootfsControl reaches the |
| +// package's own implementations. |
| +// |
| +// It under-approximates in three ways: an implementation outside this |
| +// package (instancectl's control client, for one) is not followed, so the |
| +// control-level launch is refused by its interface method's name |
| +// (StartRootfsWorker); a function value reached through a variable, a |
| +// field or a parameter is followed only where some reached body names |
| +// that function; and reflection is not followed. |
| +func funcGraph(pkg *packages.Package, withFixtures bool) (edges map[*types.Func][]*types.Func, byName map[string]*types.Func) { |
| edges, byName = map[*types.Func][]*types.Func{}, map[string]*types.Func{} |
| + declared := map[*types.Func]bool{} |
| + var ifaceUses []*types.Func |
| for base, f := range packageFiles(pkg) { |
| - if strings.HasPrefix(base, verbGuardFixturePrefix) || strings.HasSuffix(base, "_test.go") { |
| + if strings.HasSuffix(base, "_test.go") || (!withFixtures && strings.HasPrefix(base, verbGuardFixturePrefix)) { |
| continue |
| } |
| for _, d := range f.Decls { |
| @@ -299,20 +314,64 @@ func funcGraph(pkg *packages.Package) (edges map[*types.Func][]*types.Func, byNa |
| if !ok { |
| continue |
| } |
| + declared[fn] = true |
| byName[fn.FullName()] = fn |
| ast.Inspect(fd.Body, func(n ast.Node) bool { |
| if id, ok := n.(*ast.Ident); ok { |
| if callee, ok := pkg.TypesInfo.Uses[id].(*types.Func); ok && callee.Pkg() == pkg.Types { |
| edges[fn] = append(edges[fn], callee) |
| + if interfaceMethod(callee) != nil { |
| + ifaceUses = append(ifaceUses, callee) |
| + } |
| } |
| } |
| return true |
| }) |
| } |
| } |
| + // Each interface method named is joined to its implementations. |
| + scope := pkg.Types.Scope() |
| + done := map[*types.Func]bool{} |
| + for _, m := range ifaceUses { |
| + if done[m] { |
| + continue |
| + } |
| + done[m] = true |
| + iface := interfaceMethod(m) |
| + for _, name := range scope.Names() { |
| + tn, ok := scope.Lookup(name).(*types.TypeName) |
| + if !ok { |
| + continue |
| + } |
| + named, ok := tn.Type().(*types.Named) |
| + if !ok || named.TypeParams().Len() > 0 || types.IsInterface(named) { |
| + continue |
| + } |
| + for _, typ := range []types.Type{named, types.NewPointer(named)} { |
| + if !types.Implements(typ, iface) { |
| + continue |
| + } |
| + obj, _, _ := types.LookupFieldOrMethod(typ, true, pkg.Types, m.Name()) |
| + if impl, ok := obj.(*types.Func); ok && declared[impl] && !slices.Contains(edges[m], impl) { |
| + edges[m] = append(edges[m], impl) |
| + } |
| + } |
| + } |
| + } |
| return edges, byName |
| } |
| +// interfaceMethod is the interface fn is a method of, or nil for a |
| +// concrete function or method. |
| +func interfaceMethod(fn *types.Func) *types.Interface { |
| + sig, ok := fn.Type().(*types.Signature) |
| + if !ok || sig.Recv() == nil { |
| + return nil |
| + } |
| + iface, _ := sig.Recv().Type().Underlying().(*types.Interface) |
| + return iface |
| +} |
| + |
| // reachable is every function reachable from root in g, each with the |
| // path that first reached it. |
| func reachable(g map[*types.Func][]*types.Func, root *types.Func) map[*types.Func][]string { |
| @@ -332,39 +391,83 @@ func reachable(g map[*types.Func][]*types.Func, root *types.Func) map[*types.Fun |
| return seen |
| } |
| +// reachedNamed is the path to a function named name in m, or nil. |
| +func reachedNamed(m map[*types.Func][]string, name string) []string { |
| + for fn, path := range m { |
| + if fn.Name() == name { |
| + return path |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// wakeStartViolations is every worker start reachable from root in g, as |
| +// "<name>: <path>", sorted. |
| +func wakeStartViolations(g map[*types.Func][]*types.Func, root *types.Func) []string { |
| + from := reachable(g, root) |
| + var out []string |
| + for _, name := range workerStartFuncs { |
| + if path := reachedNamed(from, name); path != nil { |
| + out = append(out, name+": "+strings.Join(path, " -> ")) |
| + } |
| + } |
| + slices.Sort(out) |
| + return out |
| +} |
| + |
| +// wakeGuardFixtures are the doWake guard's negative fixtures: each root |
| +// starts a worker by a route the guard must follow, with the start it |
| +// must report. |
| +var wakeGuardFixtures = map[string]string{ |
| + // a launch behind an interface: only the interface's method is named |
| + "(*github.com/carlosframework/platform/internal/activator.Service).wakeGuardInterfaceRoot": "LaunchRootfsWorker", |
| + // the control-level launch, with no LaunchRootfsWorker on the path |
| + "(*github.com/carlosframework/platform/internal/activator.Service).wakeGuardControlRoot": "StartRootfsWorker", |
| +} |
| + |
| // Paul's ruling (W1 round 2): nothing doWake reaches starts or installs a |
| // worker. Its controls: the walk reaches doWake's own worker step |
| -// (declareRootfsWorkers), and from the resident tick it does reach a |
| -// worker start, so a guard that resolved nothing cannot pass. |
| +// (declareRootfsWorkers) and, through ProcessReader, the backends' own |
| +// Processes; from the resident tick it does reach a worker start; and |
| +// each negative fixture's route is refused. A guard that resolved nothing |
| +// cannot pass. |
| func TestRootfsWorkerNeverStartsInsideDoWake(t *testing.T) { |
| pkg, err := loadVerbGuardPackage() |
| if err != nil { |
| t.Fatal(err) |
| } |
| - g, byName := funcGraph(pkg) |
| + g, byName := funcGraph(pkg, false) |
| root := byName["(*github.com/carlosframework/platform/internal/activator.Service).doWake"] |
| tick := byName["(*github.com/carlosframework/platform/internal/activator.Service).rootfsTick"] |
| if root == nil || tick == nil { |
| t.Fatal("doWake or rootfsTick not resolved: the guard proves nothing") |
| } |
| fromWake := reachable(g, root) |
| - named := func(m map[*types.Func][]string, name string) []string { |
| - for fn, path := range m { |
| - if fn.Name() == name { |
| - return path |
| - } |
| - } |
| - return nil |
| - } |
| - if named(fromWake, "declareRootfsWorkers") == nil { |
| + if reachedNamed(fromWake, "declareRootfsWorkers") == nil { |
| t.Fatal("doWake does not reach declareRootfsWorkers: the walk resolves nothing") |
| } |
| - if named(reachable(g, tick), "runWorkerStart") == nil { |
| + execProcesses := byName["(*github.com/carlosframework/platform/internal/activator.ExecBackend).Processes"] |
| + if _, ok := fromWake[execProcesses]; execProcesses == nil || !ok { |
| + t.Error("doWake does not reach ExecBackend.Processes through ProcessReader: interface calls are not resolved") |
| + } |
| + if reachedNamed(reachable(g, tick), "runWorkerStart") == nil { |
| t.Fatal("the tick does not reach runWorkerStart: the walk resolves nothing") |
| } |
| - for _, name := range workerStartFuncs { |
| - if path := named(fromWake, name); path != nil { |
| - t.Errorf("doWake reaches %s, a worker start: %s", name, strings.Join(path, " -> ")) |
| - } |
| + for _, v := range wakeStartViolations(g, root) { |
| + t.Errorf("doWake reaches a worker start, %s", v) |
| + } |
| + |
| + fg, fbyName := funcGraph(pkg, true) |
| + for name, want := range wakeGuardFixtures { |
| + t.Run(name, func(t *testing.T) { |
| + froot := fbyName[name] |
| + if froot == nil { |
| + t.Fatalf("fixture root %s was not loaded", name) |
| + } |
| + got := wakeStartViolations(fg, froot) |
| + if !slices.ContainsFunc(got, func(v string) bool { return strings.HasPrefix(v, want+":") }) { |
| + t.Fatalf("the guard did not refuse %s's start of %s; it reported %v", name, want, got) |
| + } |
| + }) |
| } |
| } |
diff --git a/internal/activator/rootfsworkers.go b/internal/activator/rootfsworkers.go| index 3a7599377..562585be4 100644 |
| --- a/internal/activator/rootfsworkers.go |
| +++ b/internal/activator/rootfsworkers.go |
| @@ -822,8 +822,12 @@ func (a *Service) stopRootfsWorkers(e *actEntry) bool { |
| // start has ended (by then what it started is installed, proven or a |
| // stray), never holding the others behind it (C7). The gates admitted |
| // each start with workerStartLead left on the lease, so the start ends |
| - // on its bound and its proof fits before the safety deadline. |
| + // on its bound and its proof fits before the safety deadline. A |
| + // worker that cannot be running (a pending declaration never started, |
| + // or a start that ran nothing or was proven gone) is forgotten with no |
| + // request to root (dropUnlaunchedLocked). |
| a.mu.Lock() |
| + a.dropUnlaunchedLocked(e) |
| var waits []chan struct{} |
| var starting, now []string |
| for _, n := range workerNamesLocked(e) { |
| @@ -846,6 +850,10 @@ func (a *Service) stopRootfsWorkers(e *actEntry) bool { |
| <-w |
| } |
| a.mu.Lock() |
| + // A start that ended having run nothing leaves nothing to prove. |
| + a.dropUnlaunchedLocked(e) |
| + known := workerNamesLocked(e) |
| + starting = slices.DeleteFunc(starting, func(n string) bool { return !slices.Contains(known, n) }) |
| later, busyLater := claimWorkerNamesLocked(e, starting) |
| a.mu.Unlock() |
| a.proveWorkerNames(e, later) |
| @@ -858,6 +866,24 @@ func (a *Service) stopRootfsWorkers(e *actEntry) bool { |
| return len(e.rootfsWorkers) == 0 && len(e.strayRootfsWorkers) == 0 |
| } |
| +// dropUnlaunchedLocked forgets, on an entry no longer awake, every worker |
| +// that cannot be running: no start in flight, no handle and no stray of |
| +// its name. That is a pending declaration never started (Astra W1 r3), a |
| +// start refused before root or one that ran nothing, or a start already |
| +// proven gone. Every launch that may have run is a handle or a stray, so |
| +// it keeps its proof. It asks root nothing. The caller holds a.mu. |
| +func (a *Service) dropUnlaunchedLocked(e *actEntry) { |
| + if e.state == actAwake { |
| + return |
| + } |
| + for n, wk := range e.rootfsWorkers { |
| + if wk.starting || wk.proc != nil || slices.ContainsFunc(e.strayRootfsWorkers, func(s *rootfsWorker) bool { return s.name == n }) { |
| + continue |
| + } |
| + delete(e.rootfsWorkers, n) |
| + } |
| +} |
| + |
| // stopRootfsWorkersAtLoss stops a resident's workers the moment its lease |
| // is lost [pf W1.10], beside the generation kills and without waiting for |
| // the entry's stop: each worker not mid-start is stopped and proven gone |
| @@ -868,6 +894,7 @@ func (a *Service) stopRootfsWorkers(e *actEntry) bool { |
| // before the safety deadline (C7). No kill: root refuses one for a worker. |
| func (a *Service) stopRootfsWorkersAtLoss(e *actEntry) { |
| a.mu.Lock() |
| + a.dropUnlaunchedLocked(e) |
| var names []string |
| for _, n := range workerNamesLocked(e) { |
| if wk := e.rootfsWorkers[n]; wk == nil || !wk.starting { |
| @@ -886,6 +913,7 @@ func (a *Service) stopRootfsWorkersAtLoss(e *actEntry) { |
| // entry. Never call it with a.mu held. |
| func (a *Service) rootfsWorkersGone(e *actEntry) bool { |
| a.mu.Lock() |
| + a.dropUnlaunchedLocked(e) |
| if len(e.rootfsWorkers) == 0 && len(e.strayRootfsWorkers) == 0 { |
| a.mu.Unlock() |
| return true |
diff --git a/internal/activator/rootfsworkers_test.go b/internal/activator/rootfsworkers_test.go| index 100ed7c04..7bef9719b 100644 |
| --- a/internal/activator/rootfsworkers_test.go |
| +++ b/internal/activator/rootfsworkers_test.go |
| @@ -1570,6 +1570,10 @@ func TestRootfsWorkersStopAtLeaseLossUnwatched(t *testing.T) { |
| r := newWorkerRig(t, rt, "jobs", "mailer") |
| r.a.residentLister = &fakeResidents{routes: []registry.Route{rt}} |
| startResident(t, r.a, r.host) |
| + r.tick(t) // the first pass starts the declared workers |
| + if n := len(r.b.workers("jobs")) + len(r.b.workers("mailer")); n != 2 { |
| + t.Fatalf("%d workers started, want 2", n) |
| + } |
| r.a.mu.Lock() |
| watched := r.a.entries[r.host].ownerDone != nil |
| r.a.mu.Unlock() |
| @@ -1960,6 +1964,68 @@ func TestRootfsWorkerFailedStartsCountOnce(t *testing.T) { |
| }) |
| } |
| +// [Astra W1 r3 P2] A declaration the tick has not started yet cannot be |
| +// running: a stop, Shutdown or a lost lease before the first tick forgets |
| +// it with no request to root, makes no stray, holds nothing pending, and |
| +// the lease is released, though every worker stop root would answer fails. |
| +// A worker that did start still needs its proof. |
| +func TestRootfsWorkerPendingNeedsNoProof(t *testing.T) { |
| + failAll := func(string) error { return instancectl.ErrGenUnproven } |
| + noWorkerAsked := func(t *testing.T, r *workerRig) { |
| + t.Helper() |
| + for _, ev := range r.b.events() { |
| + if strings.HasPrefix(ev, "gone ") || strings.HasPrefix(ev, "start jobs") || strings.HasPrefix(ev, "start mailer") { |
| + t.Fatalf("root was asked about a worker never started: %v", r.b.events()) |
| + } |
| + } |
| + if index(r.b.events(), "close", 0) < 0 { |
| + t.Fatalf("the lease was not released: %v", r.b.events()) |
| + } |
| + } |
| + t.Run("a bounce", func(t *testing.T) { |
| + r := newWorkerRig(t, workerHost(), "jobs", "mailer") |
| + r.wakeOnly(t) |
| + r.b.setGone(failAll) |
| + r.a.Bounce(r.host) |
| + waitFor(t, "the stop to finish", func() bool { return !r.a.hasEntry(r.host) }) |
| + noWorkerAsked(t, r) |
| + }) |
| + t.Run("Shutdown", func(t *testing.T) { |
| + logs := captureLogLines(t) |
| + r := newWorkerRig(t, workerHost(), "jobs", "mailer") |
| + r.wakeOnly(t) |
| + r.b.setGone(failAll) |
| + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| + defer cancel() |
| + r.a.Shutdown(ctx) |
| + if r.a.hasEntry(r.host) { |
| + t.Fatal("Shutdown kept the entry") |
| + } |
| + noWorkerAsked(t, r) |
| + if n := logs.matching("worker(s) not proven stopped"); n != 0 { |
| + t.Fatalf("the exit report names %d never-started workers; logs:\n%s", n, logs) |
| + } |
| + }) |
| + t.Run("the lease lost", func(t *testing.T) { |
| + r, rep := residentWokenRig(t, time.Hour) |
| + r.b.setGone(failAll) |
| + rep().lose(lease.ErrLost) |
| + waitFor(t, "the loss's stop to finish", func() bool { return !r.a.hasEntry(r.host) }) |
| + noWorkerAsked(t, r) |
| + }) |
| + t.Run("a started worker keeps its proof", func(t *testing.T) { |
| + r := newWorkerRig(t, workerHost(), "jobs") |
| + r.wake(t) |
| + r.b.setGone(failAll) |
| + r.a.Bounce(r.host) |
| + waitFor(t, "the stop to be kept pending", r.pendingStop) |
| + if got := r.strays(); !slices.Equal(got, []string{"jobs"}) { |
| + t.Fatalf("strays %v, want the started jobs", got) |
| + } |
| + r.b.setGone(nil) |
| + }) |
| +} |
| + |
| // The rootfs launch fixture's worker seams: the real backend's, over the |
| // rig's own row, as its other seams are. |
| func (b *rootfsLaunch) LaunchRootfsWorker(ctx context.Context, _ registry.Route, name string, g Gen, web Proc) (Proc, error) { |
diff --git a/internal/activator/testdata/rootfs_verb_guard/wake_guard_control.go b/internal/activator/testdata/rootfs_verb_guard/wake_guard_control.go| new file mode 100644 |
| index 000000000..d02003a22 |
| --- /dev/null |
| +++ b/internal/activator/testdata/rootfs_verb_guard/wake_guard_control.go |
| @@ -0,0 +1,16 @@ |
| +// The doWake guard's control fixture (Astra W1 r3): a worker launched |
| +// through the control-level primitive directly (RootfsControl's |
| +// StartRootfsWorker), with no LaunchRootfsWorker on the path. The guard |
| +// must refuse it. |
| +package activator |
| + |
| +import ( |
| + "context" |
| + |
| + "github.com/carlosframework/platform/internal/instancectl" |
| +) |
| + |
| +func (a *Service) wakeGuardControlRoot(ctx context.Context, rc RootfsControl, host string) error { |
| + _, err := rc.StartRootfsWorker(ctx, host, "jobs", 0, instancectl.StagedImage{}) |
| + return err |
| +} |
diff --git a/internal/activator/testdata/rootfs_verb_guard/wake_guard_interface.go b/internal/activator/testdata/rootfs_verb_guard/wake_guard_interface.go| new file mode 100644 |
| index 000000000..88ef192f7 |
| --- /dev/null |
| +++ b/internal/activator/testdata/rootfs_verb_guard/wake_guard_interface.go |
| @@ -0,0 +1,26 @@ |
| +// The doWake guard's interface fixture (Astra W1 r3): a worker launch |
| +// behind an interface. The root names only the interface's method; its one |
| +// implementation in the package launches the worker. The guard must follow |
| +// the call into the implementation and refuse it. |
| +package activator |
| + |
| +import ( |
| + "context" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +type wakeGuardSeam interface { |
| + wakeGuardPrepare(ctx context.Context, rt registry.Route) error |
| +} |
| + |
| +type wakeGuardLauncher struct{ b *ExecBackend } |
| + |
| +func (l *wakeGuardLauncher) wakeGuardPrepare(ctx context.Context, rt registry.Route) error { |
| + _, err := l.b.LaunchRootfsWorker(ctx, rt, "jobs", Gen{}, nil) |
| + return err |
| +} |
| + |
| +func (a *Service) wakeGuardInterfaceRoot(ctx context.Context, s wakeGuardSeam, rt registry.Route) error { |
| + return s.wakeGuardPrepare(ctx, rt) |
| +} |