region-ha p1b-2: holder discovery's lock read is bounded at 2 s
A store that took the connection and stalled held the visitor's request, and every request queued on the same read, with no deadline. The read now runs under holderReadTimeout, detached from the initiator's cancellation so one visitor hanging up fails no other; a timeout caches nothing and ends the flight.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+60
−2
internal/activator/haforward.go+13 −2internal/activator/haforward_test.go+47 −0
Intent
Human
<task-notification> <task-id>bqg18j1iz</task-id> <tool-use-id>toolu_011eCJCwuKYWCPHP36qvpsQb</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/953e7524-2e06-4308-b9ce-fd1b38579971/tasks/bqg18j1iz.output</output-file> <status>completed</status> <summary>Background command "Write branch review brief, push, run Astra" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/activator/haforward.go b/internal/activator/haforward.go| index b07c72ce5..4daed63d1 100644 |
| --- a/internal/activator/haforward.go |
| +++ b/internal/activator/haforward.go |
| @@ -86,6 +86,12 @@ func boxSelfRef(b Backend) (string, bool) { |
| return "", false |
| } |
| +// holderReadTimeout bounds holder discovery's one lock read, headers and |
| +// body: a store that takes the connection and stalls holds no request, nor |
| +// any request queued on the read, past it. The read then answers "serve |
| +// here" and caches nothing. |
| +const holderReadTimeout = 2 * time.Second |
| + |
| // holderReadErrLogged is when a failed lock read was last logged (unix |
| // nanos): at most one line a minute for the box, since every request of |
| // a host whose read fails reads again. |
| @@ -118,10 +124,15 @@ func (a *Service) ForwardTarget(ctx context.Context, rt registry.Route) string { |
| return "" |
| } |
| return a.holders.resolve(ctx, rt.Host, func() (string, bool) { |
| - v, err := r.readHolderView(ctx, rt) |
| + // The read is every queued caller's, not only this one's: detached |
| + // from this request's cancellation, so one visitor hanging up does |
| + // not fail the rest, and bounded on its own. |
| + rctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), holderReadTimeout) |
| + defer cancel() |
| + v, err := r.readHolderView(rctx, rt) |
| if err != nil { |
| now := time.Now() |
| - if last := holderReadErrLogged.Load(); ctx.Err() == nil && now.UnixNano()-last >= int64(time.Minute) && holderReadErrLogged.CompareAndSwap(last, now.UnixNano()) { |
| + if last := holderReadErrLogged.Load(); now.UnixNano()-last >= int64(time.Minute) && holderReadErrLogged.CompareAndSwap(last, now.UnixNano()) { |
| log.Printf("activator: %s: reading the holder: %v; serving here", rt.Host, err) |
| } |
| return "", false |
diff --git a/internal/activator/haforward_test.go b/internal/activator/haforward_test.go| index 671b1fee0..27a38dd46 100644 |
| --- a/internal/activator/haforward_test.go |
| +++ b/internal/activator/haforward_test.go |
| @@ -470,3 +470,50 @@ func TestHoppedSidecarRefusalsSayNone(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// A store that takes the connection and never answers holds no request past |
| +// holderReadTimeout: the read's initiator and every caller queued on it |
| +// return "", nothing is cached, the flight is gone, and the next lookup |
| +// reads afresh. |
| +func TestForwardTargetStalledReadIsBounded(t *testing.T) { |
| + s := haHeldByB(t) |
| + release := make(chan struct{}) |
| + stall := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + select { |
| + case <-release: |
| + case <-r.Context().Done(): |
| + } |
| + })) |
| + t.Cleanup(stall.Close) |
| + t.Cleanup(func() { close(release) }) // before stall.Close, which waits on the handlers |
| + t.Setenv("CARLOS_S3_ENDPOINT", stall.URL) |
| + start := time.Now() |
| + got := make(chan string, 10) |
| + for range cap(got) { |
| + go func() { got <- s.a.ForwardTarget(context.Background(), s.rt) }() |
| + } |
| + bound := time.After(holderReadTimeout + 2*time.Second) |
| + for range cap(got) { |
| + select { |
| + case ref := <-got: |
| + if ref != "" { |
| + t.Fatalf("a stalled read answered %q", ref) |
| + } |
| + case <-bound: |
| + t.Fatalf("lookups still waiting %s after a stalled read began", time.Since(start)) |
| + } |
| + } |
| + if _, _, found := s.a.holders.get(s.rt.Host); found { |
| + t.Fatal("a stalled read cached an entry") |
| + } |
| + s.a.holders.mu.Lock() |
| + flights := len(s.a.holders.flights) |
| + s.a.holders.mu.Unlock() |
| + if flights != 0 { |
| + t.Fatalf("%d flights left after the stalled read", flights) |
| + } |
| + t.Setenv("CARLOS_S3_ENDPOINT", s.fx.URL) |
| + if ref := s.a.ForwardTarget(context.Background(), s.rt); ref != "b" || lockGets(s.b, s.rt) != 1 { |
| + t.Fatalf("the lookup after the stall: %q with %d reads, want b and 1", ref, lockGets(s.b, s.rt)) |
| + } |
| +} |