carlos / platform Public

activator: a proven worker's handle is kept until it is released, on every path (W2 fix round 1)

Astra W2 r1 P1 (fault family W1.17): the handover forgot a retiring
worker once root proved its unit gone, even while its handle's observer
and listener close were still running. The listener unlinks the log
socket as it closes, so it could remove the successor's socket at the
same path.

A handle is now let go in one place, dropProvenStraysLocked, and only
once workerReleased: its observer has finished and its listeners are
closed (unitProc.Released; listenersClosed is set by closeListeners).
That rule now holds on every entry, not only awake ones. A kept stray
holds a stop, and with it the lease, so a later wake cannot prepare the
name beside it. proveWorkerNames routes a proven retiring worker's
handle through it (handover, stop, lease loss, releasable's re-proof).
runWorkerStart keeps a start's lingering handle on any entry. The
restart's old-handle check reads workerReleased. Supervision still
reads workerEnded, so a crash is still seen at once.

Tests: TestHandoverKeepsALingeringOldHandle (gen 2's jobs is not
prepared until gen 1's handle ends), TestRootfsWorkerStopKeepsALingeringHandle
(a running worker's handle and a start's own),
TestRootfsWorkerRestartWaitsForTheListeners, and
TestWorkerReleasedWaitsForTheListeners. No instancectl change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 258d0e6795f2a09ca640d124264e49d91ccfc954 parent 99426ea
4 files changed, +233 −11
  • internal/activator/rootfs_deploy_test.go +161 −0
  • internal/activator/rootfsworkers.go +43 −11
  • internal/activator/rootfsworkers_test.go +11 −0
  • internal/activator/unitproc.go +18 −0
diff --git a/internal/activator/rootfs_deploy_test.go b/internal/activator/rootfs_deploy_test.go
index 5d34aa1af..d7a3da4ce 100644
--- a/internal/activator/rootfs_deploy_test.go
+++ b/internal/activator/rootfs_deploy_test.go
@@ -771,3 +771,164 @@ func TestRegistrationNeverDropsAHandle(t *testing.T) {
t.Fatalf("the old handle exited %v, %d alive; want it gone and one jobs", old.Exited(), r.b.alive())
}
}
+
+// Astra W2 r1 (P1, fault family W1.17): root proves gen 1's jobs gone
+// while its handle lingers (its observer, and with it the listener's
+// close that unlinks the log socket, has not finished). The handover
+// keeps that handle as a stray, and gen 2's jobs is not prepared (its log
+// socket is not bound) until the old handle has ended.
+func TestHandoverKeepsALingeringOldHandle(t *testing.T) {
+ h := newHandoverFixture(t, "jobs")
+ old := h.w.lastWorker(t, "jobs")
+ old.mu.Lock()
+ old.lingers = true
+ old.mu.Unlock()
+ res := h.a.Swap(context.Background(), h.attempt(ModeOverlap), h.newest)
+ if res.Outcome != SwapDone {
+ t.Fatalf("Swap = %+v", res)
+ }
+ g1, g2 := h.incumbent.N, res.Gen.N
+ ev := h.b.events()
+ if p := index(ev, "proven jobs", lastWithPrefix(ev, "switch:")); p < 0 {
+ t.Fatalf("root did not prove gen 1's jobs gone; order %v", ev)
+ }
+ if s := strayNames(h.a, h.host); !slices.Equal(s, []string{fmt.Sprintf("jobs gen%d", g1)}) {
+ t.Fatalf("strays %v: the lingering handle was let go", s)
+ }
+ for range 3 {
+ h.clk.advance(sidecarRestartBase)
+ h.tick(t)
+ }
+ if n := h.w.launchCount("jobs"); n != 1 {
+ t.Fatalf("gen 2's jobs was prepared (%d preparations) while gen 1's handle lingers; order %v", n, h.b.events())
+ }
+ h.b.record("ended jobs")
+ old.end()
+ for i := 0; len(startsOf(h.b.events(), g2)) == 0; i++ {
+ if i == 5 {
+ t.Fatalf("gen 2's jobs never started after the old handle ended; order %v", h.b.events())
+ }
+ h.clk.advance(sidecarRestartBase)
+ h.tick(t)
+ }
+ ev = h.b.events()
+ if ended, start := index(ev, "ended jobs", 0), startsOf(ev, g2)[0]; start < ended {
+ t.Fatalf("gen 2's jobs started (%d) before gen 1's handle ended (%d); order %v", start, ended, ev)
+ }
+ if n := h.w.launchCount("jobs"); n != 2 {
+ t.Fatalf("jobs prepared %d times, want gen 1's and gen 2's", n)
+ }
+ if s := strayNames(h.a, h.host); len(s) != 0 {
+ t.Fatalf("strays %v after the old handle ended", s)
+ }
+}
+
+// The same rule on a stop (the one place, dropProvenStraysLocked): a
+// worker proven gone whose handle lingers is kept as a stray, so the stop,
+// and with it the lease, waits for the handle; a later wake's worker of
+// the name cannot be prepared beside it. Both the running worker's handle
+// and a start's own (a start that may have run, proven gone) are kept.
+func TestRootfsWorkerStopKeepsALingeringHandle(t *testing.T) {
+ held := func(t *testing.T, r *workerRig, p *fakeWorkerProc) {
+ t.Helper()
+ waitFor(t, "the stop to be kept pending", r.pendingStop)
+ for range 3 {
+ r.clk.advance(actStopTimeout)
+ r.a.sweepOnce()
+ }
+ time.Sleep(50 * time.Millisecond)
+ if !r.pendingStop() || r.b.releaseCount() != 0 {
+ t.Fatalf("the stop finished beside a lingering handle: pending %v, releases %d; order %v", r.pendingStop(), r.b.releaseCount(), r.b.events())
+ }
+ if s := r.strays(); !slices.Equal(s, []string{"jobs"}) {
+ t.Fatalf("strays %v, want the lingering jobs", s)
+ }
+ p.end()
+ r.clk.advance(actStopTimeout)
+ r.a.sweepOnce()
+ waitFor(t, "the stop to finish", func() bool { return !r.a.hasEntry(r.host) })
+ }
+ t.Run("the running worker's handle", func(t *testing.T) {
+ r := newWorkerRig(t, workerHost(), "jobs")
+ r.wake(t)
+ p := r.b.lastWorker(t, "jobs")
+ p.mu.Lock()
+ p.lingers = true
+ p.mu.Unlock()
+ r.a.Bounce(r.host)
+ held(t, r, p)
+ })
+ t.Run("a start's own handle", func(t *testing.T) {
+ r := newWorkerRig(t, workerHost(), "jobs")
+ r.wake(t)
+ r.b.lastWorker(t, "jobs").exit("exited", "1")
+ r.tick(t)
+ block, entered := make(chan struct{}), make(chan string, 1)
+ r.b.wmu.Lock()
+ r.b.block, r.b.entered, r.b.uncertain = block, entered, true
+ r.b.wmu.Unlock()
+ r.clk.advance(sidecarRestartMax)
+ r.a.rootfsTick()
+ select {
+ case <-entered:
+ case <-time.After(10 * time.Second):
+ t.Fatal("the restart never reached root")
+ }
+ r.a.Bounce(r.host)
+ r.b.wmu.Lock()
+ r.b.block, r.b.entered = nil, nil
+ r.b.wmu.Unlock()
+ close(block)
+ waitFor(t, "the start's handle", func() bool { return len(r.b.workers("jobs")) == 2 })
+ held(t, r, r.b.lastWorker(t, "jobs"))
+ })
+}
+
+// workerReleased waits for a unit handle's listener cleanup, not only its
+// observer: a stop that timed out closes the listeners after done.
+func TestWorkerReleasedWaitsForTheListeners(t *testing.T) {
+ up := &unitProc{done: make(chan struct{})}
+ if workerReleased(up) {
+ t.Fatal("a running handle reads as released")
+ }
+ close(up.done)
+ if !workerEnded(up) || workerReleased(up) {
+ t.Fatalf("observer done, listeners open: ended %v, released %v; want ended and not released", workerEnded(up), workerReleased(up))
+ }
+ up.closeListeners()
+ if !workerReleased(up) {
+ t.Fatal("observer done and listeners closed, still not released")
+ }
+}
+
+// A restart waits for the old handle's release, not only its end: a
+// worker that exited but whose listeners are still open (its observer
+// returned, the listener close still to come) is proven gone, and the
+// restart prepares nothing until the listeners are closed [pf W1.17].
+func TestRootfsWorkerRestartWaitsForTheListeners(t *testing.T) {
+ r := newWorkerRig(t, workerHost(), "jobs")
+ r.wake(t)
+ old := r.b.lastWorker(t, "jobs")
+ old.mu.Lock()
+ old.listening = true
+ old.mu.Unlock()
+ old.exit("exited", "1")
+ r.tick(t) // the exit is noted
+ for range 3 {
+ r.clk.advance(sidecarRestartMax)
+ r.tick(t)
+ }
+ if n := r.b.launchCount("jobs"); n != 1 {
+ t.Fatalf("jobs was prepared again (%d preparations) while its old handle's listeners are open", n)
+ }
+ old.mu.Lock()
+ old.listening = false
+ old.mu.Unlock()
+ for i := 0; r.b.launchCount("jobs") < 2; i++ {
+ if i == 5 {
+ t.Fatal("jobs never restarted after its old handle was released")
+ }
+ r.clk.advance(sidecarRestartMax)
+ r.tick(t)
+ }
+}
diff --git a/internal/activator/rootfsworkers.go b/internal/activator/rootfsworkers.go
index a7afb1fe6..ab8d12f1f 100644
--- a/internal/activator/rootfsworkers.go
+++ b/internal/activator/rootfsworkers.go
@@ -296,7 +296,7 @@ func (a *Service) handOverRootfsWorkers(e *actEntry) {
a.mu.Unlock()
if old {
if !a.stopRootfsWorkersWhere(e, retiring) {
- log.Printf("activator: %s: a worker of an earlier generation is not proven stopped; generation %d's workers start once it is", rt.Host, g.N)
+ log.Printf("activator: %s: a worker of an earlier generation is not yet proven stopped and released; generation %d's workers start once it is", rt.Host, g.N)
}
// The proven old workers' pins go; a stray's stay until it is
// proven.
@@ -424,10 +424,11 @@ func (a *Service) runWorkerStart(ctx context.Context, wb RootfsWorkerBackend, e
switch {
case stray:
a.strayWorkerLocked(e, wk, now, started)
- case started != nil && !workerEnded(started) && e.state == actAwake:
- // Proven gone, but the handle's observer has not returned: kept
- // as a stray, due again at once, so no worker of its name is
- // prepared beside it [pf W1.17].
+ case started != nil && !workerReleased(started):
+ // Proven gone, but the handle's observer or its listener
+ // cleanup has not finished: kept as a stray, due again at
+ // once, so no worker of its name is prepared beside it, on
+ // this entry or (the stop held) a later one [pf W1.17].
a.strayWorkerLocked(e, wk, now, started)
wk.strayAt = time.Time{}
}
@@ -588,6 +589,27 @@ func workerEnded(p Proc) bool {
return p.Exited()
}
+// workerReleased reports whether a worker's handle has let go of
+// everything a later invocation of its name reuses [pf W1.17]: a unit
+// handle once its observer has finished and its listeners are closed (the
+// log listener unlinks its socket path as it closes, which would remove a
+// successor's socket bound at the same path), any other once Exited. A
+// unit whose stop timed out closes its listeners only after its observer
+// returns, so a proven unit's handle may still hold them. Supervision
+// reads workerEnded; every reuse of a name reads this.
+func workerReleased(p Proc) bool {
+ if r, ok := p.(workerReleaser); ok {
+ return r.Released()
+ }
+ return workerEnded(p)
+}
+
+// workerReleaser is a worker handle that tells when it has let go of what
+// a later invocation of its name reuses (unitProc.Released).
+type workerReleaser interface {
+ Released() bool
+}
+
// noteWorkerExitLocked notes one exit of wk (or one failed start): the
// backoff (sidecarRestartBase doubling to sidecarRestartMax, reset by a
// run of sidecarHealthyRun), the restart count, and the member's event:
@@ -717,7 +739,7 @@ func (a *Service) restartRootfsWorkerLocked(e *actEntry, wk *rootfsWorker) {
// listeners closed) before prepare binds the log socket again.
// One that has not is no crash: asked again after the base
// backoff, its stray kept until then.
- if slices.ContainsFunc(handles, func(p Proc) bool { return !workerEnded(p) }) {
+ if slices.ContainsFunc(handles, func(p Proc) bool { return !workerReleased(p) }) {
wk.retryAt = a.now().Add(sidecarRestartBase)
wk.starting = false
close(wk.startDone)
@@ -735,15 +757,19 @@ func (a *Service) restartRootfsWorkerLocked(e *actEntry, wk *rootfsWorker) {
// dropProvenStraysLocked forgets the strays named name once a proof by
// name has proven their unit gone (one unit per host and name proves them
-// all). On an awake entry, where a restart may follow, a stray whose
-// handle has not ended yet is kept, due again at once, so no worker of
-// its name starts beside its observer [pf W1.17]. The caller holds a.mu.
+// all). It is the one place a proven worker's handle is let go [pf
+// W1.17]: a stray whose handle has not been released (workerReleased) is
+// kept, due again at once, so no worker of its name is prepared beside
+// its observer or its listener's close. That holds on every entry: on an
+// awake one a restart or a handed-over set may follow; on a stopping one
+// the kept stray holds the stop, and with it the lease, so a later wake's
+// worker of the name cannot follow either. The caller holds a.mu.
func (a *Service) dropProvenStraysLocked(e *actEntry, name string) {
e.strayRootfsWorkers = slices.DeleteFunc(e.strayRootfsWorkers, func(s *rootfsWorker) bool {
if s.name != name {
return false
}
- if e.state == actAwake && s.unproven != nil && !workerEnded(s.unproven) {
+ if s.unproven != nil && !workerReleased(s.unproven) {
s.strayAt = time.Time{}
return false
}
@@ -833,9 +859,15 @@ func (a *Service) proveWorkerNames(e *actEntry, names []string) {
if errs[i] == nil {
// An awake entry's worker of its current generation is the
// not-yet-restarted replacement of a stray (proc unset): the
- // supervisor starts it. A retiring one is forgotten.
+ // supervisor starts it. A retiring one leaves; its handle, if
+ // it has one, goes through dropProvenStraysLocked as a stray,
+ // which lets it go only once it is released [pf W1.17].
if wk := e.rootfsWorkers[n]; wk != nil && !wk.starting && workerRetiringLocked(e, wk) {
delete(e.rootfsWorkers, n)
+ if wk.proc != nil {
+ wk.strayAt, wk.unproven = time.Time{}, wk.proc
+ e.strayRootfsWorkers = append(e.strayRootfsWorkers, wk)
+ }
}
a.dropProvenStraysLocked(e, n)
} else {
diff --git a/internal/activator/rootfsworkers_test.go b/internal/activator/rootfsworkers_test.go
index 7bef9719b..b9bd07b98 100644
--- a/internal/activator/rootfsworkers_test.go
+++ b/internal/activator/rootfsworkers_test.go
@@ -39,6 +39,17 @@ type fakeWorkerProc struct {
// lingers: its observer has not returned, so neither a stop nor a
// proof of its unit ends it (a stray's handle, W1.17).
lingers bool
+ // listening: it has ended, but its listeners are not closed yet (a
+ // unit handle whose stop timed out closes them only after its observer
+ // returns), so it is not Released.
+ listening bool
+}
+
+// Released is workerReleased's answer: ended, its listeners closed.
+func (p *fakeWorkerProc) Released() bool {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ return p.dead && !p.listening
}
func (p *fakeWorkerProc) Stop(context.Context) error {
diff --git a/internal/activator/unitproc.go b/internal/activator/unitproc.go
index a1ba655f5..a368938d7 100644
--- a/internal/activator/unitproc.go
+++ b/internal/activator/unitproc.go
@@ -53,6 +53,11 @@ type unitProc struct {
// returns ErrStillRunning, which otherwise leaves ctl and logs open
// for as long as the agent runs. Armed at most once per unitProc.
listenersArmed atomic.Bool
+ // listenersClosed is set once closeListeners has closed both
+ // listeners (the log listener unlinks its socket as it closes), so a
+ // rootfs worker's next invocation of the same name may bind them
+ // again (workerReleased, W1.17).
+ listenersClosed atomic.Bool
// stopRequested is set before the FIRST Control.Stop this proc's owner
// ever issues for it: once true, the observer stops treating a
// differently-invoked or not-yet-invoked unit as a FOREIGN restart to
@@ -318,6 +323,18 @@ func (p *unitProc) finaliseGen() {
}
}
+// Released reports whether the handle has let go of everything a later
+// invocation of its unit reuses: its observer has finished and both
+// listeners are closed (workerReleased, W1.17).
+func (p *unitProc) Released() bool {
+ select {
+ case <-p.done:
+ return p.listenersClosed.Load()
+ default:
+ return false
+ }
+}
+
func (p *unitProc) closeListeners() {
if p.closeOnce.Swap(true) {
return
@@ -328,6 +345,7 @@ func (p *unitProc) closeListeners() {
if p.logs != nil {
p.logs.Close()
}
+ p.listenersClosed.Store(true)
}
// observe is the goroutine of design §6.5. It proves termination from two