carlos / platform Public

activator: cancel held legs before a claim producer's stop (#432)

Human

Prompt

Task 1 review round 1 (Astra). P1: bindProducer's cleanup stops
a claim process directly on producer cancellation (lease loss), before
any held-leg cancellation point runs. P2: TestHeldWakeHookFires asserted
the wake hook before waiting for it.

From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.

Summary

bindClaimProducer gives the producer's cleanup a pre-stop step,
refuseHeldLegsOn: under a.mu it marks the claim process as refusing held
legs and cancels every leg on the entry it serves, before the first Stop.
heldAdmissibleLocked refuses a process so marked; the shared loop then
answers ErrHeldAsleep (errStartupStopped only if the service lifetime
ended). Replication and lease handling are unchanged. New witness
TestClaimProducerStopCancelsHeldLegsFirst checks the leg is cancelled and
admission refused when Stop is entered. The hook test now waits, bounded,
for the wake's hook call.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev f2a684b71fbd598b7a8a4ad2258832ec925e8288 parent 991e7b4
4 files changed, +143 −6
  • internal/activator/claimproducer_test.go +2 −2
  • internal/activator/claimwake.go +24 −2
  • internal/activator/heldlegs.go +31 −2
  • internal/activator/heldlegs_test.go +86 −0
