carlos / platform Public

edge tests: deadAddr holds its port so no other test can take it

TestAllUpstreamsDownAnswers503WithRetryAfter failed once on CI with a
404 (discussion 867). deadAddr reserved a loopback port and closed it;
with the gate running dozens of test processes, another process's server
got the port. The edge ejected the first dead upstream, connected to the
second and proxied that server's 404 (one "down" line in the log, not
two).

deadAddr now holds the port with a bound, never-listening socket: dials
get a reset and nothing else can bind it until the test ends. Tests that
bring a dead address back to life call reviveDeadAddr, which makes that
same socket listen, so there is no gap. Test-only. Reviewed by Fable 5.2;
its nit (key held sockets per call, not by fd number) is in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from edge-404-flake into main.
claude agent, operated by paul@keymail.dev a5225f7ed6d05d96433debfa9a9d9986de0f1bed parent 8d5f499
4 files changed, +146 −25
  • internal/edge/deadaddr_test.go +140 −0
  • internal/edge/edge_test.go +0 −18
  • internal/edge/localhold_probed_test.go +1 −1
  • internal/edge/localhold_test.go +5 −6

CI passed — run details

diff --git a/internal/edge/deadaddr_test.go b/internal/edge/deadaddr_test.go
new file mode 100644
index 000000000..ad505358a
--- /dev/null
+++ b/internal/edge/deadaddr_test.go
@@ -0,0 +1,140 @@
+//go:build unix
+
+package edge
+
+import (
+ "errors"
+ "net"
+ "os"
+ "strconv"
+ "sync"
+ "syscall"
+ "testing"
+)
+
+// deadSockets maps each address deadAddr handed out to the socket holding
+// its port, until reviveDeadAddr takes it or the test's cleanup closes it.
+// Entries are pointers, one per deadAddr call, so a cleanup can never
+// mistake a later test's socket that reused the same fd number and port
+// for its own.
+type deadSocket struct{ fd int }
+
+var (
+ deadSocketsMu sync.Mutex
+ deadSockets = map[string]*deadSocket{}
+)
+
+// deadAddr is a loopback address that refuses every connection, instantly,
+// for as long as the test runs. Instant, so a test of the dial-failure path
+// never waits out a timeout.
+//
+// It holds the port with a socket that is bound but never listens. Linux
+// and macOS answer a connection to such a port with a reset, and nothing
+// else can bind it until the test's cleanup closes the socket. Closing a
+// reserved listener straight away, as this did until discussion 867, freed
+// the port for the next caller of 127.0.0.1:0 in any process, and the CI
+// gate runs dozens of test processes at once: an edge test then proxied
+// to another test's server and got its 404. SO_REUSEADDR is left off on
+// purpose: Go's listeners set it, and Linux lets two sockets that both set
+// it share a port that nobody is listening on.
+func deadAddr(t *testing.T) string {
+ t.Helper()
+ // Under ForkLock with close-on-exec set, as the net package opens its
+ // sockets: tests here start child processes, and a held port must not
+ // leak into one and outlive the test.
+ syscall.ForkLock.RLock()
+ fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
+ if err == nil {
+ syscall.CloseOnExec(fd)
+ }
+ syscall.ForkLock.RUnlock()
+ if err != nil {
+ t.Fatalf("socket: %v", err)
+ }
+ if err := syscall.Bind(fd, &syscall.SockaddrInet4{Addr: [4]byte{127, 0, 0, 1}}); err != nil {
+ syscall.Close(fd)
+ t.Fatalf("bind 127.0.0.1:0: %v", err)
+ }
+ sa, err := syscall.Getsockname(fd)
+ in4, ok := sa.(*syscall.SockaddrInet4)
+ if err != nil || !ok {
+ syscall.Close(fd)
+ t.Fatalf("getsockname: %T, %v; want an IPv4 address", sa, err)
+ }
+ addr := net.JoinHostPort("127.0.0.1", strconv.Itoa(in4.Port))
+ held := &deadSocket{fd: fd}
+ deadSocketsMu.Lock()
+ deadSockets[addr] = held
+ deadSocketsMu.Unlock()
+ t.Cleanup(func() {
+ deadSocketsMu.Lock()
+ defer deadSocketsMu.Unlock()
+ // Absent or someone else's: reviveDeadAddr took this one, and its
+ // listener closes it.
+ if deadSockets[addr] == held {
+ delete(deadSockets, addr)
+ syscall.Close(fd)
+ }
+ })
+ return addr
+}
+
+// reviveDeadAddr brings a deadAddr address to life: the socket holding the
+// port starts listening, so the address goes from refusing to accepting
+// with no moment in between when another process could take the port. The
+// caller closes the listener.
+func reviveDeadAddr(addr string) (net.Listener, error) {
+ deadSocketsMu.Lock()
+ held, ok := deadSockets[addr]
+ delete(deadSockets, addr)
+ deadSocketsMu.Unlock()
+ if !ok {
+ return nil, errors.New("not an address deadAddr holds: " + addr)
+ }
+ fd := held.fd
+ if err := syscall.Listen(fd, syscall.SOMAXCONN); err != nil {
+ syscall.Close(fd)
+ return nil, err
+ }
+ f := os.NewFile(uintptr(fd), addr)
+ defer f.Close() // FileListener holds its own copy of the socket
+ return net.FileListener(f)
+}
+
+// deadAddr's address must refuse every connection for the whole test, and
+// no other listener, in this process or another, may take its port. The
+// CI gate runs many test processes at once, and a port that was merely
+// closed went to another test's server, whose 404 the edge then proxied
+// (TestAllUpstreamsDownAnswers503WithRetryAfter, discussion 867).
+func TestDeadAddrStaysDeadForTheTest(t *testing.T) {
+ addr := deadAddr(t)
+ if ln, err := net.Listen("tcp", addr); err == nil {
+ ln.Close()
+ t.Fatalf("another listener took %s; a dead address must keep its port", addr)
+ }
+ conn, err := net.Dial("tcp", addr)
+ if err == nil {
+ conn.Close()
+ t.Fatalf("dial %s connected; a dead address must refuse", addr)
+ }
+ if !errors.Is(err, syscall.ECONNREFUSED) {
+ t.Fatalf("dial %s: %v, want connection refused", addr, err)
+ }
+
+ ln, err := reviveDeadAddr(addr)
+ if err != nil {
+ t.Fatalf("revive %s: %v", addr, err)
+ }
+ defer ln.Close()
+ if ln.Addr().String() != addr {
+ t.Fatalf("revived listener on %s, want %s", ln.Addr(), addr)
+ }
+ conn, err = net.Dial("tcp", addr)
+ if err != nil {
+ t.Fatalf("dial %s after revival: %v", addr, err)
+ }
+ conn.Close()
+ if _, err := reviveDeadAddr(addr); err == nil {
+ t.Fatal("revived the same address twice")
+ }
+}
diff --git a/internal/edge/edge_test.go b/internal/edge/edge_test.go
index 9cd3489af..611b7ee76 100644
--- a/internal/edge/edge_test.go
+++ b/internal/edge/edge_test.go
@@ -1662,24 +1662,6 @@ func countingBackend(t *testing.T, body string) (string, *atomic.Int64) {
return ts.Listener.Addr().String(), hits
}
-// deadAddr reserves a real loopback port and closes the listener immediately,
-// yielding an address that is well-formed and guaranteed to REFUSE
-// connections. That refusal is instant, so a test asserting the dial-failure
-// path never waits out a timeout — and, unlike a made-up port, it cannot
-// collide with something else the machine is running.
-func deadAddr(t *testing.T) string {
- t.Helper()
- ln, err := net.Listen("tcp", "127.0.0.1:0")
- if err != nil {
- t.Fatalf("listen: %v", err)
- }
- addr := ln.Addr().String()
- if err := ln.Close(); err != nil {
- t.Fatalf("close reserved listener: %v", err)
- }
- return addr
-}
-
// healthJSON marshals a probe config the way provision does when it mints a
// route, so the tests exercise the same column bytes Reload parses in
// production.
diff --git a/internal/edge/localhold_probed_test.go b/internal/edge/localhold_probed_test.go
index 190f252a8..7d0e09a33 100644
--- a/internal/edge/localhold_probed_test.go
+++ b/internal/edge/localhold_probed_test.go
@@ -301,7 +301,7 @@ func TestProbedDelayedProbeServedWithinR(t *testing.T) {
case <-stop:
return
}
- ln, err := net.Listen("tcp", a)
+ ln, err := reviveDeadAddr(a)
if err != nil {
listenErr <- fmt.Errorf("listen %s: %w", a, err)
close(back)
diff --git a/internal/edge/localhold_test.go b/internal/edge/localhold_test.go
index 44705dc66..ad336942d 100644
--- a/internal/edge/localhold_test.go
+++ b/internal/edge/localhold_test.go
@@ -180,8 +180,8 @@ func holdGet(t *testing.T, url string) (int, string) {
return resp.StatusCode, string(b)
}
-// listenLater starts handler on addr after delay, reusing a port deadAddr
-// reserved. The server is closed with the test.
+// listenLater starts handler on addr after delay, on the socket deadAddr
+// holds for it (reviveDeadAddr). The server is closed with the test.
func listenLater(t *testing.T, addr string, delay time.Duration, h http.Handler) {
t.Helper()
var (
@@ -192,7 +192,7 @@ func listenLater(t *testing.T, addr string, delay time.Duration, h http.Handler)
go func() {
defer close(done)
time.Sleep(delay)
- ln, err := net.Listen("tcp", addr)
+ ln, err := reviveDeadAddr(addr)
if err != nil {
t.Errorf("listen %s: %v", addr, err)
return
@@ -318,9 +318,8 @@ func TestHeldDialPOSTBodyArrives(t *testing.T) {
func TestHeldDialWebSocketUpgrade(t *testing.T) {
// The backend is owned for the whole test and the first dials are refused
// by an injected dialer, so the hold's retry path runs against this exact
- // server. A closed-then-rebound port (deadAddr + listenLater) can be taken
- // by another process in the gap, and the test would then upgrade against
- // a stranger.
+ // server, which an injected dialer pins more tightly than a revived
+ // deadAddr port would.
backend := httptest.NewServer(upgradeEchoHandler("ws"))
t.Cleanup(backend.Close)
backendAddr := backend.Listener.Addr().String()