{"record":{"id":"034f217f0bf141bc","repo":"henrygd/beszel","slug":"system-not-found","errorCode":null,"errorMessage":"system not found","messagePattern":"system not found","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/hub/systems/system_manager.go","lineNumber":279,"sourceCode":"\n\t// Initialize system for monitoring\n\tsys.manager = sm\n\tsys.ctx, sys.cancel = sys.getContext(sm.ctx)\n\tsys.data = &system.CombinedData{}\n\tsm.systems.Set(sys.Id, sys)\n\n\t// Start monitoring in background\n\tgo sys.StartUpdater()\n\treturn nil\n}\n\n// RemoveSystem removes a system from the manager and cleans up all associated resources.\n// It cancels the system's context, closes all connections, and removes it from the store.\n// Returns an error if the system is not found.\nfunc (sm *SystemManager) RemoveSystem(systemID string) error {\n\tsystem, ok := sm.systems.GetOk(systemID)\n\tif !ok {\n\t\treturn errors.New(\"system not found\")\n\t}\n\n\t// Stop the update goroutine\n\tif system.cancel != nil {\n\t\tsystem.cancel()\n\t}\n\n\t// Clean up all connections\n\tsystem.closeSSHConnection()\n\tsystem.closeWebSocketConnection()\n\tsm.systems.Remove(systemID)\n\treturn nil\n}\n\n// AddRecord creates a System instance from a database record and adds it to the manager.\n// If a system with the same ID already exists, it's removed first to ensure clean state.\n// If no system instance is provided, a new one is created.\n// This method is typically called when systems are created or their status changes to pending.","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/henrygd/beszel/blob/b38fb7dafa60812cc22e6a84ce313e94f1ce0a32/internal/hub/systems/system_manager.go#L261-L297","documentation":"Returned by RemoveSystem when the given systemID does not exist in the manager's store (sm.systems.GetOk fails). It signals the cleanup request referenced a system that was already removed or never added.","triggerScenarios":"Calling RemoveSystem with an ID that was never added, or that was already removed (e.g. double invocation from onRecordAfterDeleteSuccess and RemoveAllSystems, or a stale ID after a token rotation).","commonSituations":"Race between two deletion paths removing the same system; deleting a record whose system was already purged; mistyped/stale system ID in code or scripts.","solutions":["Check existence (sm.Has(systemID)) before removing, or treat 'not found' as success","Deduplicate removal paths so a system is removed exactly once","Refresh the system ID from the current record/store instead of a cached value"],"exampleFix":"// before\nif err := sm.RemoveSystem(id); err != nil {\n    return err\n}\n// after\nif err := sm.RemoveSystem(id); err != nil && !errors.Is(err, systems.ErrSystemNotFound) {\n    return err // already removed is fine\n}","handlingStrategy":"try-catch","validationCode":"if !manager.Has(systemID) {\n    return nil // nothing to remove\n}","typeGuard":null,"tryCatchPattern":"if err := manager.RemoveSystem(id); err != nil {\n    if strings.Contains(err.Error(), \"system not found\") { return nil }\n    return err\n}","preventionTips":["Centralize removal in one code path to avoid double-removal races","Track system IDs from the live store, not stale caches","Treat not-found on delete as idempotent success"],"tags":["go","not-found","system-manager","state"],"backgroundTag":"entity-not-found","analyzedSha":"b38fb7dafa60812cc22e6a84ce313e94f1ce0a32","analyzedAt":"2026-08-31T15:10:10.149Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}