{"record":{"id":"946eaa15908d10ee","repo":"nats-io/nats-server","slug":"account-exists","errorCode":null,"errorMessage":"account exists","messagePattern":"account exists","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/errors.go","lineNumber":102,"sourceCode":"\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.\n\tErrBadServiceType = errors.New(\"bad service response type\")\n\n\t// ErrBadSampling is returned when the sampling for latency tracking is not 1 >= sample <= 100.\n\tErrBadSampling = errors.New(\"bad sampling percentage, should be 1-100\")","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/errors.go#L84-L120","documentation":"ErrAccountExists is returned when registering an account that is already present in the server's account map. server.go:1774 (account registration) returns it if s.accounts already holds the name; server.go:1848 returns it from the system-account setup if the system account is registered twice. It protects against duplicate Account creation/registration.","triggerScenarios":"Calling s.RegisterAccount(acc) twice for the same account name; setting up the system account ($SYS) when it was already registered; resolver code re-adding an account fetched concurrently.","commonSituations":"Double-initialization paths in embedded-NATS applications (calling RegisterAccount from both bootstrap and reload); race between two goroutines registering the same account; tests reusing a server instance without resetting state.","solutions":["Look up the existing account first (s.LookupAccount) and reuse it instead of registering a new one","Skip registration if the account name is already known","Fix double-initialization so setup code runs once (sync.Once or guard flag)"],"exampleFix":"// before\nif _, err := s.RegisterAccount(acc); err != nil { return err } // may be ErrAccountExists\n// after\nif existing, _ := s.LookupAccount(acc.Name); existing == nil {\n    if _, err := s.RegisterAccount(acc); err != nil { return err }\n}","handlingStrategy":"try-catch","validationCode":"if existing, _ := s.LookupAccount(name); existing != nil { return existing } // skip registration","typeGuard":null,"tryCatchPattern":"acc, err := s.RegisterAccount(acc)\nif errors.Is(err, server.ErrAccountExists) {\n    acc, err = s.LookupAccount(name) // reuse existing\n}","preventionTips":["Guard registration with a prior LookupAccount","Initialize accounts once (sync.Once / single bootstrap path)","Watch for races: register under a lock or idempotent helper"],"tags":["nats","account","duplicate-registration"],"backgroundTag":"duplicate-resource-registration","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}