activator: refuse held legs on worker entries (#432)
Human
Prompt
Task 1 re-review P1: an awake worker passed heldAdmissibleLocked, and respawnWorkerLocked stops its current child without cancelling legs. Ruling: refuse workers up front, keep sidecar admission unchanged.
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
heldAdmissibleLocked is now passThroughAdmissibleLocked plus !e.worker. The shared loop admits on passThroughAdmissibleLocked, so BeginSidecarRequest is unchanged for workers; AdmitHeldLeg's admit step re-checks heldAdmissibleLocked under the same a.mu and refuses a worker with ErrHeldAsleep. New TestHeldLegsRefuseWorkers covers HeldAdmissible, AdmitHeldLeg (no leg registered) and BeginSidecarRequest on a worker.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+56
−6
internal/activator/heldlegs.go+27 −6internal/activator/heldlegs_test.go+29 −0
diff --git a/internal/activator/heldlegs.go b/internal/activator/heldlegs.go| index c510a2c8b..ebc8e9985 100644 |
| --- a/internal/activator/heldlegs.go |
| +++ b/internal/activator/heldlegs.go |
| @@ -60,6 +60,13 @@ var _ edge.HeldLeg = (*heldLeg)(nil) |
| func (a *Service) AdmitHeldLeg(ctx context.Context, rt registry.Route) (edge.HeldLeg, error) { |
| var leg *heldLeg |
| err := a.admitPassThrough(ctx, rt, func(e *actEntry) error { |
| + // The loop admits on passThroughAdmissibleLocked; a held leg needs |
| + // the whole predicate, evaluated here under the same a.mu, so |
| + // AdmitHeldLeg admits exactly when heldAdmissibleLocked holds. The |
| + // only difference, a worker, refuses as asleep. |
| + if !a.heldAdmissibleLocked(e) { |
| + return ErrHeldAsleep |
| + } |
| // awaitStartup answers nil without looking at ctx when there is no |
| // startup gate; a leg is never registered on a context already done. |
| if err := ctx.Err(); err != nil { |
| @@ -104,11 +111,25 @@ func (a *Service) noteHeldAdmissible(host string) { |
| // heldAdmissibleLocked is the one admissibility predicate (spec §3.5): |
| // startup recovery has finished, e is present and awake, its child alive, |
| -// its lease serving, the host unfenced, not probe-held, and the service not |
| -// shutting down. AdmitHeldLeg (and BeginSidecarRequest) admit exactly when |
| -// it is true; the stop a lost lease or a dead child starts is theirs, not |
| -// this predicate's. The caller holds a.mu. |
| +// its lease serving, the host unfenced, not probe-held, the service not |
| +// shutting down, and e is not a worker. AdmitHeldLeg admits exactly when |
| +// it is true; the stop a lost lease or a dead child starts is |
| +// admitPassThrough's, not this predicate's. The caller holds a.mu. |
| +// |
| +// A worker is refused up front: it has no socket, the edge never proxies |
| +// to one, and its supervised respawn (respawnWorkerLocked) stops its |
| +// current child without beginStopLocked, so no leg may be registered on |
| +// it. The refusal is the held side's alone: BeginSidecarRequest admits on |
| +// passThroughAdmissibleLocked, which says nothing about workers, as it did |
| +// before held legs. |
| func (a *Service) heldAdmissibleLocked(e *actEntry) bool { |
| + return a.passThroughAdmissibleLocked(e) && !e.worker |
| +} |
| + |
| +// passThroughAdmissibleLocked is the admission shared by sidecar-marked |
| +// requests and held legs (admitPassThrough): everything in |
| +// heldAdmissibleLocked except the worker refusal. The caller holds a.mu. |
| +func (a *Service) passThroughAdmissibleLocked(e *actEntry) bool { |
| if e == nil || !a.startupDone() || a.shuttingDown { |
| return false |
| } |
| @@ -186,7 +207,7 @@ func (a *Service) cancelHeldLegsLocked(e *actEntry, gen uint64) { |
| // admitPassThrough is the admission loop for traffic that passes through |
| // to an awake instance without registering activity: sidecar-marked |
| // requests (BeginSidecarRequest) and held legs (AdmitHeldLeg). It admits |
| -// exactly when heldAdmissibleLocked holds, calling onAdmit (when non-nil) |
| +// exactly when passThroughAdmissibleLocked holds, calling onAdmit (when non-nil) |
| // under a.mu before it unlocks; onAdmit's error refuses. Otherwise a lost |
| // lease or a dead child starts a stop and the loop decides again (it then |
| // finds a stopping or absent entry and refuses rather than chaining into a |
| @@ -201,7 +222,7 @@ func (a *Service) admitPassThrough(ctx context.Context, rt registry.Route, onAdm |
| for { |
| a.mu.Lock() |
| e := a.entries[rt.Host] |
| - if a.heldAdmissibleLocked(e) { |
| + if a.passThroughAdmissibleLocked(e) { |
| var err error |
| if onAdmit != nil { |
| err = onAdmit(e) |
diff --git a/internal/activator/heldlegs_test.go b/internal/activator/heldlegs_test.go| index c610c364e..6ed23d2cd 100644 |
| --- a/internal/activator/heldlegs_test.go |
| +++ b/internal/activator/heldlegs_test.go |
| @@ -503,3 +503,32 @@ func TestClaimProducerStopCancelsHeldLegsFirst(t *testing.T) { |
| t.Fatalf("AdmitHeldLeg during the producer's stop = %v, want ErrHeldAsleep", admitErr) |
| } |
| } |
| + |
| +// A worker is never admissible for held legs: it has no socket, and its |
| +// supervised respawn stops its current child outside beginStopLocked. The |
| +// refusal is the held side's only; a sidecar-marked request to the same |
| +// awake worker is admitted as before. |
| +func TestHeldLegsRefuseWorkers(t *testing.T) { |
| + t.Parallel() |
| + a, _, _, _ := testActivator(t, time.Hour) |
| + rt := testRoute("worker.example.com") |
| + arrangeAwake(t, a, rt) |
| + a.mu.Lock() |
| + e := a.entries[rt.Host] |
| + e.worker = true // an awake worker entry, as WakeWorker leaves one |
| + a.mu.Unlock() |
| + |
| + if a.HeldAdmissible(rt.Host) { |
| + t.Fatal("HeldAdmissible true for an awake worker") |
| + } |
| + leg, err := a.AdmitHeldLeg(t.Context(), rt) |
| + if !errors.Is(err, ErrHeldAsleep) || leg != nil { |
| + t.Fatalf("AdmitHeldLeg on a worker = %v, %v; want ErrHeldAsleep", leg, err) |
| + } |
| + if n := heldLegCount(a, rt.Host); n != 0 { |
| + t.Fatalf("registered legs on a worker = %d, want 0", n) |
| + } |
| + if err := a.BeginSidecarRequest(t.Context(), rt); err != nil { |
| + t.Fatalf("BeginSidecarRequest on an awake worker = %v, want nil (unchanged)", err) |
| + } |
| +} |