{"record":{"id":"3d318482f8c367e7","repo":"TechnitiumSoftware/DnsServer","slug":"dhcp-server-is-already-running","errorCode":null,"errorMessage":"DHCP Server is already running.","messagePattern":"DHCP Server is already running\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dhcp/DhcpServer.cs","lineNumber":1398,"sourceCode":"            _maintenanceTimer.Change(MAINTENANCE_TIMER_INTERVAL, Timeout.Infinite);\n        }\n\n        private void StopMaintenanceTimer()\n        {\n            _maintenanceTimer.Change(Timeout.Infinite, Timeout.Infinite);\n        }\n\n        #endregion\n\n        #region public\n\n        public void Start()\n        {\n            if (_disposed)\n                ObjectDisposedException.ThrowIf(_disposed, this);\n\n            if (_state != ServiceState.Stopped)\n                throw new InvalidOperationException(\"DHCP Server is already running.\");\n\n            _state = ServiceState.Starting;\n\n            LoadAllScopeFiles();\n            StartMaintenanceTimer();\n\n            _state = ServiceState.Running;\n        }\n\n        public void Stop()\n        {\n            if (_state != ServiceState.Running)\n                return;\n\n            _state = ServiceState.Stopping;\n\n            StopMaintenanceTimer();\n","sourceCodeStart":1380,"sourceCodeEnd":1416,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dhcp/DhcpServer.cs#L1380-L1416","documentation":"Start() refuses to run because the service is not in the Stopped state (it is Starting/Running/Stopping). This is an InvalidOperationException, not a DhcpServerException, signalling a lifecycle misuse.","triggerScenarios":"Calling DhcpServer.Start() when _state != ServiceState.Stopped (e.g. already Running, or mid-Start).","commonSituations":"Double Start() call. Service controller restart that calls Start before Stop completed. UI button that starts while starting.","solutions":["Call Stop() and wait until state == Stopped before calling Start().","Guard the caller: only Start() when _state == Stopped.","Audit for concurrent/re-entrant Start() invocations."],"exampleFix":"// before\nif (_dhcpServer.State != ServiceState.Running) _dhcpServer.Start(); // can throw if Starting\n// after\n_dhcpServer.Stop();\nwhile (_dhcpServer.State != ServiceState.Stopped) Thread.Yield();\n_dhcpServer.Start();","handlingStrategy":"validation","validationCode":"if (_dhcpServer.State != ServiceState.Stopped)\n    throw new InvalidOperationException(\"DHCP server must be stopped before Start()\");","typeGuard":"static bool CanStart(DhcpServer s) => s.State == ServiceState.Stopped;","tryCatchPattern":"try { _dhcpServer.Start(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already running\"))\n{ /* already started; no-op or Stop()+Start() */ }","preventionTips":["Always pair Start() with a completed Stop().","Expose State in the UI to disable Start while not Stopped.","Serialize lifecycle transitions to avoid re-entrant Start()."],"tags":["dhcp","lifecycle","startup","concurrency"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}