{"record":{"id":"66cf929d9eca09c6","repo":"GoogleContainerTools/skaffold","slug":"cannot-find-available-port","errorCode":null,"errorMessage":"cannot find available port","messagePattern":"cannot find available port","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/util/port.go","lineNumber":199,"sourceCode":"func AllocatePort(isPortAvailable func(int32) bool, desiredPort int32) int32 {\n\tvar maxPort int32 = 65535 // ports are normally [1-65535]\n\tif desiredPort < 1024 || desiredPort > maxPort {\n\t\tlog.Entry(context.TODO()).Debugf(\"skipping reserved port %d\", desiredPort)\n\t\tdesiredPort = 1024 // skip reserved ports\n\t}\n\t// We assume ports are rather sparsely allocated, so even if desiredPort\n\t// is allocated, desiredPort+1 or desiredPort+2 are likely to be free\n\tfor port := desiredPort; port < maxPort; port++ {\n\t\tif isPortAvailable(port) {\n\t\t\treturn port\n\t\t}\n\t}\n\tfor port := desiredPort; port > 1024; port-- {\n\t\tif isPortAvailable(port) {\n\t\t\treturn port\n\t\t}\n\t}\n\tpanic(\"cannot find available port\") // exceedingly unlikely\n}\n","sourceCodeStart":181,"sourceCodeEnd":201,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/util/port.go#L181-L201","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Free local ports or reduce long-lived listeners (check with ss -tlnp / netstat)","Restart the dev environment/container to clear ephemeral port exhaustion","Catch the panic at a recover boundary and fall back to letting the OS pick a port (net.Listen(\":0\"))","Investigate isPortAvailable if it fails even for obviously free ports (permissions, IPv6-only, firewall)"],"exampleFix":"// before\nl, _ := net.Listen(\"tcp\", fmt.Sprintf(\":%d\", AllocatePort(8080)))\n// after\nl, err := net.Listen(\"tcp\", \":8080\") // OS picks a free port if 8080 is taken\nif err != nil { /* handle */ }","handlingStrategy":"fallback","validationCode":"func hostHasFreePorts() bool {\n  l, err := net.Listen(\"tcp\", \":0\")\n  if err != nil { return false }\n  l.Close()\n  return true\n}","typeGuard":null,"tryCatchPattern":"func allocatePortSafe(desired int) (port int) {\n  defer func() {\n    if recover() != nil {\n      if l, err := net.Listen(\"tcp\", \":0\"); err == nil {\n        port = l.Addr().(*net.TCPAddr).Port\n        l.Close()\n      }\n    }\n  }()\n  return util.AllocatePort(desired)\n}","preventionTips":["Prefer net.Listen(\":0\") for ephemeral ports instead of manual scanning","Monitor host listener counts (ss -s) in long-running dev environments","Raise ulimit or recycle the dev container if you see port exhaustion"],"tags":["network","ports","panic","port-exhaustion"],"backgroundTag":"port-allocation-exhausted","analyzedSha":"a1189de023efc32d4b8e11f395acc678aa555011","analyzedAt":"2026-09-05T12:09:27.064Z","contentChangedAt":"2026-09-05T12:09:27.064Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}