{"record":{"id":"7c916c07d4fa5314","repo":"henrygd/beszel","slug":"system-exists","errorCode":null,"errorMessage":"system exists","messagePattern":"system exists","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/hub/systems/system_manager.go","lineNumber":40,"sourceCode":")\n\n// System status constants\nconst (\n\tup      string = \"up\"      // System is online and responding\n\tdown    string = \"down\"    // System is offline or not responding\n\tpaused  string = \"paused\"  // System monitoring is paused\n\tpending string = \"pending\" // System is waiting on initial connection result\n\n\t// interval is the default update interval in milliseconds (60 seconds)\n\tinterval int = 60_000\n\t// interval int = 10_000 // Debug interval for faster updates\n\n\t// sessionTimeout is the maximum time to wait for SSH connections\n\tsessionTimeout = 4 * time.Second\n)\n\n// errSystemExists is returned when attempting to add a system that already exists\nvar errSystemExists = errors.New(\"system exists\")\n\n// SystemManager manages a collection of monitored systems and their connections.\n// It handles system lifecycle, status updates, and maintains both SSH and WebSocket connections.\ntype SystemManager struct {\n\thub           hubLike                               // Hub interface for database and alert operations\n\tsystems       *store.Store[string, *System]         // Thread-safe store of active systems\n\tsshConfig     *ssh.ClientConfig                     // SSH client configuration for system connections\n\tsmartFetchMap *expirymap.ExpiryMap[smartFetchState] // Stores last SMART fetch time/result; TTL is only for cleanup\n\tctx           context.Context                       // Cancelled when the app terminates\n\tcancel        context.CancelFunc                    // Cancels ctx and all child system contexts\n}\n\n// hubLike defines the interface requirements for the hub dependency.\n// It extends core.App with system-specific functionality.\ntype hubLike interface {\n\tcore.App\n\tGetSSHKey(dataDir string) (ssh.Signer, error)\n\tHandleSystemAlerts(systemRecord *core.Record, data *system.CombinedData) error","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/henrygd/beszel/blob/b38fb7dafa60812cc22e6a84ce313e94f1ce0a32/internal/hub/systems/system_manager.go#L22-L58","documentation":"errSystemExists is a sentinel error returned by SystemManager.AddSystem when a system with the same ID is already registered in the manager's store. Beszel throws it to enforce unique system IDs, preventing duplicate SSH connections and duplicate database records for one system.","triggerScenarios":"Calling AddSystem with a System whose Id matches a system already added to the SystemManager (sm.systems.Has(sys.Id) is true).","commonSituations":"Re-running initialization code on hub restart or hot-reload; adding a host with a duplicated ID in config; a caller like AddRecord retrying after a partial failure; two components both registering the same host.","solutions":["Check sm.Has(systemId) (or remove the existing system first) before calling AddSystem","If intentional replacement, call RemoveSystem(systemID) then AddSystem(sys)","Make the incoming System.Id unique in your config or generation logic","Treat the sentinel error as idempotent success if the existing system matches the desired one"],"exampleFix":"// before\nif err := hub.AddSystem(sys); err != nil {\n    return err\n}\n// after\nif err := hub.AddSystem(sys); err != nil {\n    if errors.Is(err, systems.ErrSystemExists) { // sentinel: already registered\n        return nil // idempotent\n    }\n    return err\n}","handlingStrategy":"validation","validationCode":"if manager.Has(sys.Id) {\n    return nil // or remove-and-readd if replacement is intended\n}\nif err := manager.AddSystem(sys); err != nil {\n    return err\n}","typeGuard":null,"tryCatchPattern":"if err := manager.AddSystem(sys); err != nil {\n    if errors.Is(err, errSystemExists) { /* already registered; skip or replace */ }\n    return err\n}","preventionTips":["Check existence before adding","Make system IDs unique in config (e.g. hostname-based, not index-based)","Guard hot-reload/retry paths so they don't re-add systems"],"tags":["go","duplicate-key","system-manager","state"],"backgroundTag":"duplicate-entity-registration","analyzedSha":"b38fb7dafa60812cc22e6a84ce313e94f1ce0a32","analyzedAt":"2026-08-31T15:10:10.149Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}