carlos / platform Public

activator: rootfs workers fit before the lease's safety deadline; failed proofs and starts are counted once (W1 fix round 1)

Astra's W1 round 1 (NOT READY), all three findings.

P1 (C7): a worker start is admitted only with workerStartLead left
before the lease's safety deadline, read from the lease's SafeUntil
through watchOf, as runRelease reads it. The lead is the start's bound
(workerStartBound, now on doWake's starts too) plus the worker proof's
bound. So a start root may not interrupt still ends, and is stopped and
proven, before another box can take the lease. A stop proves every
worker not starting at once, in parallel; a starting one is proven once
its start has ended, never holding the others behind it.

P2: a failed proof before a restart keeps the existing stray and
reschedules. Only a worker's own last handle becomes a stray, once, so
repeated failures never add strays and proof retries are no crashes.

P2: a launch that fails (not a refusal) is noted as one crash where it
fails, before any stray's replacement takes its pacing. The backoff
advances and C10 is reached.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 3576d003defe1a356fbf85b6f510e7fac66fb00b parent 2e75556
2 files changed, +460 −49
  • internal/activator/rootfsworkers.go +137 −42
  • internal/activator/rootfsworkers_test.go +323 −7
diff --git a/internal/activator/rootfsworkers.go b/internal/activator/rootfsworkers.go
index a6ca493af..c58228308 100644
--- a/internal/activator/rootfsworkers.go
+++ b/internal/activator/rootfsworkers.go
@@ -101,6 +101,45 @@ const (
// The termination proof is RootfsWorkerGone's, on its own bounds.
const workerListenerWait = 2 * time.Second
+// workerStartBound bounds one worker start (doWake's or a restart's), from
+// its preparation to root's answer: one control request. A var so tests
+// shrink it.
+var workerStartBound = instancectl.DeadlineControl
+
+// workerProofBound is how long a worker's stop takes to be proven at most:
+// RootfsWorkerGone's own bounds (root's stop on DeadlineControl, then the
+// cgroup wait on roleEmptyBound) and the handle's listener close. A var so
+// tests shrink it.
+var workerProofBound = instancectl.DeadlineControl + roleEmptyBound + workerListenerWait
+
+// workerStartLead is the safe time a worker start needs left on the lease
+// when its gate is asked (C7, the rule R0.2 sets for a release): the start
+// may not be interruptible once root has it, so it must be able to end on
+// its own bound and then be stopped and proven inside the lease's safety
+// deadline. A gate with less left refuses the start.
+func workerStartLead() time.Duration { return workerStartBound + workerProofBound }
+
+// errWorkerNoHeadroom refuses a worker start that could not be started,
+// stopped and proven before the lease's safety deadline.
+var errWorkerNoHeadroom = errors.New("activator: the instance's lease is too close to its safety deadline to start a worker")
+
+// workerHeadroom is the gates' lease deadline check, read as runRelease
+// reads it (watchOf, SafeUntil, on the real clock as the lease runs): nil
+// when rep's safety deadline is more than workerStartLead away, or when rep
+// has no lease signal (the tick's rep.Err check is then its only line, as
+// for its web). Never call it with a.mu held.
+func workerHeadroom(rep Rep) error {
+ held := watchOf(rep)
+ if held == nil {
+ return nil
+ }
+ until, err := held.SafeUntil()
+ if err != nil || time.Until(until) <= workerStartLead() {
+ return fmt.Errorf("%w: %w", errSwapLeaseLost, errWorkerNoHeadroom)
+ }
+ return nil
+}
+
// errWorkerStray refuses a worker's start beside a stray it must not run
// beside (W1.14): a worker of another generation, or of the same name,
// whose stop is not proven.
@@ -199,7 +238,9 @@ func (a *Service) startRootfsWorkers(ctx context.Context, e *actEntry, rt regist
}
e.rootfsWorkers[name] = wk
a.mu.Unlock()
- a.runWorkerStart(ctx, wb, e, rt, wk, w.gen, w.proc, a.workerWakeGate(e, w.gen, w.rep, name))
+ sctx, cancel := context.WithTimeout(ctx, workerStartBound)
+ a.runWorkerStart(sctx, wb, e, rt, wk, w.gen, w.proc, a.workerWakeGate(e, w.gen, w.rep, name))
+ cancel()
}
}
@@ -207,9 +248,14 @@ func (a *Service) startRootfsWorkers(ctx context.Context, e *actEntry, rt regist
// under a.mu right before root is (runExecGate): the entry is the host's,
// unfenced, not shutting down (ownedLocked, waking) and still waking; the
// startup sweep proved the box clean; the wake's replica holds the lease;
-// no release record of the host is open; and the stray rule.
+// no release record of the host is open; the stray rule; and the lease's
+// safety deadline far enough away to start, stop and prove the worker
+// (workerHeadroom, C7).
func (a *Service) workerWakeGate(e *actEntry, g Gen, rep Rep, name string) func() error {
return func() error {
+ if err := workerHeadroom(rep); err != nil {
+ return err
+ }
a.mu.Lock()
defer a.mu.Unlock()
if err := a.ownedLocked(e, true); err != nil {
@@ -235,30 +281,41 @@ func (a *Service) workerWakeGate(e *actEntry, g Gen, rep Rep, name string) func(
// under a.mu right before root is: the startup sweep proved the box clean;
// the entry is the host's, awake, unfenced, its replica holding the lease
// (ownedLocked); its current generation is the worker's; the worker is
-// still the entry's; no release record is open; and the stray rule.
+// still the entry's; no release record is open; the stray rule; and the
+// lease's safety deadline far enough away (workerHeadroom, C7).
func (a *Service) workerGate(e *actEntry, wk *rootfsWorker) func() error {
return func() error {
a.mu.Lock()
- defer a.mu.Unlock()
- if !a.rootfsSwept.Load() {
- return errRootfsUnswept
- }
- if err := a.ownedLocked(e, false); err != nil {
+ rep := e.rep
+ err := a.workerGateLocked(e, wk)
+ a.mu.Unlock()
+ if err != nil {
return err
}
- if e.gen.N != wk.gen {
- return errWorkerGeneration
- }
- if e.rootfsWorkers[wk.name] != wk {
- return errSwapSuperseded
- }
- if a.releaseOps[e.host] != nil {
- return errReleaseOpen
- }
- return workerStrayRuleLocked(e, wk.gen, wk.name)
+ return workerHeadroom(rep)
}
}
+// workerGateLocked is workerGate's checks under a.mu. The caller holds it.
+func (a *Service) workerGateLocked(e *actEntry, wk *rootfsWorker) error {
+ if !a.rootfsSwept.Load() {
+ return errRootfsUnswept
+ }
+ if err := a.ownedLocked(e, false); err != nil {
+ return err
+ }
+ if e.gen.N != wk.gen {
+ return errWorkerGeneration
+ }
+ if e.rootfsWorkers[wk.name] != wk {
+ return errSwapSuperseded
+ }
+ if a.releaseOps[e.host] != nil {
+ return errReleaseOpen
+ }
+ return workerStrayRuleLocked(e, wk.gen, wk.name)
+}
+
// workerStrayRuleLocked refuses a start of worker name of generation gen
// while any stray of the entry is of another generation or has that name
// [pf W1.14]. The caller holds a.mu.
@@ -329,7 +386,13 @@ func (a *Service) runWorkerStart(ctx context.Context, wb RootfsWorkerBackend, e
wk.startFailed = true
wk.retryAt = now.Add(max(wk.backoff, sidecarRestartBase))
default:
+ // A launch that failed, or ran and was stopped: one crash,
+ // noted here, exactly once, so its backoff, count and events
+ // (C10) advance before any stray's replacement takes its pacing
+ // (Review Focus 3). started is cleared first: a failed start
+ // is no healthy run.
wk.started, wk.retryAt, wk.startFailed = time.Time{}, time.Time{}, true
+ a.noteWorkerExitLocked(e, wk, now)
}
switch {
case stray:
@@ -557,7 +620,7 @@ func workerExitStatus(p Proc) (int, bool) {
// close its listeners; (ii) RootfsWorkerGone, so the old invocation is
// proven gone before a new one starts [pf W1.17] (unproven, it becomes a
// stray and nothing starts beside it); (iii) to (v) runWorkerStart, on a
-// context bounded by DeadlineControl and the Service's lifetime, through
+// context bounded by workerStartBound and the Service's lifetime, through
// workerGate, installing only on the same entry still awake. The caller
// holds a.mu.
func (a *Service) restartRootfsWorkerLocked(e *actEntry, wk *rootfsWorker) {
@@ -579,8 +642,25 @@ func (a *Service) restartRootfsWorkerLocked(e *actEntry, wk *rootfsWorker) {
go func() {
if needProof {
if err := a.proveWorkerGone(wb, rt, wk.name, handles...); err != nil {
+ // Not proven, and no restart beside it. A proof retry is
+ // no crash. wk's own last handle, if it has one, is a
+ // distinct launch that may run: it becomes a stray, once.
+ // Otherwise wk is a stray's placeholder: the strays are
+ // kept as they are (their next try paced from now) and wk
+ // is asked again after its backoff, so failed proofs never
+ // add strays.
a.mu.Lock()
- a.strayWorkerLocked(e, wk, a.now(), nil)
+ now := a.now()
+ if wk.proc != nil {
+ a.strayWorkerLocked(e, wk, now, nil)
+ } else {
+ for _, s := range e.strayRootfsWorkers {
+ if s.name == wk.name {
+ s.strayAt = now
+ }
+ }
+ wk.retryAt = now.Add(max(wk.backoff, sidecarRestartBase))
+ }
wk.starting = false
close(wk.startDone)
a.mu.Unlock()
@@ -603,7 +683,7 @@ func (a *Service) restartRootfsWorkerLocked(e *actEntry, wk *rootfsWorker) {
}
a.mu.Unlock()
}
- ctx, cancel := context.WithTimeout(a.life(), instancectl.DeadlineControl)
+ ctx, cancel := context.WithTimeout(a.life(), workerStartBound)
defer cancel()
a.runWorkerStart(ctx, wb, e, rt, wk, g, web, a.workerGate(e, wk))
}()
@@ -745,33 +825,47 @@ func workerNamesLocked(e *actEntry) []string {
return names
}
-// stopRootfsWorkers is a stop's first step [pf W1.8, W1.9]: once every
-// start in flight has finished, every worker of the entry (and every
-// stray) is stopped and proven gone, in parallel; an unproven one is a
+// stopRootfsWorkers is a stop's first step [pf W1.8, W1.9]: every worker
+// of the entry (and every stray) is stopped and proven gone, in parallel,
+// a starting one once its start has ended; an unproven one is a
// stray, which keeps the lease (releasable). The state is stopping by now,
// so no new start passes its gate or installs. It reports whether none is
// left. Never call it with a.mu held.
func (a *Service) stopRootfsWorkers(e *actEntry) bool {
- for {
- a.mu.Lock()
- var wait chan struct{}
- for _, wk := range e.rootfsWorkers {
- if wk.starting {
- wait = wk.startDone
- break
- }
- }
- a.mu.Unlock()
- if wait == nil {
- break
+ // Every worker not starting is stopped and proven at once, in
+ // parallel; a worker whose start is in flight is proven once that
+ // 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.
+ a.mu.Lock()
+ var waits []chan struct{}
+ var starting, now []string
+ for _, n := range workerNamesLocked(e) {
+ if wk := e.rootfsWorkers[n]; wk != nil && wk.starting {
+ waits = append(waits, wk.startDone)
+ starting = append(starting, n)
+ continue
}
- <-wait
+ now = append(now, n)
+ }
+ mine, busy := claimWorkerNamesLocked(e, now)
+ a.mu.Unlock()
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ a.proveWorkerNames(e, mine)
+ }()
+ for _, w := range waits {
+ <-w
}
a.mu.Lock()
- mine, busy := claimWorkerNamesLocked(e, workerNamesLocked(e))
+ later, busyLater := claimWorkerNamesLocked(e, starting)
a.mu.Unlock()
- a.proveWorkerNames(e, mine)
- for _, ch := range busy {
+ a.proveWorkerNames(e, later)
+ wg.Wait()
+ for _, ch := range append(busy, busyLater...) {
<-ch
}
a.mu.Lock()
@@ -784,8 +878,9 @@ func (a *Service) stopRootfsWorkers(e *actEntry) bool {
// the entry's stop: each worker not mid-start is stopped and proven gone
// in parallel, off a.mu; an unproven one is a stray, and the stop path
// still proves it before the lease goes. A start in flight proves what it
-// started itself (its install is refused). No kill: root refuses one for
-// a worker.
+// started itself (its install is refused): its gate admitted it with
+// workerStartLead left, so it ends on workerStartBound and its proof fits
+// before the safety deadline (C7). No kill: root refuses one for a worker.
func (a *Service) stopRootfsWorkersAtLoss(e *actEntry) {
a.mu.Lock()
var names []string
diff --git a/internal/activator/rootfsworkers_test.go b/internal/activator/rootfsworkers_test.go
index 942090974..29e16e4d5 100644
--- a/internal/activator/rootfsworkers_test.go
+++ b/internal/activator/rootfsworkers_test.go
@@ -109,6 +109,19 @@ type fakeWorkerBackend struct {
// launches counts LaunchRootfsWorker calls per name: each is a
// preparation (prepareWorker binds the worker's log socket), gate or no.
launches map[string]int
+ // hang, when set, makes the next start that passed its gate wait for
+ // root's answer until its context ends (a start response that never
+ // comes in time), then return its handle with the context's error;
+ // it is cleared by that start. Its return is logged "returned <name>".
+ hang bool
+ // failStarts makes that many next starts that passed their gate return
+ // their handle with an error (a 502: the start may have run).
+ failStarts int
+ // goneDelay is how long each RootfsWorkerGone takes (the worker's
+ // drain); a proof that succeeds is logged "proven <name>" when it
+ // returns, at provenAt.
+ goneDelay time.Duration
+ provenAt map[string][]time.Time
}
func (b *fakeWorkerBackend) LaunchRootfsWorker(ctx context.Context, _ registry.Route, name string, g Gen, _ Proc) (Proc, error) {
@@ -138,13 +151,24 @@ func (b *fakeWorkerBackend) LaunchRootfsWorker(ctx context.Context, _ registry.R
p := &fakeWorkerProc{name: name}
b.wmu.Lock()
b.wprocs = append(b.wprocs, p)
- uncertain := b.uncertain
- b.uncertain = false
+ uncertain, hang, fail := b.uncertain, b.hang, b.failStarts > 0
+ b.uncertain, b.hang = false, false
+ if fail {
+ b.failStarts--
+ }
b.wmu.Unlock()
if uncertain {
p.lingers = true
return p, errors.New("instance control: 502 Bad Gateway")
}
+ if hang {
+ <-ctx.Done()
+ b.record("returned " + name)
+ return p, fmt.Errorf("instance control: start %s: %w", name, ctx.Err())
+ }
+ if fail {
+ return p, errors.New("instance control: 502 Bad Gateway")
+ }
return p, nil
}
@@ -157,15 +181,23 @@ func (b *fakeWorkerBackend) launchCount(name string) int {
func (b *fakeWorkerBackend) RootfsWorkerGone(_ context.Context, _ registry.Route, name string) error {
b.record("gone " + name)
b.wmu.Lock()
- f := b.goneErr
+ f, delay := b.goneErr, b.goneDelay
b.wmu.Unlock()
+ if delay > 0 {
+ time.Sleep(delay)
+ }
if f != nil {
if err := f(name); err != nil {
return err
}
}
+ b.record("proven " + name)
b.wmu.Lock()
defer b.wmu.Unlock()
+ if b.provenAt == nil {
+ b.provenAt = map[string][]time.Time{}
+ }
+ b.provenAt[name] = append(b.provenAt[name], time.Now())
for _, p := range b.wprocs {
if p.name == name {
p.mu.Lock()
@@ -850,6 +882,17 @@ func TestRootfsWorkerStrayNamedAtShutdown(t *testing.T) {
// residentWorkerRig is a workerRig over an awake resident whose replica
// carries the lease's signal (its lease watch runs), with jobs and mailer.
func residentWorkerRig(t *testing.T) (*workerRig, func() *ownedRep) {
+ t.Helper()
+ r, rep := residentWorkerRigUntil(t, time.Hour)
+ if n := len(r.b.workers("jobs")) + len(r.b.workers("mailer")); n != 2 {
+ t.Fatalf("%d workers started, want 2", n)
+ }
+ return r, rep
+}
+
+// residentWorkerRigUntil is residentWorkerRig whose replica's safety
+// deadline is safe after its creation (on the real clock, as the lease's).
+func residentWorkerRigUntil(t *testing.T, safe time.Duration) (*workerRig, func() *ownedRep) {
t.Helper()
shrinkResidentPoll(t, time.Hour) // the test ticks by hand
rt := residentRoute("shop.example.com")
@@ -863,7 +906,7 @@ func residentWorkerRig(t *testing.T) (*workerRig, func() *ownedRep) {
fr.setOnClose(func() { kb.record("close") })
mu.Lock()
defer mu.Unlock()
- rep = newOwnedRep(fr, time.Now().Add(time.Hour))
+ rep = newOwnedRep(fr, time.Now().Add(safe))
return rep
}
kb.mu.Unlock()
@@ -875,9 +918,6 @@ func residentWorkerRig(t *testing.T) (*workerRig, func() *ownedRep) {
}
})
startResident(t, r.a, r.host)
- if n := len(r.b.workers("jobs")) + len(r.b.workers("mailer")); n != 2 {
- t.Fatalf("%d workers started, want 2", n)
- }
return r, func() *ownedRep {
mu.Lock()
defer mu.Unlock()
@@ -1571,6 +1611,282 @@ func TestRootfsWorkerInstallNeedsTheLeaseWatch(t *testing.T) {
}
}
+// shrinkWorkerBounds shrinks a worker start's bound and its proof's bound,
+// and so workerStartLead. Not parallel: they are package vars.
+func shrinkWorkerBounds(t *testing.T, start, proof time.Duration) {
+ t.Helper()
+ oldStart, oldProof := workerStartBound, workerProofBound
+ workerStartBound, workerProofBound = start, proof
+ t.Cleanup(func() { workerStartBound, workerProofBound = oldStart, oldProof })
+}
+
+// sleepUntil waits on the real clock, as the lease runs.
+func sleepUntil(at time.Time) {
+ if d := time.Until(at); d > 0 {
+ time.Sleep(d)
+ }
+}
+
+// [C7, Astra W1 r1 P1] Every worker is stopped and proven before the
+// lease's safety deadline, a worker whose start is in flight included, and
+// no worker start is admitted without the headroom to start, stop and
+// prove it first (workerStartLead, read from the lease's SafeUntil as a
+// release's is). On the real clock: lead 1.6 s (start 1 s, proof 600 ms),
+// each worker's drain 200 ms.
+func TestRootfsWorkerStartsFitBeforeTheLeaseDeadline(t *testing.T) {
+ shrinkWorkerBounds(t, time.Second, 600*time.Millisecond)
+
+ // A restart admitted with 2 s left, whose response never comes in
+ // time: it waits out its 1 s bound, past the moment the lease's drain
+ // begins (1.3 s before the deadline), and the lease then expires. The
+ // other worker is proven at once, not behind the start; the started
+ // one once its start ends; all before the deadline.
+ t.Run("a delayed start in flight at the drain", func(t *testing.T) {
+ shrinkDrainBudget(t, 1300*time.Millisecond)
+ r, rep := residentWorkerRigUntil(t, 3*time.Second)
+ until, err := rep().SafeUntil()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if n := len(r.b.workers("jobs")) + len(r.b.workers("mailer")); n != 2 {
+ t.Fatalf("%d workers started, want 2", n)
+ }
+ r.b.wmu.Lock()
+ r.b.goneDelay = 200 * time.Millisecond
+ r.b.wmu.Unlock()
+ r.b.lastWorker(t, "jobs").exit("exited", "1")
+ r.tick(t)
+ r.clk.advance(sidecarRestartBase)
+ sleepUntil(until.Add(-2 * time.Second))
+ r.b.wmu.Lock()
+ r.b.hang = true
+ r.b.wmu.Unlock()
+ r.a.rootfsTick()
+ waitFor(t, "the restart to reach root", func() bool { return len(r.b.workers("jobs")) == 2 })
+ waitFor(t, "the stop to finish", func() bool { return !r.a.hasEntry(r.host) })
+ ev := r.b.events()
+ returned := index(ev, "returned jobs", 0)
+ if mailer := index(ev, "proven mailer", 0); returned < 0 || mailer < 0 || mailer > returned {
+ t.Fatalf("order %v: mailer's proof waited behind jobs' start in flight", ev)
+ }
+ if index(ev, "proven jobs", returned) < 0 {
+ t.Fatalf("order %v: the started worker was not proven after its start ended", ev)
+ }
+ r.b.wmu.Lock()
+ defer r.b.wmu.Unlock()
+ for name, ats := range r.b.provenAt {
+ if last := ats[len(ats)-1]; !last.Before(until) {
+ t.Errorf("worker %s proven %s after the lease's safety deadline", name, last.Sub(until))
+ }
+ }
+ for _, p := range r.b.wprocs {
+ if !p.Exited() {
+ t.Errorf("worker %s runs after the stop", p.name)
+ }
+ }
+ })
+
+ // A restart due with 1.4 s left (less than the 1.6 s lead) is not
+ // admitted: root is asked nothing, and the drain proves the rest before
+ // the deadline.
+ t.Run("a restart without the headroom", func(t *testing.T) {
+ shrinkDrainBudget(t, 1300*time.Millisecond)
+ r, rep := residentWorkerRigUntil(t, 3*time.Second)
+ until, err := rep().SafeUntil()
+ if err != nil {
+ t.Fatal(err)
+ }
+ r.b.lastWorker(t, "jobs").exit("exited", "1")
+ r.tick(t)
+ r.clk.advance(sidecarRestartBase)
+ sleepUntil(until.Add(-1400 * time.Millisecond))
+ r.a.rootfsTick()
+ waitFor(t, "the restart to finish", r.settled)
+ if n := len(r.b.workers("jobs")); n != 1 {
+ t.Fatalf("root was asked for %d worker starts with 1.4 s left of a 1.6 s lead", n-1)
+ }
+ waitFor(t, "the stop to finish", func() bool { return !r.a.hasEntry(r.host) })
+ r.b.wmu.Lock()
+ defer r.b.wmu.Unlock()
+ if ats := r.b.provenAt["mailer"]; len(ats) == 0 || !ats[len(ats)-1].Before(until) {
+ t.Errorf("mailer proven at %v, deadline %v", ats, until)
+ }
+ })
+
+ // The doWake start is bounded the same way: a start whose response
+ // never comes ends on workerStartBound, the wake completes, web serves
+ // and the worker is left for the supervisor, proven gone.
+ t.Run("the wake's start is bounded", func(t *testing.T) {
+ r := newWorkerRig(t, workerHost(), "jobs")
+ r.b.wmu.Lock()
+ r.b.hang = true
+ r.b.wmu.Unlock()
+ woke := make(chan struct{})
+ go func() {
+ defer close(woke)
+ if release, err := r.a.beginRequest(context.Background(), r.rt); err == nil {
+ release()
+ }
+ }()
+ select {
+ case <-woke:
+ case <-time.After(4 * time.Second):
+ t.Fatal("the wake waited past its worker start's bound")
+ }
+ if !entryAwake(r.a, r.host) {
+ t.Fatal("web does not serve after its worker's start timed out")
+ }
+ ev := r.b.events()
+ if index(ev, "proven jobs", index(ev, "returned jobs", 0)) < 0 {
+ t.Fatalf("order %v: the timed-out start was not proven gone", ev)
+ }
+ })
+
+ // The doWake start asks the same: a wake whose lease has less than the
+ // lead left starts no worker, and web serves.
+ t.Run("the wake's start without the headroom", func(t *testing.T) {
+ shrinkDrainBudget(t, 100*time.Millisecond)
+ r, rep := residentWorkerRigUntil(t, 1200*time.Millisecond)
+ if n := len(r.b.workers("jobs")) + len(r.b.workers("mailer")); n != 0 {
+ t.Fatalf("root was asked for %d worker starts with less than the lead left", n)
+ }
+ // The lease's drain then stops it (and the watch reads its budget
+ // for the last time before that, under a.mu's order).
+ until, err := rep().SafeUntil()
+ if err == nil {
+ sleepUntil(until.Add(-50 * time.Millisecond))
+ }
+ waitFor(t, "the drain's stop", func() bool { return !r.a.hasEntry(r.host) })
+ })
+}
+
+// [Astra W1 r1 P2] A failed proof before a restart never adds a stray:
+// the first one makes the exited worker's own handle a stray, once, and
+// every later failure keeps that one stray and reschedules, counting no
+// crash.
+func TestRootfsWorkerFailedProofsKeepOneStray(t *testing.T) {
+ r := newWorkerRig(t, workerHost(), "jobs")
+ r.wake(t)
+ r.b.lastWorker(t, "jobs").exit("exited", "1")
+ r.tick(t)
+ r.b.setGone(func(string) error { return instancectl.ErrGenUnproven })
+ // Each round is past the restart's 2 s backoff and inside the stray's
+ // actStopTimeout, so every failed proof is the restart's own.
+ for i := 0; i < 8; i++ {
+ r.clk.advance(3 * time.Second)
+ r.tick(t)
+ if got := r.strays(); len(got) != 1 {
+ t.Fatalf("after %d failed proofs: strays %v, want the one", i+1, got)
+ }
+ }
+ if n := r.b.launchCount("jobs"); n != 1 {
+ t.Fatalf("%d starts beside an unproven worker", n-1)
+ }
+ if got := r.a.RootfsWorkers(r.host); len(got) != 1 || got[0].Restarts != 1 {
+ t.Fatalf("report %+v: proof retries counted as crashes", got)
+ }
+ r.b.setGone(nil)
+ for i := 0; i < 3 && r.b.launchCount("jobs") == 1; i++ {
+ r.clk.advance(sidecarRestartMax)
+ r.tick(t)
+ }
+ if n := r.b.launchCount("jobs"); n != 2 || len(r.strays()) != 0 {
+ t.Fatalf("after the proof: %d starts, strays %v; want 1 and none", n-1, r.strays())
+ }
+}
+
+// [Astra W1 r1 P2] Each launch that fails (not a refusal) is one crash:
+// the backoff doubles and the count rises once per failure, C10 is
+// reached, whether its proof is given at once or the failure leaves a
+// stray, and the stray's proof retries count no more.
+func TestRootfsWorkerFailedStartsCountOnce(t *testing.T) {
+ // restartAfter ticks second by second until jobs is asked again and
+ // answers the wait.
+ restartAfter := func(t *testing.T, r *workerRig) time.Duration {
+ t.Helper()
+ from, n := r.clk.now(), r.b.launchCount("jobs")
+ for r.b.launchCount("jobs") == n {
+ if r.clk.now().Sub(from) > 3*time.Minute {
+ t.Fatal("no restart within 3m")
+ }
+ r.clk.advance(time.Second)
+ r.tick(t)
+ }
+ return r.clk.now().Sub(from)
+ }
+ t.Run("proven at once", func(t *testing.T) {
+ r := newWorkerRig(t, workerHost(), "jobs")
+ r.wake(t)
+ r.b.wmu.Lock()
+ r.b.failStarts = 6
+ r.b.wmu.Unlock()
+ r.b.lastWorker(t, "jobs").exit("exited", "1")
+ r.tick(t) // the exit: one crash, 2 s
+ for i, want := range []time.Duration{2, 4, 8, 16, 32, 60} {
+ if got := restartAfter(t, r); got != want*time.Second {
+ t.Errorf("start %d: asked after %s, want %s", i+1, got, want*time.Second)
+ }
+ if got := r.a.RootfsWorkers(r.host); len(got) != 1 || got[0].Restarts != i+2 {
+ t.Errorf("after failed start %d: report %+v, want %d restarts", i+1, got, i+2)
+ }
+ }
+ if n := len(r.sink.with(fmt.Sprintf(rolloutwords.EventWorkerCrashLoop, "jobs"))); n != 1 {
+ t.Errorf("RFS-3-C10 emitted %d times, want once", n)
+ }
+ if len(r.strays()) != 0 {
+ t.Errorf("strays %v for failures proven at once", r.strays())
+ }
+ })
+ t.Run("left as a stray", func(t *testing.T) {
+ r := newWorkerRig(t, workerHost(), "jobs")
+ r.wake(t)
+ r.b.lastWorker(t, "jobs").exit("exited", "1")
+ r.tick(t)
+ // The restart's own proof of the exited handle passes; its start
+ // fails with a handle whose proof does not, and stays failing.
+ var calls atomic.Int32
+ r.b.setGone(func(string) error {
+ if calls.Add(1) >= 2 {
+ return instancectl.ErrGenUnproven
+ }
+ return nil
+ })
+ r.b.wmu.Lock()
+ r.b.failStarts = 1
+ r.b.wmu.Unlock()
+ if got := restartAfter(t, r); got != 2*time.Second {
+ t.Errorf("first restart after %s, want 2s", got)
+ }
+ if got := r.a.RootfsWorkers(r.host); len(got) != 1 || got[0].Restarts != 2 {
+ t.Fatalf("after the failed start: report %+v, want 2 restarts", got)
+ }
+ // The restart's own proofs fail (each round past its backoff,
+ // inside the stray's actStopTimeout), then the tick's re-proofs.
+ for i := 0; i < 4; i++ {
+ r.clk.advance(5 * time.Second)
+ r.tick(t)
+ }
+ for i := 0; i < 2; i++ {
+ r.clk.advance(sidecarRestartMax)
+ r.tick(t)
+ }
+ if got := r.a.RootfsWorkers(r.host); len(got) != 1 || got[0].Restarts != 2 {
+ t.Fatalf("proof retries counted as crashes: report %+v", got)
+ }
+ if got := r.strays(); len(got) != 1 {
+ t.Fatalf("strays %v, want the one failed start", got)
+ }
+ // Proven: the next start comes after the backoff the failure set
+ // (4 s), not the first exit's.
+ r.b.setGone(nil)
+ r.clk.advance(sidecarRestartMax)
+ r.tick(t) // the stray's proof
+ if got := restartAfter(t, r); got > 4*time.Second {
+ t.Errorf("restart after the proof took %s", got)
+ }
+ })
+}
+
// 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) {