nats-io/nats-server · error

attempted to connect to wrong port

Error message

attempted to connect to wrong port

What it means

ErrConnectedToWrongPort indicates a solicited leaf-node connection reached an endpoint that is not a leaf-node acceptor: the INFO returned by the remote had no valid CID or no LeafNodeURLs, which proves the remote answered on a non-leaf port. The server logs the error and closes the connection with WrongPort (leafnode.go:1586). Typically the remote URL pointed at a client or route port instead of the leafnode port.

Source

Thrown at server/errors.go:98

	// ErrClientConnectedToRoutePort represents an error condition when a client
	// attempted to connect to the route listen port.
	ErrClientConnectedToRoutePort = errors.New("attempted to connect to route port")

	// ErrClientConnectedToLeafNodePort represents an error condition when a client
	// attempted to connect to the leaf node listen port.
	ErrClientConnectedToLeafNodePort = errors.New("attempted to connect to leaf node port")

	// ErrLeafNodeHasSameClusterName represents an error condition when a leafnode is a cluster
	// and it has the same cluster name as the hub cluster.
	ErrLeafNodeHasSameClusterName = errors.New("remote leafnode has same cluster name")

	// ErrLeafNodeDisabled is when we disable leafnodes.
	ErrLeafNodeDisabled = errors.New("leafnodes disabled")

	// ErrConnectedToWrongPort represents an error condition when a connection is attempted
	// to the wrong listen port (for instance a LeafNode to a client port, etc...)
	ErrConnectedToWrongPort = errors.New("attempted to connect to wrong port")

	// ErrAccountExists is returned when an account is attempted to be registered
	// but already exists.
	ErrAccountExists = errors.New("account exists")

	// ErrBadAccount represents a malformed or incorrect account.
	ErrBadAccount = errors.New("bad account")

	// ErrReservedAccount represents a reserved account that can not be created.
	ErrReservedAccount = errors.New("reserved account")

	// ErrMissingAccount is returned when an account does not exist.
	ErrMissingAccount = errors.New("account missing")

	// ErrMissingService is returned when an account does not have an exported service.
	ErrMissingService = errors.New("service missing")

	// ErrBadServiceType is returned when latency tracking is being applied to non-singleton response types.

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Point the leaf remote URL at the remote's leafnodes listen port (default 7422)
  2. Ensure the remote server has leafnodes { listen: ... } enabled and the port is reachable
  3. Check for port substitutions from environment templates or service discovery

Example fix

// before
leafnodes { remotes: [ { url: "nats://remote:4222" } ] }
// after
leafnodes { remotes: [ { url: "nats://remote:7422" } ] }
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(remoteURL)
if err != nil { return err }
if p := u.Port(); p == "4222" || p == "6222" {
    return fmt.Errorf("leaf remote %q must use the leafnode listen port (7422)", remoteURL)
}

Type guard

func isLeafRemotePort(u *url.URL) bool { return u.Port() == "7422" }

Try / catch

// leaf connect errors surface in the server's error callback/close reason
if strings.Contains(errStr, "wrong port") {
    log.Printf("leaf remote %s points at a non-leaf port", url)
}

Prevention

When it happens

Trigger: Configuring leafnodes { remotes: [{ url: ... }] } with a URL pointing at the client port (4222) or route port (6222) rather than the leafnodes listen port (7422); the remote accepts TCP, sends an INFO without leafnode semantics, and the check (didSolicit && (info.CID == 0 || info.LeafNodeURLs == nil)) fires.

Common situations: Copy-paste of nats://host:4222 client URLs into leafnode remote config; service discovery returning the client port; docker-compose where the leaf port is not exposed so the client port is used as a fallback.

Related errors


AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02). Data as JSON: /api/errors/00f9a5d6005164a3. Report an issue: GitHub.