Skip to content

test(proxy): give the late-first-byte test a resend deadline that holds under load - #1020

Merged
amiddavid merged 1 commit into
mainfrom
fix/flake-847
Nov 10, 2026
Merged

amiddavid merged 1 commit into
mainfrom
fix/flake-847

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Test-only, no behavior change. Only proxy/upstreamsilence_test.go changes.

Summary

  • The flake is a timing artifact of the test (case a), not a proxy bug.
  • The fix gives the resend more time. The test still proves the same property.

What we found

  • The test used one 5 ms silence limit for both attempts.
  • Attempt 1 does not depend on the limit. Its trace hook holds the first byte until the watchdog cancels it.
  • The resend does depend on it. The server answers at once, but under -race on a loaded box the answer can take more than 5 ms.
  • Then the resend is also cut as silent, and the call fails with "on two connections".
  • That is correct proxy behavior for a really silent upstream. The proxy did not wait wrongly.

What changed

  • Silence limit 5 ms to 15 ms. Calls per run 300 to 100, so one run costs about the same as before.
  • A comment explains why.

How it was tested

Box, 4 busy-loop cores, -race:

  • Before: go test -race -count=200 -run '^TestALateFirstByteOnTheRealTransportNeverReachesTheClient$' ./proxy/ gave 3 failures in 200 runs.
  • After: same test as a -race test binary with -test.count=200: 0 failures in 200 runs.
  • Mutation: disable the check that drops a response whose watchdog fired (if false && err == nil && errors.Is(context.Cause(ctx), errUpstreamSilent)). The test fails 20 of 20 runs ("the client got a response whose watchdog had fired").
  • go test -race ./proxy/...: ok. go vet ./proxy/ and gofmt: clean.

Closes #847

…ds under load

The test used one 5 ms limit for both attempts. The first attempt does not
depend on the limit, because its trace hook holds the byte until the watchdog
cancels it. The resend does depend on it. Under -race on a loaded box the
resend went silent past 5 ms and the call failed with "on two connections".

Use 15 ms and 100 calls. Test-only, no behavior change.

Closes #847

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 497593f0f3c0 approve

Verdict: approve

Findings

None.

Non-blocking

  • proxy/upstreamsilence_test.go:712 — 100 calls lower the chance to catch a regression. The bad shape shows in about 1 call of 20, so one run of 100 calls catches it with about 99.4% chance (300 calls gave ~100%). A regressed build could pass about 1 run of 170. The mutation run in the PR (20 of 20 failures with the guard disabled) covers this today. Acceptable trade for a cost-neutral run.
  • proxy/upstreamsilence_test.go:711 — 15 ms is an empirical bound, not a guarantee. A box slower than the test box (4 busy-loop cores, -race, 0 failures in 200 runs) could still exceed it and bring the flake back. The test cannot do better without a production change: doWithSilenceGuard uses one silence value for both attempts.

What I checked

  • The full diff: one commit, one test file, no production code. No behavior change, so the behavior-change and data-dependence rules do not apply.
  • The comment's first claim: the limit bounds the resend too. True — doWithSilenceGuard passes the same silence to every silenceAttempt (proxy/upstreamsilence.go:144), and a resend whose first byte is late is cancelled by its own watchdog.
  • The comment's second claim: the first attempt does not depend on the limit. True — the test hook GotFirstResponseByte: func() { <-ctx.Done() } (proxy/upstreamsilence_test.go:719) holds the byte until the watchdog has cancelled. httptrace.WithClientTrace runs the newly added trace's hooks first, so the blocking hook runs before the guard's own GotFirstResponseByte hook sets answered; the watchdog therefore always fires, at any limit. No deadlock path: the watchdog sees answered == false while the hook blocks.
  • The math in the comment: 100 calls at 1-in-20 gives about 5 expected hits per run. Consistent with the PR's mutation result (20 of 20 failures).
  • Test cost: 100 calls at ~15 ms is about the same run time as 300 calls at ~5 ms, as the PR states.
  • Commit hygiene: DCO Signed-off-by present, Assisted-By: attribution (not Co-Authored-By), conventional-commit title.
  • The PR's test evidence (0 failures in 200 runs after, 20 of 20 mutation failures) is the author's run on the eval box. I did not re-run it: live tests are not approved for this review.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 497593f0f3c0 approve

Verdict: approve

One commit. It changes one test only: TestALateFirstByteOnTheRealTransportNeverReachesTheClient
in proxy/upstreamsilence_test.go. The silence limit goes from 5 ms to 15 ms, the loop from 300 to
100 calls, and a comment explains why. No production code changes, so no behavior change and no
data-dependence change.

Findings

None.

Non-blocking

  • proxy/upstreamsilence_test.go:709 — "100 calls still find the 1-in-20 shape" is not a promise.
    The shape appears on about 1 call in 20 (the file's own comment at line 695). The chance that 100
    calls see none is 0.95^100 = 0.59%. With the old 300 calls it was 0.95^300 ≈ 0.00002%. So
    about 1 run in 170 now passes without ever exercising the race it guards. Small, and it buys back
    the 3-in-200 hard flake, so I accept it. If you want the coverage back, keep 300 calls and accept
    a ~4.5 s test, or count the shape and assert a minimum.
  • proxy/upstreamsilence_test.go:711 — 15 ms lowers the flake but keeps its cause. One limit still
    does two jobs: it arms the watchdog on the first attempt (where its length is irrelevant, the hook
    holds the byte until the cancel) and bounds the resend's first byte. A load spike past 15 ms
    re-creates the failure. A structural fix needs no timing margin: a resend that goes silent too
    fails the call with the two-silences error, and the client still got no dead body — that outcome
    is compliant, so the test could tolerate it and stay fatal only on the dead-body symptom
    (a wrong body or a failed read). Then the limit could go back to 5 ms with 300 calls.

What I checked

  • The full diff, e37d14ca..HEAD: one file, 11 insertions, 2 deletions. Every changed line read.
  • proxy/upstreamsilence.go in full: the watchdog arms on WroteRequest with the same silence
    for every attempt (doWithSilenceGuard line 144), so the comment's premise — the limit also
    bounds the resend — matches the code.
  • The claim that the first attempt does not depend on the limit. It holds. The blocking
    GotFirstResponseByte hook on the first attempt waits for ctx.Done(), which only the watchdog
    closes. The watchdog cancels it whatever the limit is, because answered is not stored before
    the hook returns: httptrace.WithClientTrace runs the newer trace's hook first, and the older
    hook (which stores answered) runs only after the block ends. So the first attempt always ends
    cancelled and resent, and its duration grows with the limit — the reason 100 calls, not 300, keep
    the old ~1.5 s runtime.
  • The new failure mode the comment describes. At 5 ms, a resend whose answer took more than 5 ms
    was itself cancelled, produced errUpstreamSilent "on two connections", and hit the
    t.Fatalf at line 726. That matches the code path: one silence error is resent, a second is
    returned (lines 126-131).
  • That nothing else references the old 5 ms / 300 values, and that the silence const shadows
    nothing in the function.
  • Live tests were not approved, so I did not run the test. The 3-in-200 figure is the author's
    data; I could not reproduce it, only check that the code admits it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

test: TestALateFirstByteOnTheRealTransportNeverReachesTheClient is flaky under -race

1 participant