carlos / platform Public

activator: Shutdown names every abandoned checkpoint (M1 review P2)

A fleet stop's checkpoint now stays registered (checkpointBusy) until
its outcome is handled, so Shutdown's exit scan cannot miss it between
CloseProven's return and the pending state or abandonment. C45 goes
through a per-entry once (abandonNotice), and Shutdown names any
checkpoint whose replicator ended abandoned (Abandoned), by whichever
call, before it returns. Barrier witnesses hold the handling after
CloseProven returns (checkpointReturned seam).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 3b43211a89e2db13e6c2096a1e2ea3ed175507d0 parent 562113a
2 files changed, +210 −24
  • internal/activator/activator.go +75 −24
  • internal/activator/pendingwake_linux_test.go +135 −0
diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index ea0b1b0df..d558292f1 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -676,14 +676,19 @@ type actEntry struct {
// (finishPendingStop) the moment it's proven.
pendingStop bool
// The fleet checkpoint's state (fleet record labels R2), under a.mu.
- // checkpointCancel is non-nil while finishStop's CloseProven is in
- // flight, and cancels its context; checkpointUnproven marks a stop
- // kept pending over an unproven checkpoint; checkpointAbandoned marks
- // one Shutdown abandoned when its budget ended
- // (abandonCheckpointsAtExit), which no later finishStop tries again.
+ // checkpointBusy is true from finishStop's CloseProven until its
+ // outcome has been handled (closeFleetCheckpoint), and
+ // checkpointCancel cancels that call's context meanwhile;
+ // checkpointUnproven marks a stop kept pending over an unproven
+ // checkpoint; checkpointAbandoned marks one Shutdown abandoned when
+ // its budget ended (abandonCheckpointsAtExit), which no later
+ // finishStop tries again. checkpointNotice is its C45, logged once by
+ // whichever call learns of the abandonment first (not under a.mu).
+ checkpointBusy bool
checkpointCancel context.CancelFunc
checkpointUnproven bool
checkpointAbandoned bool
+ checkpointNotice abandonNotice
// stopRetrying is true while a retried proc.Stop call for a still-running
// pendingStop entry is in flight, so finishPendingStops does not launch a
// second one on top of it. stopRetriedAt is when the last retry was
@@ -1748,6 +1753,10 @@ type Service struct {
// at a chosen point. It is a test seam; nil in production
// (context.WithCancel).
checkpointContext func(context.Context) (context.Context, context.CancelFunc)
+ // checkpointReturned, when set, is called (off a.mu) once a fleet
+ // stop's CloseProven has returned, before its outcome is handled
+ // (closeFleetCheckpoint). It is a test seam; nil in production.
+ checkpointReturned func(host string, err error)
}
// New wires the state machine to its side-effect backend and due-time store.
@@ -5531,6 +5540,9 @@ func (a *Service) finishStop(e *actEntry, outcome *stopObservation) {
type provenCloser interface {
CloseProven(ctx context.Context) error
TryAbandon(ctx context.Context) bool
+ // Abandoned reports whether the teardown, by whichever call, was an
+ // abandonment: the lease frees at its TTL (lease.Replicator).
+ Abandoned() bool
}
var _ provenCloser = (*lease.Replicator)(nil)
@@ -5586,25 +5598,37 @@ func (a *Service) closeFleetCheckpoint(ctx context.Context, e *actEntry, pc prov
mk = context.WithCancel
}
cctx, cancel := mk(ctx)
- e.checkpointCancel = cancel
+ e.checkpointCancel, e.checkpointBusy = cancel, true
started := a.checkpointStarted
a.mu.Unlock()
defer cancel()
+ // The checkpoint stays registered until its outcome has been handled
+ // (M1 review Astra P2-2): Shutdown's exit (abandonCheckpointsAtExit)
+ // then never misses it, between CloseProven's return and the pending
+ // state or the abandonment below.
+ defer func() {
+ a.mu.Lock()
+ e.checkpointCancel, e.checkpointBusy = nil, false
+ a.mu.Unlock()
+ }()
if started != nil {
started(e.host)
}
err = pc.CloseProven(cctx)
a.mu.Lock()
- e.checkpointCancel = nil
shutting := a.shuttingDown
+ returned := a.checkpointReturned
a.mu.Unlock()
+ if returned != nil {
+ returned(e.host, err)
+ }
prefix := ""
if shutting {
prefix = "shutdown: "
}
switch {
case errors.Is(err, lease.ErrCheckpointAbandoned):
- logCheckpointAbandoned(prefix, e.host)
+ e.checkpointNotice.name(prefix, e.host)
case errors.Is(err, lease.ErrCheckpointUnproven):
a.mu.Lock()
shutting = a.shuttingDown
@@ -5616,33 +5640,55 @@ func (a *Service) closeFleetCheckpoint(ctx context.Context, e *actEntry, pc prov
a.mu.Unlock()
if !shutting {
log.Printf("activator: final checkpoint %s: %v", e.host, err)
- // draft for copy review (C44)
- log.Printf("activator: final checkpoint for %s failed. keeping its lease until the checkpoint succeeds", e.host)
+ logCheckpointKept(e.host)
if hook != nil {
hook(e.host)
}
return true, err
}
- if abandonCheckpoint(pc) {
- logCheckpointAbandoned("shutdown: ", e.host)
+ abandonCheckpoint(pc)
+ if pc.Abandoned() {
+ e.checkpointNotice.name("shutdown: ", e.host)
}
}
return false, err
}
+// logCheckpointKept is the line a fleet stop or failed wake kept over an
+// unproven final checkpoint logs (C44).
+func logCheckpointKept(host string) {
+ // draft for copy review (C44)
+ log.Printf("activator: final checkpoint for %s failed. keeping its lease until the checkpoint succeeds", host)
+}
+
// abandonCheckpoint gives an unproven fleet checkpoint's lease up to its
// TTL: renewal and replication stop, nothing is released, and, under a
// context already done, nothing more is synced (the checkpoint just
// failed, as a crash would ship nothing more). It reports whether this
-// call tore the replicator down, and so must name it (C45).
+// call tore the replicator down. Whoever names the abandonment (C45)
+// asks pc.Abandoned instead, which answers for any call's teardown.
func abandonCheckpoint(pc provenCloser) bool {
done, cancel := context.WithCancel(context.Background())
cancel()
return pc.TryAbandon(done)
}
+// abandonNotice is the C45 of one abandoned fleet checkpoint (M1 review
+// Astra P2-2): every call that learns its replicator was abandoned names
+// it through the notice, which logs once. A call that comes second
+// returns only once the first has logged (sync.Once), so Shutdown, which
+// names each checkpoint it abandons or finds abandoned before it
+// returns, never returns before the line is out.
+type abandonNotice struct{ once sync.Once }
+
+// name logs C45 for host with prefix, the first time it is called.
+func (n *abandonNotice) name(prefix, host string) {
+ n.once.Do(func() { logCheckpointAbandoned(prefix, host) })
+}
+
// logCheckpointAbandoned is the ERROR an abandoned fleet checkpoint logs,
-// naming the host; prefix says where ("shutdown: " or "").
+// naming the host; prefix says where ("shutdown: " or ""). It is logged
+// only through an abandonNotice.
func logCheckpointAbandoned(prefix, host string) {
// draft for copy review (C45)
log.Printf("activator: ERROR: %s%s: its final checkpoint failed, so its lease is not released and frees at its expiry", prefix, host)
@@ -5650,18 +5696,20 @@ func logCheckpointAbandoned(prefix, host string) {
// abandonCheckpointsAtExit runs as Shutdown's budget ends: no unproven
// fleet checkpoint is left renewing or unnamed when Shutdown returns.
-// Every fleet stop with a checkpoint in flight (e.checkpointCancel), or
-// kept pending over an unproven one, is marked abandoned (so no later
-// finishStop tries it again), its checkpoint's context is cancelled, and
-// its replicator abandoned here, whatever the call in flight is waiting
-// on, and named when this is the teardown. Nothing waits for the calls
-// in flight: once abandoned they hold no lease, and each finishes its
-// entry when it returns.
+// Every fleet stop with a checkpoint in flight or not yet handled
+// (e.checkpointBusy), or kept pending over an unproven one, is marked
+// abandoned (so no later finishStop tries it again), its checkpoint's
+// context is cancelled, and its replicator abandoned here, whatever the
+// call in flight is waiting on. When its teardown was an abandonment, by
+// this call or any other, it is named (C45) before this returns, once in
+// all (abandonNotice). Nothing waits for the calls in flight: once
+// abandoned they hold no lease, and each finishes its entry when it
+// returns.
func (a *Service) abandonCheckpointsAtExit() {
a.mu.Lock()
var left []*actEntry
for _, e := range a.entries {
- if e.checkpointAbandoned || (e.checkpointCancel == nil && !e.checkpointUnproven) {
+ if e.checkpointAbandoned || (!e.checkpointBusy && !e.checkpointUnproven) {
continue
}
e.checkpointAbandoned = true
@@ -5672,8 +5720,11 @@ func (a *Service) abandonCheckpointsAtExit() {
}
a.mu.Unlock()
for _, e := range left {
- if pc, ok := a.fleetProvenCloser(e); ok && abandonCheckpoint(pc) {
- logCheckpointAbandoned("shutdown: ", e.host)
+ if pc, ok := a.fleetProvenCloser(e); ok {
+ abandonCheckpoint(pc)
+ if pc.Abandoned() {
+ e.checkpointNotice.name("shutdown: ", e.host)
+ }
}
}
}
diff --git a/internal/activator/pendingwake_linux_test.go b/internal/activator/pendingwake_linux_test.go
index 8fb2d460d..cd468c772 100644
--- a/internal/activator/pendingwake_linux_test.go
+++ b/internal/activator/pendingwake_linux_test.go
@@ -1911,3 +1911,138 @@ func TestShutdownAbandonsACheckpointWhoseOwnBudgetEnded(t *testing.T) {
t.Fatalf("no C45 ERROR naming %s before Shutdown returned:\n%s", r.rt.Host, logged)
}
}
+
+// c45Text is the body of C45, the ERROR an abandoned fleet checkpoint
+// logs, whatever its prefix.
+const c45Text = "its final checkpoint failed, so its lease is not released and frees at its expiry"
+
+// holdCheckpointReturn holds the next fleet checkpoint's handling once its
+// CloseProven has returned (Service.checkpointReturned): returned receives
+// CloseProven's answer, and the handling goes on once release is called.
+// Later checkpoints are not held.
+func (r *checkpointRig) holdCheckpointReturn(t *testing.T) (returned chan error, release func()) {
+ t.Helper()
+ returned, gate := make(chan error, 1), make(chan struct{})
+ var once, closeOnce sync.Once
+ release = func() { closeOnce.Do(func() { close(gate) }) }
+ t.Cleanup(release)
+ r.a.mu.Lock()
+ r.a.checkpointReturned = func(_ string, err error) {
+ held := false
+ once.Do(func() { held = true })
+ if held {
+ returned <- err
+ <-gate
+ }
+ }
+ r.a.mu.Unlock()
+ return returned, release
+}
+
+// shutdownSpent runs Shutdown with its budget already over and waits for
+// it to return.
+func (r *checkpointRig) shutdownSpent(t *testing.T) {
+ t.Helper()
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ done := make(chan struct{})
+ go func() {
+ r.a.Shutdown(ctx)
+ close(done)
+ }()
+ select {
+ case <-done:
+ case <-time.After(30 * time.Second):
+ t.Fatal("Shutdown did not return once its budget was over")
+ }
+}
+
+// M1 review Astra P2-2 (a): a fleet stop's checkpoint stays registered
+// until its outcome is handled. Its CloseProven has answered unproven and
+// its handling is held when Shutdown runs out of budget: Shutdown must
+// still abandon it (lock.json kept, renewal stopped) and name it (C45)
+// before it returns, and the handling, once let go, names it no again.
+func TestShutdownAbandonsACheckpointBeforeItsOutcomeIsHandled(t *testing.T) {
+ r := newCheckpointRig(t, registry.PlacementTarget("pi"))
+ e := r.wake(t)
+ t.Cleanup(r.fx.FailReplicaPuts(r.prefix))
+ appendRow(t, r.rt.DBPath)
+ returned, release := r.holdCheckpointReturn(t)
+ stopped := r.stop(e)
+ select {
+ case err := <-returned:
+ if !errors.Is(err, lease.ErrCheckpointUnproven) {
+ t.Fatalf("CloseProven = %v, want ErrCheckpointUnproven", err)
+ }
+ case <-time.After(30 * time.Second):
+ t.Fatalf("the stop's checkpoint never returned:\n%s", r.log.String())
+ }
+ r.shutdownSpent(t)
+ if r.fx.LockBody(r.prefix) == nil {
+ t.Fatal("the lease was released over an unproven checkpoint")
+ }
+ r.a.mu.Lock()
+ rep := e.rep
+ r.a.mu.Unlock()
+ if err := rep.Err(); err == nil {
+ t.Fatal("the lease still renews after Shutdown returned: it was not abandoned")
+ }
+ if n := strings.Count(r.log.String(), c45Text); n != 1 {
+ t.Fatalf("C45 logged %d times before Shutdown returned, want once:\n%s", n, r.log.String())
+ }
+ release()
+ select {
+ case <-stopped:
+ case <-time.After(30 * time.Second):
+ t.Fatal("the stop never finished once its handling went on")
+ }
+ logged := r.log.String()
+ if n := strings.Count(logged, c45Text); n != 1 {
+ t.Fatalf("C45 logged %d times, want once:\n%s", n, logged)
+ }
+ if strings.Contains(logged, c44Line) {
+ t.Fatalf("C44 promised to keep a lease Shutdown gave up:\n%s", logged)
+ }
+}
+
+// M1 review Astra P2-2 (b): C45 has one owner, and Shutdown completes it.
+// CloseProven over a lost lease abandons the replicator itself (the
+// teardown claimed) and its handling, which names it, is held: Shutdown,
+// whose own abandonment finds the teardown already claimed, must still
+// see C45 logged before it returns, and only once in all.
+func TestShutdownNamesAnAbandonmentClaimedElsewhere(t *testing.T) {
+ r := newCheckpointRig(t, registry.PlacementTarget("pi"))
+ e := r.wake(t)
+ t.Cleanup(r.fx.FailReplicaPuts(r.prefix))
+ appendRow(t, r.rt.DBPath)
+ rep, ok := e.rep.(*lease.Replicator)
+ if !ok {
+ t.Fatalf("the entry's replica is %T", e.rep)
+ }
+ lockKey := r.prefix + "/lock.json"
+ r.fx.FailNextMatching("PUT", 403, func(k string, _ []byte) bool { return k == lockKey })
+ lease.LoseLeaseForTest(rep, errors.New("renewal: AccessDenied"))
+ returned, release := r.holdCheckpointReturn(t)
+ stopped := r.stop(e)
+ select {
+ case err := <-returned:
+ if !errors.Is(err, lease.ErrCheckpointAbandoned) {
+ t.Fatalf("CloseProven = %v, want ErrCheckpointAbandoned", err)
+ }
+ case <-time.After(30 * time.Second):
+ t.Fatalf("the stop's checkpoint never returned:\n%s", r.log.String())
+ }
+ r.shutdownSpent(t)
+ if n := strings.Count(r.log.String(), c45Text); n != 1 {
+ t.Fatalf("C45 logged %d times before Shutdown returned, want once:\n%s", n, r.log.String())
+ }
+ release()
+ select {
+ case <-stopped:
+ case <-time.After(30 * time.Second):
+ t.Fatal("the stop never finished once its handling went on")
+ }
+ if n := strings.Count(r.log.String(), c45Text); n != 1 {
+ t.Fatalf("C45 logged %d times, want once:\n%s", n, r.log.String())
+ }
+}