Skip to content

fix: ✅ Fix port reuse flaky test - #1998

Merged
huang195 merged 1 commit into
rossoctl:mainfrom
evaline-ju:fix-egress-port-flake
Nov 19, 2026
Merged

huang195 merged 1 commit into
rossoctl:mainfrom
evaline-ju:fix-egress-port-flake

Conversation

@evaline-ju

@evaline-ju evaline-ju commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Tests
    • Improved coverage of egress behavior when an upstream connection is refused, verifying the expected 502 response.

Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
@evaline-ju
evaline-ju requested a review from a team as a code owner November 19, 2026 15:37
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The egress dial-failure test now starts its rig before reserving and closing the local port used as the unreachable destination. It continues to assert a 502 response with upstream_refused.

Changes: Egress dial-failure test

Layer / File(s) Summary
Reorder test setup
core/listener/forwardproxy/egress_test.go
The test starts the egress rig before reserving and closing the destination port. The 502 and upstream_refused assertions remain.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Other

Possibly related PRs

Suggested reviewers: huang195, clawgenti

Merge Risk: Minimal · up to e8873

This change fixes a flaky test by starting the servers before reserving the unreachable port. It has no production impact and is safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title says the PR fixes a port reuse flaky test. The raw summary and objectives both describe a change to the setup order in TestEgress_DialFailure502WithKind so that the refused-destination port …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title says the PR fixes a port reuse flaky test. The raw summary and objectives both describe a change to the setup order in TestEgress_DialFailure502WithKind so that the refused-destination port stays unused. The title names the main change, so it passes. The title uses a conventional commit prefix and an emoji, which the title guidance treats as noise, but this does not make the title unrelated or vague.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cadb8688-e830-4249-a175-aa52c5795d16
Commits

Reviewing files that changed from the base of the PR and between 9064cbe and e887332.

Files selected for processing: 1

  • core/listener/forwardproxy/egress_test.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@github-actions github-actions Bot added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 19, 2026

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@huang195 huang195 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)

@huang195
huang195 merged commit 049585e into rossoctl:main Nov 19, 2026
43 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 19, 2026
@evaline-ju
evaline-ju deleted the fix-egress-port-flake branch November 19, 2026 17:09
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

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants