tests: the latency host prefers cancellation over its delay
RoundTrip checked the request's context only inside a select against the delay timer, so a probe on a cancelled request could, after a long enough pause, take the timer branch and answer 404. It now fails a cancelled request before making a timer and re-checks the context after the select, so cancellation always wins.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+11
−2
internal/console/perflatency_test.go+11 −2
diff --git a/internal/console/perflatency_test.go b/internal/console/perflatency_test.go| index df32ea772..b6ed92231 100644 |
| --- a/internal/console/perflatency_test.go |
| +++ b/internal/console/perflatency_test.go |
| @@ -74,12 +74,21 @@ func (h *latencyHost) RoundTrip(r *http.Request) (*http.Response, error) { |
| } |
| h.l.host.Add(1) |
| if h.l.on.Load() && h.l.HostDelay > 0 { |
| + // A request already cancelled fails before any timer exists, and |
| + // cancellation wins when it and the delay are both ready, so the |
| + // answer never depends on which a select happened to pick. |
| + ctx := r.Context() |
| + if err := ctx.Err(); err != nil { |
| + return nil, err |
| + } |
| t := time.NewTimer(h.l.HostDelay) |
| select { |
| case <-t.C: |
| - case <-r.Context().Done(): |
| + case <-ctx.Done(): |
| t.Stop() |
| - return nil, r.Context().Err() |
| + } |
| + if err := ctx.Err(); err != nil { |
| + return nil, err |
| } |
| } |
| status, body := http.StatusNotFound, "404 page not found\n" |