JuliusBrussee/caveman · error

invalid listen address

Error message

invalid listen address %q: %w

What it means

PortFromListen parses a 'host:port' listen address and returns this error when net.SplitHostPort cannot split the string. It means the listen address is not in a recognizable host:port form, so no port can be extracted. The original SplitHostPort error is wrapped for detail.

Solutions

  1. Format the listen address as host:port, e.g. '127.0.0.1:8080' or '[::1]:8080' for IPv6
  2. Wrap IPv6 hosts in square brackets when a port follows
  3. Remove extra colons or whitespace from the listen value
  4. Check the config file/env var that supplies the listen address

Example fix

// before
port, err := runstate.PortFromListen("8080")
// after
port, err := runstate.PortFromListen("127.0.0.1:8080")
Defensive patterns

Strategy: validation

Validate before calling

func validListen(s string) bool {
    _, port, err := net.SplitHostPort(s)
    if err != nil { return false }
    p, err := strconv.Atoi(port)
    return err == nil && p >= 1 && p <= 65535
}

Try / catch

port, err := runstate.PortFromListen(listen)
if err != nil {
    var addrErr *net.AddrError
    if errors.As(err, &addrErr) { return fmt.Errorf("fix listen %q: %v", listen, addrErr) }
    return err
}

Prevention

When it happens

Trigger: Passing strings like '8080' (no colon), 'host:port:notaport' (too many colons for a non-bracketed host), '[::1' (unclosed bracket), or an empty string to PortFromListen, or to New/read/runStatus which call it.

Common situations: Config files with a bare port number instead of host:port, IPv6 addresses written without brackets, or environment-supplied listen values with typos or stray whitespace.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

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

	if upstreams == nil {
		return nil
	}
	out := make(map[string]string, len(upstreams))
	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" {

View on GitHub (pinned to 3ee70a1026)