GoogleContainerTools/skaffold · error

cannot find available port

Error message

cannot find available port

What it means

util.AllocatePort scans downward from the desired port to 1024 looking for a free local port; if none is free it panics "cannot find available port". The comment notes this is exceedingly unlikely — it requires every port from 1024 to the desired one to be occupied on the machine.

Source

Thrown at pkg/skaffold/util/port.go:199

func AllocatePort(isPortAvailable func(int32) bool, desiredPort int32) int32 {
	var maxPort int32 = 65535 // ports are normally [1-65535]
	if desiredPort < 1024 || desiredPort > maxPort {
		log.Entry(context.TODO()).Debugf("skipping reserved port %d", desiredPort)
		desiredPort = 1024 // skip reserved ports
	}
	// We assume ports are rather sparsely allocated, so even if desiredPort
	// is allocated, desiredPort+1 or desiredPort+2 are likely to be free
	for port := desiredPort; port < maxPort; port++ {
		if isPortAvailable(port) {
			return port
		}
	}
	for port := desiredPort; port > 1024; port-- {
		if isPortAvailable(port) {
			return port
		}
	}
	panic("cannot find available port") // exceedingly unlikely
}

View on GitHub (pinned to a1189de023)

Solutions

  1. Free local ports or reduce long-lived listeners (check with ss -tlnp / netstat)
  2. Restart the dev environment/container to clear ephemeral port exhaustion
  3. Catch the panic at a recover boundary and fall back to letting the OS pick a port (net.Listen(":0"))
  4. Investigate isPortAvailable if it fails even for obviously free ports (permissions, IPv6-only, firewall)

Example fix

// before
l, _ := net.Listen("tcp", fmt.Sprintf(":%d", AllocatePort(8080)))
// after
l, err := net.Listen("tcp", ":8080") // OS picks a free port if 8080 is taken
if err != nil { /* handle */ }
Defensive patterns

Strategy: fallback

Validate before calling

func hostHasFreePorts() bool {
  l, err := net.Listen("tcp", ":0")
  if err != nil { return false }
  l.Close()
  return true
}

Try / catch

func allocatePortSafe(desired int) (port int) {
  defer func() {
    if recover() != nil {
      if l, err := net.Listen("tcp", ":0"); err == nil {
        port = l.Addr().(*net.TCPAddr).Port
        l.Close()
      }
    }
  }()
  return util.AllocatePort(desired)
}

Prevention

When it happens

Trigger: Calling AllocatePort(desiredPort) when isPortAvailable is false for all ports in (1024, desiredPort] — i.e. an extremely saturated host port space, or a broken isPortAvailable returning false for everything.

Common situations: Running on a host with thousands of open connections/listeners (port exhaustion); running inside a sandbox/network namespace where binding checks always fail; pathological local firewall setups.

Related errors


AI-assisted analysis of GoogleContainerTools/skaffold@a1189de023 (2026-09-05). Data as JSON: /api/errors/66cf929d9eca09c6. Report an issue: GitHub.