{"record":{"id":"00f9a5d6005164a3","repo":"nats-io/nats-server","slug":"attempted-to-connect-to-wrong-port","errorCode":null,"errorMessage":"attempted to connect to wrong port","messagePattern":"attempted to connect to wrong port","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/errors.go","lineNumber":98,"sourceCode":"\n\t// ErrClientConnectedToRoutePort represents an error condition when a client\n\t// attempted to connect to the route listen port.\n\tErrClientConnectedToRoutePort = errors.New(\"attempted to connect to route port\")\n\n\t// ErrClientConnectedToLeafNodePort represents an error condition when a client\n\t// attempted to connect to the leaf node listen port.\n\tErrClientConnectedToLeafNodePort = errors.New(\"attempted to connect to leaf node port\")\n\n\t// ErrLeafNodeHasSameClusterName represents an error condition when a leafnode is a cluster\n\t// and it has the same cluster name as the hub cluster.\n\tErrLeafNodeHasSameClusterName = errors.New(\"remote leafnode has same cluster name\")\n\n\t// ErrLeafNodeDisabled is when we disable leafnodes.\n\tErrLeafNodeDisabled = errors.New(\"leafnodes disabled\")\n\n\t// ErrConnectedToWrongPort represents an error condition when a connection is attempted\n\t// to the wrong listen port (for instance a LeafNode to a client port, etc...)\n\tErrConnectedToWrongPort = errors.New(\"attempted to connect to wrong port\")\n\n\t// ErrAccountExists is returned when an account is attempted to be registered\n\t// but already exists.\n\tErrAccountExists = errors.New(\"account exists\")\n\n\t// ErrBadAccount represents a malformed or incorrect account.\n\tErrBadAccount = errors.New(\"bad account\")\n\n\t// ErrReservedAccount represents a reserved account that can not be created.\n\tErrReservedAccount = errors.New(\"reserved account\")\n\n\t// ErrMissingAccount is returned when an account does not exist.\n\tErrMissingAccount = errors.New(\"account missing\")\n\n\t// ErrMissingService is returned when an account does not have an exported service.\n\tErrMissingService = errors.New(\"service missing\")\n\n\t// ErrBadServiceType is returned when latency tracking is being applied to non-singleton response types.","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/errors.go#L80-L116","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Point the leaf remote URL at the remote's leafnodes listen port (default 7422)","Ensure the remote server has leafnodes { listen: ... } enabled and the port is reachable","Check for port substitutions from environment templates or service discovery"],"exampleFix":"// before\nleafnodes { remotes: [ { url: \"nats://remote:4222\" } ] }\n// after\nleafnodes { remotes: [ { url: \"nats://remote:7422\" } ] }","handlingStrategy":"validation","validationCode":"u, err := url.Parse(remoteURL)\nif err != nil { return err }\nif p := u.Port(); p == \"4222\" || p == \"6222\" {\n    return fmt.Errorf(\"leaf remote %q must use the leafnode listen port (7422)\", remoteURL)\n}","typeGuard":"func isLeafRemotePort(u *url.URL) bool { return u.Port() == \"7422\" }","tryCatchPattern":"// leaf connect errors surface in the server's error callback/close reason\nif strings.Contains(errStr, \"wrong port\") {\n    log.Printf(\"leaf remote %s points at a non-leaf port\", url)\n}","preventionTips":["Use dedicated config variables for leaf remote URLs","Expose the leafnode port explicitly in container/orchestrator configs","Test leaf links in CI so wrong-port configs fail early"],"tags":["nats","leafnode","wrong-port","configuration"],"backgroundTag":"wrong-port-connection","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}