temporalio/temporal · error

failed assigning ephemeral port: %w

Error message

failed assigning ephemeral port: %w

What it means

Error "failed assigning ephemeral port: %w" thrown in temporalio/temporal.

Source

Thrown at common/testing/freeport/freeport.go:30

// 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 MustGetFreePort() int {
	port, err := getFreePort("127.0.0.1")
	if err != nil {
		// try ipv6
		port, err = getFreePort("[::1]")
		if err != nil {
			panic(fmt.Errorf("failed assigning ephemeral port: %w", err))
		}
	}
	return port
}

func getFreePort(host string) (int, error) {
	l, err := net.Listen("tcp", host+":0")
	if err != nil {
		return 0, fmt.Errorf("failed to assign a free port: %v", err)
	}
	defer l.Close()
	port := l.Addr().(*net.TCPAddr).Port

	// On Linux and some BSD variants, ephemeral ports are randomized, and may
	// consequently repeat within a short time frame after the listening 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,

View on GitHub (pinned to bde624efd1)

When it happens

Trigger: Thrown at common/testing/freeport/freeport.go:30 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of temporalio/temporal@bde624efd1 (2026-09-01). Data as JSON: /api/errors/8baa34bf5e8989b6. Report an issue: GitHub.