Repository navigation
fix: ✅ Fix port reuse flaky test - #1998
Conversation
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
clawgenti
left a comment
There was a problem hiding this comment.
Summary
The reordering fixes the flake. Once newEgressRig has bound the origin and egress listeners, net.Listen("127.0.0.1:0") can no longer return either of their ports, so the request can't land on the rig's own servers (the 403 from egress, or a 200 from the origin). I checked this locally by running TestEgress_DialFailure502WithKind 20000 times (-count=20000) on each side:
| Failures / 20000 | Statuses seen | |
|---|---|---|
| base (old order) | 1271 (~6.4%) | 200 (origin got the port), 403 (egress got the port), 404 |
| head (this PR) | 1 | 302 (an unrelated listener on the host took the freed port) |
The full forwardproxy package passes at head, and go vet and gofmt are clean. I left one non-blocking suggestion about the remaining cross-process race.
Author: evaline-ju (CONTRIBUTOR association, repo admin — maintainer)
Areas reviewed: Go (test only)
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing so far (some jobs still pending)
| func TestEgress_DialFailure502WithKind(t *testing.T) { | ||
| // The rig's servers start first, so the closed port cannot be handed to either of them. | ||
| rig := newEgressRig(t, egressTestToken, okOrigin) | ||
| ln, err := net.Listen("tcp", "127.0.0.1:0") |
There was a problem hiding this comment.
suggestion (non-blocking): The race inside the test is gone, but the close-then-dial pattern still leaves a window across processes. Between ln.Close() and the dial, any other process can bind the freed port. On CI, go test ./... runs package test binaries in parallel, and their httptest servers draw from the same ephemeral range. I hit this once in 20000 runs at head (a 302 from some unrelated listener), so it's rare but still possible.
If you want it fully deterministic, the handler only needs errors.Is(err, syscall.ECONNREFUSED) (core/model/snapshot.go:206). You could inject a transport whose DialContext returns &net.OpError{Op: "dial", Net: "tcp", Err: os.NewSyscallError("connect", syscall.ECONNREFUSED)}, the way TestEgress_GuardRefusal403 injects its refusing transport. Then no real port is involved. Keeping the real kernel refusal is a reasonable choice too, so this is your call.
There was a problem hiding this comment.
The fix is correct. newEgressRig keeps both server ports until cleanup, so the closed port cannot go to the origin server or the egress server.
Suggestion, outside this diff: TestHop_DialFailureIsGatewayUnavailable (core/listener/forwardproxy/hop_test.go:391) has the same race. It calls closedAddr(t) before hopProxy starts its httptest server. If the proxy gets that port, the hop dial goes back to the proxy, is not refused, and the test does not get 503. The fix is the same idea as here: the proxy server must start before closedAddr(t) runs. But hopProxy takes the gateway address as an argument, so the fix is more than a one-line move. You can do it here or in a separate PR. The other closedAddr callers start their servers first, so they do not have this race.
Author: evaline-ju (MEMBER — maintainer)
Areas reviewed: Go tests
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (40 pass, 1 skipped)
Summary
The egress dial-failure test closed a port to use as a refused destination, then started its servers, which could be handed that same port. When the egress server got it, the request was forwarded back to the egress handler without a token and answered 403 instead of 502. The test now starts its servers first, so the closed port stays unused. Reported in review of #1984
Assisted-By: Claude (Anthropic AI) noreply@anthropic.com
Summary by CodeRabbit