Skip to content

test(egress): wait for the accept loop in TestCapRefusalsAreBoundedPerVM - #652

Merged
pdettori merged 2 commits into
mainfrom
fix/egress-cap-refusal-flake
Oct 28, 2026
Merged

pdettori merged 2 commits into
mainfrom
fix/egress-cap-refusal-flake

Conversation

@pdettori

Copy link
Copy Markdown
Member

What

proto has been flaking on main (run 40708539112) and on PRs (#645) with:

--- FAIL: TestCapRefusalsAreBoundedPerVM (0.00s)
    proxy_test.go:530: no refusal was ever in flight: the test did not exercise the refusal path

Root cause

The test's offer loop (48 unix-socket dials + CONNECT writes, sampling capRefusalsInFlight after each) never blocks: the kernel completes the dials into the listener backlog and the requests fit the socket buffers. b.capRefusals is only incremented by admit() on the accept goroutine, so when that goroutine isn't scheduled during the loop — deterministic at GOMAXPROCS=1 (10/10 repro), occasional on a loaded two-vCPU CI runner under -race after ~49s of package tests — every sample reads 0 and the peak == 0 validity 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 == 0 after 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 sibling TestCapRefusalPastTheBoundClosesWithoutDraining and waitCapCharged.

Verification

  • GOMAXPROCS=1 go test -race -run TestCapRefusalsAreBoundedPerVM -count=10: failed 10/10 before, passes 20/20 after
  • mutation check (drain := true, bound removed in admit): the fixed test still fails 5/5, catching the violation through the catch-up sample
  • full go test -race ./... in remote-worker: green

Assisted-By: Claude Code

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]>
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Oct 28, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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]>
@pdettori
pdettori merged commit fda2f6b into main Oct 28, 2026
15 checks passed
@pdettori
pdettori deleted the fix/egress-cap-refusal-flake branch October 28, 2026 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants