Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get-free-port: prevent duplicate ports on Linux #1478

Merged
merged 5 commits into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 55 additions & 39 deletions testsuite/freeport.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,57 +25,73 @@ package testsuite
import (
"fmt"
"net"
"runtime"
)

// Modified from Temporalite which itself modified from
// https://github.com/phayes/freeport/blob/95f893ade6f232a5f1511d61735d89b1ae2df543/freeport.go
// Copied and adapted from
// https://github.com/temporalio/cli/blob/350cb2f9dca55e5063b39ffbdaa2739fdeab4399/temporalcli/devserver/freeport.go

func newPortProvider() *portProvider {
return &portProvider{}
}

type portProvider struct {
listeners []*net.TCPListener
}

// GetFreePort asks the kernel for a free open port that is ready to use.
// Returns the interface's IP and the free port.
func (p *portProvider) GetFreePort() (string, int, error) {
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
if err != nil {
if addr, err = net.ResolveTCPAddr("tcp6", "[::1]:0"); err != nil {
return "", 0, fmt.Errorf("failed to get free port: %w", err)
}
}

l, err := net.ListenTCP("tcp", addr)
// Returns a TCP port that is available to listen on, for the given (local) host.
//
// This works by binding a new TCP socket on port 0, which requests the OS to
// allocate a free port. There is no strict guarantee that the port will remain
// available after this function returns, but it should be safe to assume that
// a given port will not be allocated again to any process on this machine
// within a few seconds.
//
// On Unix-based systems, binding to the port returned by this function requires
// setting the `SO_REUSEADDR` socket option (Go already does that by default,
// but other languages may not); otherwise, the OS may fail with a message such
// as "address already in use". Windows default behavior is already appropriate
// in this regard; on that platform, `SO_REUSEADDR` has a different meaning and
// should not be set (setting it may have unpredictable consequences).
func getFreePort(host string) (string, int, error) {
l, err := net.Listen("tcp", host+":0")
if err != nil {
return "", 0, err
return "", 0, fmt.Errorf("failed to assign a free port: %w", err)
}
defer func() { _ = l.Close() }()
port := l.Addr().(*net.TCPAddr).Port

p.listeners = append(p.listeners, l)
tcpAddr := l.Addr().(*net.TCPAddr)

return tcpAddr.IP.String(), tcpAddr.Port, nil
}

func (p *portProvider) Close() error {
for _, l := range p.listeners {
if err := l.Close(); err != nil {
return err
// On Linux and some BSD variants, ephemeral ports are randomized, and may
// consequently repeat within a short time frame after the listenning end
// has been closed. To avoid this, we make a connection to the port, then
// close that connection from the server's side (this is very important),
// which puts the connection in TIME_WAIT state for some time (by default,
// 60s on Linux). While it remains in that state, the OS will not reallocate
// that port number for bind(:0) syscalls, yet we are not prevented from
// explicitly binding to it (thanks to SO_REUSEADDR).
//
// On macOS and Windows, the above technique is not necessary, as the OS
// allocates ephemeral ports sequentially, meaning a port number will only
// be reused after the entire range has been exhausted. Quite the opposite,
// given that these OSes use a significantly smaller range for ephemeral
// ports, making an extra connection just to reserve a port might actually
// be harmful (by hastening ephemeral port exhaustion).
if runtime.GOOS != "darwin" && runtime.GOOS != "windows" {
r, err := net.DialTCP("tcp", nil, l.Addr().(*net.TCPAddr))
if err != nil {
return "", 0, fmt.Errorf("failed to assign a free port: %w", err)
}
c, err := l.Accept()
if err != nil {
return "", 0, fmt.Errorf("failed to assign a free port: %w", err)
}
// Closing the socket from the server side
_ = c.Close()
defer func() { _ = r.Close() }()
}
return nil

return host, port, nil
}

func getFreeHostPort() (string, error) {
pp := newPortProvider()
host, port, err := pp.GetFreePort()
closeErr := pp.Close()
host, port, err := getFreePort("127.0.0.1")
if err != nil {
return "", err
} else if closeErr != nil {
return "", fmt.Errorf("failed to close TCP listener: %w", closeErr)
host, port, err = getFreePort("[::1]")
if err != nil {
return "", err
}
}
return fmt.Sprintf("%v:%v", host, port), nil
}
Loading