JuliusBrussee/caveman · error

invalid listen port

Error message

invalid listen port %q

What it means

PortFromListen splits the listen address successfully but the port component fails validation: it is either not an integer or outside the 1-65535 range. This is a distinct error from the address-split failure and carries no wrapped cause.

Solutions

  1. Use a port between 1 and 65535 in the listen address
  2. Check for typos such as leading zeros with extra digits or a swapped value
  3. Strip any URL scheme before passing the address
  4. Validate the port in config before passing it to New

Example fix

// before
runstate.PortFromListen("127.0.0.1:70000")
// after
runstate.PortFromListen("127.0.0.1:8080")
Defensive patterns

Strategy: validation

Validate before calling

_, port, err := net.SplitHostPort(listen)
if err == nil {
    p, aerr := strconv.Atoi(port)
    if aerr != nil || p < 1 || p > 65535 { return fmt.Errorf("port %q out of range", port) }
}

Try / catch

port, err := runstate.PortFromListen(listen)
if err != nil && strings.HasPrefix(err.Error(), "invalid listen port") {
    return fmt.Errorf("configure a port between 1 and 65535, got %q", listen)
}

Prevention

When it happens

Trigger: Passing a listen string whose port is non-numeric ('host:abc'), zero ('host:0'), negative ('host:-1'), or above 65535 ('host:70000').

Common situations: Placeholder ports like 0 in config, typos adding an extra digit, or copying a URL that includes a scheme so the 'port' becomes garbage.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/7cea634a85354ca3. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/runstate/runstate.go:97

	for name, raw := range upstreams {
		out[name] = ""
		u, err := url.Parse(raw)
		if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Hostname() == "" || u.User != nil || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" {
			continue
		}
		out[name] = raw
	}
	return out
}

func PortFromListen(listen string) (int, error) {
	_, raw, err := net.SplitHostPort(listen)
	if err != nil {
		return 0, fmt.Errorf("invalid listen address %q: %w", listen, err)
	}
	port, err := strconv.Atoi(raw)
	if err != nil || port < 1 || port > 65535 {
		return 0, fmt.Errorf("invalid listen port %q", raw)
	}
	return port, nil
}

func Path(home string, port int) string {
	return filepath.Join(home, "run", strconv.Itoa(port)+".json")
}

func New(listen, mode, owner, version string) (State, error) {
	port, err := PortFromListen(listen)
	if err != nil {
		return State{}, err
	}
	if owner != "wrap" && owner != "start" {
		owner = "start"
	}
	var token [16]byte
	if _, err := rand.Read(token[:]); err != nil {

View on GitHub (pinned to 3ee70a1026)