{"record":{"id":"a9d4da556bee5f0f","repo":"slimtoolkit/slim","slug":"cannot-detect-host-port","errorCode":null,"errorMessage":"cannot detect host port","messagePattern":"cannot detect host port","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/app/master/kubernetes/kubectl.go","lineNumber":176,"sourceCode":"\tif err != nil {\n\t\treturn cmd, \"\", err\n\t}\n\n\tif err := cmd.Start(); err != nil {\n\t\treturn cmd, \"\", err\n\t}\n\n\tvar actualHostPort int\n\tpattern := fmt.Sprintf(\"Forwarding from %s:%s -> %s\", address, \"%d\", podPort)\n\tscanner := bufio.NewScanner(out)\n\tfor scanner.Scan() {\n\t\tn, err := fmt.Sscanf(scanner.Text(), pattern, &actualHostPort)\n\t\tif err == nil && n == 1 {\n\t\t\treturn cmd, fmt.Sprintf(\"%d\", actualHostPort), nil\n\t\t}\n\t}\n\n\treturn cmd, \"\", errors.New(\"cannot detect host port\")\n}\n","sourceCodeStart":158,"sourceCodeEnd":178,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/app/master/kubernetes/kubectl.go#L158-L178","documentation":"After starting `kubectl port-forward`, this code scans its stdout for the actual locally-bound host port. If the process is running but no line matching the expected output pattern was parsed, it returns this error while still handing back the running command.","triggerScenarios":"kubectl emits output not matching the expected \"Forwarding from ... -> ...\" pattern (locale/CLI version differences, warnings interleaved, stderr vs stdout) so Sscanf never yields n==1.","commonSituations":"Newer kubectl versions changing the message format; kubectl writing forwarding info to stderr; immediate connection failures producing error text instead of the forwarding line.","solutions":["Check kubectl stdout/stderr for connection errors (bad pod, port closed)","Verify the kubectl version's port-forward output format matches what is parsed","Increase the wait timeout and confirm the pod/port are valid before forwarding"],"exampleFix":"// ensure the forwarded port is valid before calling\nif _, err := strconv.Atoi(podPort); err != nil { return fmt.Errorf(\"bad podPort %q\", podPort) }","handlingStrategy":"try-catch","validationCode":"// sanity-check the target before forwarding\n_, err := clientset.CoreV1().Pods(ns).Get(ctx, pod, metav1.GetOptions{})\nif err != nil { return err } // pod must exist and be running","typeGuard":null,"tryCatchPattern":"cmd, hostPort, err := k.PortForward(ctx, pod, addr, \"\", podPort)\nif err != nil && strings.Contains(err.Error(), \"cannot detect host port\") {\n  // capture cmd stdout/stderr, kill cmd, retry once with verbose kubectl\n}","preventionTips":["Pin a kubectl version whose port-forward output format you've tested","Confirm pod is Running and the container port exists before forwarding","Always terminate the returned cmd on this error to avoid orphans"],"tags":["kubernetes","port-forward","parsing"],"backgroundTag":"port-forward-failed","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}