activator: deterministic startup-wait witness for held admission (#432)
Human
Prompt
Astra re-review P2: the startup-gate case of the admissibility table opened the gate straight after a non-blocking check, so it did not prove AdmitHeldLeg waited.
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
The test goroutine carries a pprof label, and the test polls the goroutine profile until that goroutine is inside awaitStartup with the gate shut, then opens the gate and asserts the leg. "Waited" now comes from that observation, not the case flag. Test-only; no production change. Mutation (skip the startup wait in admitPassThrough) fails the case.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+52
−10
internal/activator/heldlegs_test.go+52 −10
diff --git a/internal/activator/heldlegs_test.go b/internal/activator/heldlegs_test.go| index 43b0f27b4..bdda3678a 100644 |
| --- a/internal/activator/heldlegs_test.go |
| +++ b/internal/activator/heldlegs_test.go |
| @@ -3,6 +3,8 @@ package activator |
| import ( |
| "context" |
| "errors" |
| + "runtime/pprof" |
| + "strings" |
| "sync" |
| "testing" |
| "time" |
| @@ -230,21 +232,36 @@ func TestHeldAdmissibleMatchesAdmit(t *testing.T) { |
| err error |
| } |
| resC := make(chan result, 1) |
| - go func() { |
| - leg, err := a.AdmitHeldLeg(t.Context(), rt) |
| + label := pprof.Labels("heldadmit", c.name) |
| + go pprof.Do(t.Context(), label, func(ctx context.Context) { |
| + leg, err := a.AdmitHeldLeg(ctx, rt) |
| resC <- result{leg, err} |
| - }() |
| + }) |
| // Admitted without waiting: every gate this case could wait on |
| // stays shut until the call has returned, so a call that |
| // returns a leg here did not wait for one. The bound only ends |
| // a call that waits for ever; it classifies nothing. |
| var res result |
| + waited := false |
| if c.waits { |
| - // Never flaky: a call can only be early, never late, here. |
| - select { |
| - case r := <-resC: |
| - t.Fatalf("AdmitHeldLeg returned %v, %v with the startup gate shut", r.leg, r.err) |
| - default: |
| + // Observe the call parked in awaitStartup's select while |
| + // the gate is shut. It cannot return before the gate |
| + // opens, so this is reached only if it really waits; a |
| + // call that skips the wait returns and fails here. |
| + deadline := time.Now().Add(30 * time.Second) |
| + for !waited { |
| + select { |
| + case r := <-resC: |
| + t.Fatalf("AdmitHeldLeg returned %v, %v with the startup gate shut; it did not wait", r.leg, r.err) |
| + default: |
| + } |
| + waited = parkedInAwaitStartup(c.name) |
| + if !waited { |
| + if time.Now().After(deadline) { |
| + t.Fatalf("AdmitHeldLeg never parked in awaitStartup within 30s") |
| + } |
| + time.Sleep(time.Millisecond) |
| + } |
| } |
| close(gate) |
| res = <-resC |
| @@ -263,12 +280,12 @@ func TestHeldAdmissibleMatchesAdmit(t *testing.T) { |
| if res.leg != nil { |
| defer res.leg.Done() |
| } |
| - admitted := !c.waits && res.err == nil |
| + admitted := !waited && res.err == nil |
| if admissible != c.want { |
| t.Errorf("HeldAdmissible = %v, want %v", admissible, c.want) |
| } |
| if admissible != admitted { |
| - t.Errorf("HeldAdmissible = %v but AdmitHeldLeg = %v (waited for a gate: %v)", admissible, res.err, c.waits) |
| + t.Errorf("HeldAdmissible = %v but AdmitHeldLeg = %v (waited for a gate: %v)", admissible, res.err, waited) |
| } |
| if res.err != nil && res.leg != nil { |
| t.Errorf("a refused admission returned a leg") |
| @@ -277,6 +294,31 @@ func TestHeldAdmissibleMatchesAdmit(t *testing.T) { |
| } |
| } |
| +// parkedInAwaitStartup reports whether a goroutine labelled heldadmit=name |
| +// is inside awaitStartup, blocked on the shut gate, read from the goroutine profile. |
| +func parkedInAwaitStartup(name string) bool { |
| + var buf strings.Builder |
| + if err := pprof.Lookup("goroutine").WriteTo(&buf, 1); err != nil { |
| + return false |
| + } |
| + want := `# labels: {"heldadmit":"` + name + `"}` |
| + for _, block := range strings.Split(buf.String(), "\n\n") { |
| + if !strings.Contains(block, want) { |
| + continue |
| + } |
| + // The profile hides runtime frames, so a goroutine parked in the |
| + // select shows awaitStartup as its innermost frame. With the gate |
| + // shut and the context and deadline unfired, it can only leave by |
| + // the gate opening. |
| + for _, line := range strings.Split(block, "\n") { |
| + if strings.HasPrefix(strings.TrimSpace(line), "#\t") { |
| + return strings.Contains(line, "activator.(*Service).awaitStartup+") |
| + } |
| + } |
| + } |
| + return false |
| +} |
| + |
| func TestAdmitHeldLegCancelledContext(t *testing.T) { |
| t.Parallel() |
| a, _, _, _ := testActivator(t, time.Hour) |