diff --git a/internal/activator/claimproducer_test.go b/internal/activator/claimproducer_test.go
index ac788b7c9..1b8af76d9 100644
--- a/internal/activator/claimproducer_test.go
+++ b/internal/activator/claimproducer_test.go
@@ -36,7 +36,7 @@ func TestClaimProducerCancellationStopsAndJoinsProcess(t *testing.T) {
p.settle = func(context.Context) error { settled = true; return nil }
ctx, cancel := context.WithCancel(t.Context())
finished := make(chan error, 1)
- p.bindProducer(RoleProducer{Context: ctx, Finish: func(err error) { finished <- err }})
+ p.bindProducer(RoleProducer{Context: ctx, Finish: func(err error) { finished <- err }}, nil)
cancel()
select {
case err := <-finished:
@@ -75,7 +75,7 @@ func TestClaimProducerCancellationRetriesRootSettlement(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
finished := make(chan error, 1)
- p.bindProducer(RoleProducer{Context: ctx, Finish: func(err error) { finished <- err }})
+ p.bindProducer(RoleProducer{Context: ctx, Finish: func(err error) { finished <- err }}, nil)
cancel()
select {
case err := <-finished:
diff --git a/internal/activator/claimwake.go b/internal/activator/claimwake.go
index 0b58ad636..7d687dc8f 100644
--- a/internal/activator/claimwake.go
+++ b/internal/activator/claimwake.go
@@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"sync"
+ "sync/atomic"
"syscall"
"time"
@@ -580,7 +581,7 @@ func (a *Service) startAdmitted(ctx context.Context, e *actEntry, admitted hostc
p := newClaimProcessFor(cmd, prepared, s.RoleLauncher.Root != nil)
p.settle = settled
- p.bindProducer(producer)
+ a.bindClaimProducer(e, p, producer)
if unrecorded {
// A refused record, or a held wake's refused identity, ran
// nothing of the build (ruling R-EXEC-CLAIMRETRY): no failed
@@ -736,8 +737,15 @@ type claimProcess struct {
root bool
settle func(context.Context) error
producerFinish func(error)
+ // heldRefused: the producer's cleanup is about to stop this process
+ // outside the entry's lifecycle, so it takes no held leg from here
+ // (refuseHeldLegsOn, edge-held streams spec §3.2). Written under the
+ // Service's a.mu.
+ heldRefused atomic.Bool
}
+func (p *claimProcess) refusesHeldLegs() bool { return p.heldRefused.Load() }
+
func newClaimProcess(cmd *exec.Cmd, p *cgroup.Prepared) *claimProcess {
c := &claimProcess{cmd: cmd, done: make(chan struct{})}
if p != nil {
@@ -849,7 +857,18 @@ func sameAdmission(a, b hostclaim.Observed) bool {
return x.Holder.Incarnation == y.Holder.Incarnation && x.Holder.Stamp == y.Holder.Stamp && x.Holder.Session == y.Holder.Session && x.Holder.Generation == y.Holder.Generation && x.Op.ID == y.Op.ID && x.Op.Kind == y.Op.Kind && x.Op.Phase == y.Op.Phase && x.Op.Exec == y.Op.Exec
}
-func (p *claimProcess) bindProducer(producer RoleProducer) {
+// bindClaimProducer binds producer to p, a claim start's process for e.
+// The producer's cleanup stops p directly, not through beginStopLocked, so
+// before its first Stop it refuses and cancels p's held legs under a.mu
+// (refuseHeldLegsOn): a held leg never outlives the start of that stop.
+func (a *Service) bindClaimProducer(e *actEntry, p *claimProcess, producer RoleProducer) {
+ p.bindProducer(producer, func() { a.refuseHeldLegsOn(e, p) })
+}
+
+// bindProducer stops p when the producer's context ends (or p exits), and
+// retries that stop on its own bound. beforeStop, when non-nil, runs once
+// before the first Stop.
+func (p *claimProcess) bindProducer(producer RoleProducer, beforeStop func()) {
if producer.Context == nil || producer.Finish == nil {
return
}
@@ -859,6 +878,9 @@ func (p *claimProcess) bindProducer(producer RoleProducer) {
case <-producer.Context.Done():
case <-p.done:
}
+ if beforeStop != nil {
+ beforeStop()
+ }
cleanup, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for {
diff --git a/internal/activator/heldlegs.go b/internal/activator/heldlegs.go
index c52cff6ec..c510a2c8b 100644
--- a/internal/activator/heldlegs.go
+++ b/internal/activator/heldlegs.go
@@ -118,9 +118,33 @@ func (a *Service) heldAdmissibleLocked(e *actEntry) bool {
if e.proc == nil || e.proc.Exited() || servingError(e) != nil {
return false
}
+ if r, ok := e.proc.(heldRefuser); ok && r.refusesHeldLegs() {
+ return false
+ }
return true
}
+// heldRefuser is a process whose stop has begun outside the entry's
+// lifecycle (a claim start's producer cleanup, bindClaimProducer): it takes
+// no held leg, though its entry may still read awake.
+type heldRefuser interface{ refusesHeldLegs() bool }
+
+// refuseHeldLegsOn is the pre-stop step for a claim process the producer's
+// cleanup stops directly: under a.mu, p takes no held leg from now on, and
+// every leg on the entry p serves is cancelled, before the first Stop.
+// Replication and the lease are untouched; the activator finds the stopped
+// process as it finds any dead child.
+func (a *Service) refuseHeldLegsOn(e *actEntry, p *claimProcess) {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ p.heldRefused.Store(true)
+ for _, x := range []*actEntry{e, a.entries[e.host]} {
+ if x != nil && x.proc == Proc(p) {
+ a.cancelHeldLegsLocked(x, 0)
+ }
+ }
+}
+
// startupDone reports, without waiting, what awaitStartup would: true when
// it would return at once without error.
func (a *Service) startupDone() bool {
@@ -219,9 +243,14 @@ func (a *Service) admitPassThrough(ctx context.Context, rt registry.Route, onAdm
continue
}
// Awake, serving, unfenced and not held, yet not admissible:
- // the service's lifetime ended after startup was awaited.
+ // the service's lifetime ended after startup was awaited, or
+ // the process's own stop has begun (heldRefuser), which reads
+ // as stopping.
a.mu.Unlock()
- return errStartupStopped
+ if !a.startupDone() {
+ return errStartupStopped
+ }
+ return errSidecarAsleep
case actWaking:
// Joining a wake someone else started is fine — the co-spawned
diff --git a/internal/activator/heldlegs_test.go b/internal/activator/heldlegs_test.go
index 68db738d7..c610c364e 100644
--- a/internal/activator/heldlegs_test.go
+++ b/internal/activator/heldlegs_test.go
@@ -356,6 +356,12 @@ func TestHeldWakeHookFires(t *testing.T) {
a.SetHeldWakeHook(hook)
rt := testRoute("shop.example.com")
arrangeAwake(t, a, rt)
+ // The wake closes e.ready before it calls the hook, so the request
+ // that woke the host can return first: wait, bounded, for the call.
+ waitFor(t, "the wake's hook call", func() bool { return len(seen()) > 0 })
+ if h := seen()[0]; h != rt.Host {
+ t.Fatalf("wake: hook saw %q first, want %q", h, rt.Host)
+ }
if n := count(rt.Host); n != 1 {
t.Fatalf("wake: hook saw %s %d times (%v), want 1", rt.Host, n, seen())
}
@@ -417,3 +423,83 @@ func TestHeldWakeHookFires(t *testing.T) {
t.Fatalf("a removed hook fired: %v", seen())
}
}
+
+// stopEntryContainment is a claim process's containment that records, at
+// the moment its Stop is entered (KillAndEmpty is Stop's first act on a
+// contained process), what a held leg and the admission saw.
+type stopEntryContainment struct {
+ enter func()
+ done chan struct{}
+}
+
+func (c *stopEntryContainment) KillAndEmpty(context.Context) error {
+ c.enter()
+ close(c.done)
+ return nil
+}
+func (c *stopEntryContainment) Close() error { return nil }
+
+// A claim start's producer cleanup (bindProducer) stops its process
+// directly, outside beginStopLocked: on a lease loss the host cancels the
+// producer's context first. The held leg on that process is cancelled, and
+// the process refuses new legs, before that Stop is entered.
+func TestClaimProducerStopCancelsHeldLegsFirst(t *testing.T) {
+ t.Parallel()
+ a, _, _, _ := testActivator(t, time.Hour)
+ rt := testRoute("shop.example.com")
+ arrangeAwake(t, a, rt)
+ p := &claimProcess{done: make(chan struct{})}
+ a.mu.Lock()
+ e := a.entries[rt.Host]
+ a.installProcLocked(e, nil, p, e.gen)
+ a.mu.Unlock()
+
+ leg, err := a.AdmitHeldLeg(t.Context(), rt)
+ if err != nil {
+ t.Fatalf("AdmitHeldLeg on the claim process: %v", err)
+ }
+ defer leg.Done()
+
+ var (
+ mu sync.Mutex
+ atStop error
+ admissible bool
+ admitErr error
+ entered bool
+ )
+ p.prepared = &stopEntryContainment{done: p.done, enter: func() {
+ ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
+ defer cancel()
+ late, lerr := a.AdmitHeldLeg(ctx, rt)
+ if late != nil {
+ late.Done()
+ }
+ mu.Lock()
+ atStop, entered = leg.Context().Err(), true
+ admissible, admitErr = a.HeldAdmissible(rt.Host), lerr
+ mu.Unlock()
+ }}
+ ctx, cancel := context.WithCancel(t.Context())
+ finished := make(chan error, 1)
+ a.bindClaimProducer(e, p, RoleProducer{Context: ctx, Finish: func(err error) { finished <- err }})
+ cancel() // the lease loss: the producer's parent context ends
+ select {
+ case <-finished:
+ case <-time.After(5 * time.Second):
+ t.Fatal("the producer's cleanup never stopped the process")
+ }
+ mu.Lock()
+ defer mu.Unlock()
+ if !entered {
+ t.Fatal("Stop never reached the containment")
+ }
+ if !errors.Is(atStop, context.Canceled) {
+ t.Fatalf("leg context when Stop was entered = %v, want context.Canceled", atStop)
+ }
+ if admissible {
+ t.Fatal("HeldAdmissible true on a process whose stop had begun")
+ }
+ if !errors.Is(admitErr, ErrHeldAsleep) {
+ t.Fatalf("AdmitHeldLeg during the producer's stop = %v, want ErrHeldAsleep", admitErr)
+ }
+}