{"record":{"id":"6fb1ee3ccfab75d2","repo":"TechnitiumSoftware/DnsServer","slug":"dns-server-is-already-running","errorCode":null,"errorMessage":"DNS Server is already running.","messagePattern":"DNS Server is already running\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/DnsServer.cs","lineNumber":6637,"sourceCode":"            {\n                if (!Socket.OSSupportsIPv6)\n                    throw new DnsServerException(protocolName + \" requires IPv6 support on the system to work.\");\n\n                throw new DnsServerException(protocolName + \" is supported only on Windows 11 (build 22000 and later), Windows Server 2022 (and later), and Linux. On Linux, you must install 'libmsquic' manually.\");\n            }\n        }\n\n        #endregion\n\n        #region public\n\n        public async Task StartAsync(bool throwIfBindFails = false)\n        {\n            if (_disposed)\n                ObjectDisposedException.ThrowIf(_disposed, this);\n\n            if (_state != ServiceState.Stopped)\n                throw new InvalidOperationException(\"DNS Server is already running.\");\n\n            _state = ServiceState.Starting;\n\n            //bind on all local end points\n            foreach (IPEndPoint localEP in _localEndPoints)\n            {\n                Socket udpListener = null;\n\n                try\n                {\n                    udpListener = GetUdpListenerSocket(localEP.AddressFamily);\n\n                    if ((localEP is InterfaceEndPoint intEP) && (intEP.InterfaceName is not null) && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))\n                    {\n                        try\n                        {\n                            udpListener.SetRawSocketOption(SOL_SOCKET, SO_BINDTODEVICE, Encoding.ASCII.GetBytes(intEP.InterfaceName));\n                            _log.Write(localEP, DnsTransportProtocol.Udp, $\"Socket was bound to device '{intEP.InterfaceName}' successfully.\");","sourceCodeStart":6619,"sourceCodeEnd":6655,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/DnsServer.cs#L6619-L6655","documentation":"Thrown by StartAsync when _state is not ServiceState.Stopped (i.e. already Starting, Running, or Stopping). The server is a single-instance state machine and cannot be started twice; the guard prevents overlapping listener binds.","triggerScenarios":"Calling StartAsync a second time while the server is running or still starting; a restart routine that calls Start without waiting for StopAsync to complete; double-invoke from a UI button race.","commonSituations":"Host that auto-restarts on config change without stopping first; supervisor script firing twice; the previous StopAsync is still in progress (Stopping state) when Start is invoked.","solutions":["Call StopAsync and await it fully before calling StartAsync again.","Guard the call: only start when State == ServiceState.Stopped.","Serialize start/stop with a lock or a single owner to prevent concurrent transitions.","For config changes, use the in-process reload APIs rather than stop+start."],"exampleFix":"// before\nserver.StartAsync();\n// ... later, without stopping:\nserver.StartAsync();  // throws\n\n// after\nif (server.State != ServiceState.Stopped)\n    await server.StopAsync();\nawait server.StartAsync();","handlingStrategy":"validation","validationCode":"async Task SafeStartAsync(DnsServer server)\n{\n    if (server.State != ServiceState.Stopped) await server.StopAsync();\n    await server.StartAsync();\n}","typeGuard":"static bool IsStopped(IDnsServer s) => s.State == ServiceState.Stopped;","tryCatchPattern":"try { await server.StartAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already running\")) { await server.StopAsync(); await server.StartAsync(); }","preventionTips":["Always fully await StopAsync before StartAsync.","Serialize start/stop transitions with a lock.","Use in-process reload APIs for config changes."],"tags":["lifecycle","start","state-machine","concurrency"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}