Skip to content

test: bind git HTTP test server to ephemeral port#1085

Merged
cpuguy83 merged 2 commits into
mainfrom
cpuguy83/musical-waffle
Jun 19, 2026
Merged

test: bind git HTTP test server to ephemeral port#1085
cpuguy83 merged 2 commits into
mainfrom
cpuguy83/musical-waffle

Conversation

@cpuguy83

@cpuguy83 cpuguy83 commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Problem

TestSourceGitChecksumPreservesTagMetadata (and occasionally TestGomodGitAuth/HTTP) failed intermittently with:

server error: failed to listen on 0.0.0.0:8080: listen tcp 0.0.0.0:8080: bind: address already in use

The integration-test git HTTP server hardcoded binding 0.0.0.0:8080. The gateway containers that run these servers share the BuildKit worker's network namespace, so when two servers were alive at the same time the second net.Listen hit EADDRINUSE.

TestSourceGitChecksumPreservesTagMetadata runs two t.Parallel() subtests, each starting an HTTP git server on 8080, and TestGomodGitAuth/HTTP uses 8080 too. The failure only surfaced when these overlapped under the test scheduler, which is why it was flaky.

Fix

Let the kernel assign a free port instead of hardcoding 8080:

  • cmd/server (runServe): accepts "0" as the requested port, then reads the actual bound port from listener.Addr() and reports it in the ready event.
  • StartHTTPGitServer: records the real port back into Attr.HTTPPort so the spec and client gitconfig generation that run afterward use it.
  • source_test.go / gomod_git_auth_test.go: request port "0" instead of 8080.

Trade-off

Callers must start the HTTP server before generating specs or client gitconfig, since the concrete port is only known once the server is online. Current call sites already follow this order.

SSH (port 2222) is left unchanged — only one SSH server is started per test process, so it can't collide.

The integration-test git HTTP server hardcoded binding 0.0.0.0:8080.
Gateway containers share the BuildKit worker's network namespace, so when
multiple servers ran concurrently the second net.Listen failed with
EADDRINUSE. This made TestSourceGitChecksumPreservesTagMetadata (two
t.Parallel() subtests) and TestGomodGitAuth/HTTP flaky.

The server now accepts port "0" to request an ephemeral port from the
kernel and reports the actually-bound port in its ready event.
StartHTTPGitServer records that real port back into Attr.HTTPPort so the
spec and client gitconfig generation that run afterward use it. Tests now
request port "0" instead of a fixed 8080.

Trade-off: callers must start the HTTP server before generating specs or
client gitconfig, since the concrete port is only known once the server is
online.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
@cpuguy83 cpuguy83 force-pushed the cpuguy83/musical-waffle branch from 1e21532 to 37ff73a Compare June 8, 2026 01:43
@cpuguy83 cpuguy83 marked this pull request as ready for review June 8, 2026 02:14
Copilot AI review requested due to automatic review settings June 8, 2026 02:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes flaky integration test failures (TestSourceGitChecksumPreservesTagMetadata and TestGomodGitAuth/HTTP) caused by port collisions when multiple parallel test git HTTP servers all attempted to bind to the hardcoded port 8080. The fix uses ephemeral port allocation (port "0") and propagates the actual bound port back through the existing ReadyEvent protocol.

Changes:

  • The server binary (cmd/server) now resolves the actual bound port from the listener when "0" is requested, and emits it in the ready event. Error paths correctly close the listener.
  • StartHTTPGitServer records the real port back into Attr.HTTPPort so downstream spec and gitconfig generation use the correct port.
  • Test call sites in source_test.go and gomod_git_auth_test.go request port "0" instead of "8080".

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/git_services/cmd/server/main.go Resolves the actual bound port from the TCP listener after binding, uses it in the ready event, and adds listener.Close() on error paths.
test/git_services/teststate.go Writes the actual port from the ready event back into ts.Attr.HTTPPort so downstream consumers observe the real port.
test/git_services/attributes.go Updates the HTTPPort field documentation to describe ephemeral port behavior.
test/source_test.go Changes HTTPPort from "8080" to "0".
test/gomod_git_auth_test.go Changes HTTPPort from "8080" to "0" and updates the comment to explain ephemeral port semantics.

@cpuguy83 cpuguy83 self-assigned this Jun 8, 2026
@cpuguy83 cpuguy83 requested a review from invidian June 8, 2026 16:10
The port-related fields (Attributes.HTTPPort/SSHPort, ReadyEvent.Port,
ServerResult.Port, GenerateSpec's port local) were strings. A port is
fundamentally a number, and keeping it a string forced a fallible
string->int conversion path. Modeling it as int means only the infallible
int->string conversion happens, and only at the edges that genuinely need
a string (the listen-address exec arg, the gomod auth host:port key, and
the source URL).

The server now emits the bound port as a JSON number in its ready event,
and the SSH server script emits its port unquoted to match.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
@cpuguy83

cpuguy83 commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator Author

GHA appears to be wedged.

@cpuguy83 cpuguy83 closed this Jun 8, 2026
@cpuguy83 cpuguy83 reopened this Jun 8, 2026
@cpuguy83 cpuguy83 requested a review from DannyBrito June 15, 2026 23:15

@DannyBrito DannyBrito left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm

@cpuguy83 cpuguy83 merged commit c93a206 into main Jun 19, 2026
61 of 65 checks passed
@cpuguy83 cpuguy83 deleted the cpuguy83/musical-waffle branch June 19, 2026 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants