vitessio/vitess · error

SplitHostPort: can't parse port %q: %v

Error message

SplitHostPort: can't parse port %q: %v

What it means

Returned by netutil.SplitHostPort when the port portion of a host:port string cannot be parsed, wrapping the underlying error with the offending port value.

Source

Thrown at go/netutil/netutil.go:46

)

// SplitHostPort is an alternative to net.SplitHostPort that also parses the
// integer port. In addition, it is more tolerant of improperly escaped IPv6
// addresses, such as "::1:456", which should actually be "[::1]:456".
func SplitHostPort(addr string) (string, int, error) {
	host, port, err := net.SplitHostPort(addr)
	if err != nil {
		// If the above proper parsing fails, fall back on a naive split.
		i := strings.LastIndex(addr, ":")
		if i < 0 {
			return "", 0, fmt.Errorf("SplitHostPort: missing port in %q", addr)
		}
		host = addr[:i]
		port = addr[i+1:]
	}
	p, err := strconv.ParseUint(port, 10, 16)
	if err != nil {
		return "", 0, fmt.Errorf("SplitHostPort: can't parse port %q: %v", port, err)
	}
	return host, int(p), nil
}

// JoinHostPort is an extension to net.JoinHostPort that also formats the
// integer port.
func JoinHostPort(host string, port int32) string {
	return net.JoinHostPort(host, strconv.FormatInt(int64(port), 10))
}

// FullyQualifiedHostname returns the FQDN of the machine.
func FullyQualifiedHostname() (string, error) {
	// The machine hostname (which is also returned by os.Hostname()) may not be
	// set to the FQDN, but only the first part of it e.g. "localhost" instead of
	// "localhost.localdomain".
	// To get the full FQDN, we do the following:

	// 1. Get the machine hostname. Example: localhost

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Correct the port to a numeric value in 1-65535 range
  2. Check the config source (flag, env, file) for the malformed address
  3. Log the full address being parsed to find where the bad value originates
  4. Use netutil.JoinHostPort with an int port when building addresses

Example fix

// before
netutil.SplitHostPort("localhost:mysql")
// after
netutil.SplitHostPort("localhost:3306")
Defensive patterns

Strategy: validation

Validate before calling

func validatePort(addr string) error {
    _, port, ok := strings.Cut(addr, ":")
    if !ok {
        return fmt.Errorf("missing port in %q", addr)
    }
    n, err := strconv.Atoi(port)
    if err != nil || n < 1 || n > 65535 {
        return fmt.Errorf("invalid port %q in %q", port, addr)
    }
    return nil
}

Try / catch

host, port, err := netutil.SplitHostPort(addr)
if err != nil {
    return fmt.Errorf("cannot parse address %q: %w", addr, err)
}

Prevention

When it happens

Trigger: Calling netutil.SplitHostPort with an address whose port portion is invalid, e.g. "localhost:abc", "host:99999", "host:" or an IPv6 bracket form mishandled by the naive split.

Common situations: Typos in configured addresses; port fields containing service names like "mysql" instead of numbers; truncated config strings; stray characters after the port.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/83ded6736d2249cc. Report an issue: GitHub.