Repository navigation
test(egress): wait for the accept loop in TestCapRefusalsAreBoundedPerVM - #652
Conversation
The offer loop never blocks: the kernel completes the unix dials into the listener backlog and the CONNECT requests fit the socket buffers, so the whole loop can run before the accept goroutine has charged a single refusal. On CI's loaded two-vCPU runners under -race that happens often enough to flake (main run 40708539112 and PR runs alike): every sample of capRefusalsInFlight reads zero and the peak==0 validity guard fires with "no refusal was ever in flight". Reproduces 10/10 at GOMAXPROCS=1. When peak is still zero after the loop, poll for the accept loop to drain the backlog, sampling as the charges land -- a charged refusal stays in flight for ~2*drainTimeout, and the bound must hold there too. This is the same poll-with-deadline idiom the sibling test TestCapRefusalPastTheBoundClosesWithoutDraining already uses. Verified the test still catches a removed bound (drain := true mutation fails 5/5 through the catch-up sample); fixed test passes 20/20 at GOMAXPROCS=1 and the full -race suite passes. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Summary
The root cause is right: admit() charges b.capRefusals only on the accept goroutine, and the 48 dials + CONNECT writes never block, so the burst loop can finish before a single refusal is charged. That is deterministic at GOMAXPROCS=1. Keeping the burst loop and adding a poll-with-deadline catch-up only when peak == 0 fixes the flake without weakening the burst-time bound check, and it follows the same idiom as waitCapCharged. One non-blocking suggestion: the catch-up path checks the bound only until the first refusal it sees.
Author: pdettori (MEMBER — maintainer)
Areas reviewed: Go tests (remote-worker/internal/egress)
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (proto, check, lint, k8s-kind-e2e, CodeQL, Trivy, DCO all green)
| // loop to catch up: the bound must hold as the charges land, too. | ||
| if peak == 0 { | ||
| deadline := time.Now().Add(3 * time.Second) | ||
| for peak == 0 && time.Now().Before(deadline) { |
There was a problem hiding this comment.
suggestion: This loop exits on the first non-zero sample. So when the fallback path runs, the bound is checked only at the moment the first refusal shows up, and not while the rest of the backlog drains. The comment above says "the bound must hold as the charges land, too". When the accept loop drains all 48 offers in one go (as at GOMAXPROCS=1), the first sample already sees the overshoot. If the test goroutine wakes after only a few charges, though, a broken bound would slip through. To make the catch-up path enforce the bound over the whole backlog, keep sampling until every offer has been charged:
if peak == 0 {
deadline := time.Now().Add(3 * time.Second)
for h.capCharged("vm-1") < offered && time.Now().Before(deadline) {
sample(offered)
time.Sleep(2 * time.Millisecond)
}
sample(offered)
}capCharged already counts emitted, coalesced, and pending-suppressed charges, so it reaches offered once the accept loop has caught up. Charges that were already drained are only missed if the poll stalls for about 2s, which is the same window the current code has. (Nit: with sample(offered) the failure message reads "refusal 48" for the catch-up samples. That's fine, but you could label them "catch-up" instead.)
There was a problem hiding this comment.
Implemented in 55bd74d. Verified chargeCapRefusal is called for every refusal — drained or shed — and that capCharged is monotonic (each charge lands in exactly one of: an emitted event, an event's Coalesced, or pending capSuppressed), so it reaches offered once the accept loop has caught up. The catch-up loop now polls until capCharged("vm-1") >= offered, sampling — and enforcing the bound — as every charge lands, with the same 3s deadline as waitCapCharged.
Re-ran the checks: GOMAXPROCS=1 -race -count=20 passes, the drain := true mutation still fails 5/5 (now reported as catch-up: 48 in flight, over the bound of 8), full egress package under -race green, gofmt/vet clean.
Review on #652: the catch-up poll exited on the first non-zero sample, so when it ran, the bound was checked only up to the first refusal the poll happened to see -- a broken bound in the rest of the backlog drain could slip through if the test goroutine woke after only a few charges. Poll until capCharged reaches the offer count instead, so the bound is enforced as every charge lands; label the catch-up samples in the failure message. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
What
protohas been flaking onmain(run 40708539112) and on PRs (#645) with:Root cause
The test's offer loop (48 unix-socket dials + CONNECT writes, sampling
capRefusalsInFlightafter each) never blocks: the kernel completes the dials into the listener backlog and the requests fit the socket buffers.b.capRefusalsis only incremented byadmit()on the accept goroutine, so when that goroutine isn't scheduled during the loop — deterministic atGOMAXPROCS=1(10/10 repro), occasional on a loaded two-vCPU CI runner under-raceafter ~49s of package tests — every sample reads 0 and thepeak == 0validity guard fires.This is not related to the guestagent TMPDIR/data-race flakes that #645 fixes; those are separate.
Fix
Keep the tight burst loop (the bound must hold under a burst), but when
peak == 0after it, poll with a deadline for the accept loop to drain the backlog, sampling — and enforcing the bound — as the charges land. A charged refusal stays in flight for ~2×drainTimeout, so the catch-up window is generous. Same poll-with-deadline idiom as the siblingTestCapRefusalPastTheBoundClosesWithoutDrainingandwaitCapCharged.Verification
GOMAXPROCS=1 go test -race -run TestCapRefusalsAreBoundedPerVM -count=10: failed 10/10 before, passes 20/20 afterdrain := true, bound removed inadmit): the fixed test still fails 5/5, catching the violation through the catch-up samplego test -race ./...inremote-worker: greenAssisted-By: Claude Code