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