{"record":{"id":"6a2157a5d2ec010f","repo":"apache/beam","slug":"failed-to-find-open-port-for-service-got-v","errorCode":null,"errorMessage":"failed to find open port for service, got %v","messagePattern":"failed to find open port for service, got (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/runtime/xlangx/expansionx/process.go","lineNumber":53,"sourceCode":"}\n\nfunc findOpenPort() (int, error) {\n\tlistener, err := net.Listen(\"tcp\", \":0\")\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\tdefer listener.Close()\n\treturn listener.Addr().(*net.TCPAddr).Port, nil\n}\n\n// NewExpansionServiceRunner builds an ExpansionServiceRunner struct for a given gradle target and\n// Beam version and returns a pointer to it. Passing an empty string as servicePort will request an\n// open port to be assigned to the service.\nfunc NewExpansionServiceRunner(jarPath, servicePort string) (*ExpansionServiceRunner, error) {\n\tif servicePort == \"\" {\n\t\tport, err := findOpenPort()\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to find open port for service, got %v\", err)\n\t\t}\n\t\tservicePort = fmt.Sprintf(\"%d\", port)\n\t}\n\tserviceCommand := exec.Command(\"java\", \"-jar\", jarPath, servicePort)\n\treturn &ExpansionServiceRunner{execPath: jarPath, servicePort: servicePort, serviceCommand: serviceCommand}, nil\n}\n\n// NewPyExpansionServiceRunner builds an ExpansionServiceRunner struct for a given python module and\n// Beam version and returns a pointer to it. Passing an empty string as servicePort will request an\n// open port to be assigned to the service.\nfunc NewPyExpansionServiceRunner(pythonExec, module, servicePort string) (*ExpansionServiceRunner, error) {\n\tif servicePort == \"\" {\n\t\tport, err := findOpenPort()\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to find open port for service, got %v\", err)\n\t\t}\n\t\tservicePort = fmt.Sprintf(\"%d\", port)\n\t}","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/runtime/xlangx/expansionx/process.go#L35-L71","documentation":"NewExpansionServiceRunner launches a Java expansion service jar with exec.Command. When servicePort is empty, it requests a free port via findOpenPort(); if no port can be found it wraps that error as \"failed to find open port for service\". The runner is never created in that case.","triggerScenarios":"Calling NewExpansionServiceRunner with an empty servicePort string on a host where findOpenPort() cannot bind any listener (no available ports, network stack issues, listener creation error).","commonSituations":"Port exhaustion from many open sockets; restrictive firewall/SELinux blocking ephemeral port binding; running in sandboxes where binding to localhost is disallowed.","solutions":["Pass an explicit, known-free servicePort instead of \"\" so findOpenPort is skipped.","Check system port availability (netstat/ss) and free up ephemeral ports or widen the ephemeral range (net.ipv4.ip_local_port_range).","Verify localhost binding works (firewall, SELinux, container network policy).","Retry later if the failure was transient port contention."],"exampleFix":"// before\nrunner, err := expansionx.NewExpansionServiceRunner(jarPath, \"\")\n// after\nrunner, err := expansionx.NewExpansionServiceRunner(jarPath, \"55501\") // known free port","handlingStrategy":"validation","validationCode":"if servicePort == \"\" {\n    l, err := net.Listen(\"tcp\", \"localhost:0\")\n    if err != nil {\n        return fmt.Errorf(\"no bindable port available: %w\", err)\n    }\n    servicePort = strconv.Itoa(l.Addr().(*net.TCPAddr).Port)\n    l.Close()\n}","typeGuard":null,"tryCatchPattern":"runner, err := expansionx.NewExpansionServiceRunner(jar, port)\nif err != nil {\n    return fmt.Errorf(\"starting java expansion service on %s: %w\", port, err)\n}","preventionTips":["Prefer explicit configured ports in constrained environments","Monitor ephemeral port usage on busy hosts","Verify localhost binding works in containers/sandboxes before launching"],"tags":["go","network","ports","expansion-service"],"backgroundTag":"address-already-in-use","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}