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 at
once; with the gate running dozens of test processes, the next caller of
127.0.0.1:0 in another process could get that port. The edge's dialer
ejected the first dead upstream, connected to the second, and proxied
the other test's 404. The log has one "down" line, not two, so the
second dial connected.

deadAddr now holds the port with a socket that is bound but never
listens: connections get a reset, and nothing else can bind the port
until the test ends (SO_REUSEADDR left off, since Linux lets two sockets
that both set it share a non-listening port). The tests that bring a
dead address back to life call reviveDeadAddr, which makes that same
socket listen, so there is no gap in between. Test-only change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 032174578eaadac9e066a71808916163916d7ec4 parent 3b77ecb
4 files changed, +143 −22
  • internal/edge/deadaddr_test.go +139 −0
  • internal/edge/edge_test.go +0 −18
  • internal/edge/localhold_probed_test.go +1 −1
  • internal/edge/localhold_test.go +3 −3

CI passed — run details

diff --git a/internal/edge/deadaddr_test.go b/internal/edge/deadaddr_test.go
new file mode 100644
index 000000000..8ac8c5b89
--- /dev/null
+++ b/internal/edge/deadaddr_test.go
@@ -0,0 +1,139 @@
+//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.
+var (
+ deadSocketsMu sync.Mutex
+ deadSockets = map[string]int{}
+)
+
+// 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)
+ }
+ held := true
+ t.Cleanup(func() {
+ if held {
+ syscall.Close(fd)
+ }
+ })
+ if err := syscall.Bind(fd, &syscall.SockaddrInet4{Addr: [4]byte{127, 0, 0, 1}}); err != nil {
+ t.Fatalf("bind 127.0.0.1:0: %v", err)
+ }
+ sa, err := syscall.Getsockname(fd)
+ if err != nil {
+ t.Fatalf("getsockname: %v", err)
+ }
+ in4, ok := sa.(*syscall.SockaddrInet4)
+ if !ok {
+ t.Fatalf("getsockname: %T, want an IPv4 address", sa)
+ }
+ addr := net.JoinHostPort("127.0.0.1", strconv.Itoa(in4.Port))
+ deadSocketsMu.Lock()
+ deadSockets[addr] = fd
+ deadSocketsMu.Unlock()
+ t.Cleanup(func() {
+ deadSocketsMu.Lock()
+ defer deadSocketsMu.Unlock()
+ if got, ok := deadSockets[addr]; ok && got == fd {
+ delete(deadSockets, addr)
+ } else {
+ held = false // reviveDeadAddr took it; its listener closes it
+ }
+ })
+ 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()
+ fd, ok := deadSockets[addr]
+ delete(deadSockets, addr)
+ deadSocketsMu.Unlock()
+ if !ok {
+ return nil, errors.New("not an address deadAddr holds: " + addr)
+ }
+ 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..91a4af2ba 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