DnsServer.Stop() doesn't stop?

Dec 12, 2013 at 10:37 PM
First, big thanks, I need to implement a captive portal over wi-fi on a kiosk and this library made it nearly trivial. But I do face one issue. When I call Stop() on the DnsServer, it doesn't stop. netstat -an shows it still listening on port 53 (both UDP and TCP). It seems to be in a spin loop because half the CPU stays pegged after that. Thoughts?
Dec 13, 2013 at 4:48 PM
Edited Dec 13, 2013 at 4:52 PM
I think the CPU hogging is a separate problem. Even without calling Stop() it happens, with a ridiculously deep call stack
    System.dll!System.Net.Sockets.Socket.BeginReceiveFrom(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP, System.AsyncCallback callback, object state) + 0x1fb bytes 
    ARSoft.Tools.Net.dll!ARSoft.Tools.Net.Socket.UdpListener.BeginReceive(System.AsyncCallback callback, object state) + 0xfe bytes 
    ARSoft.Tools.Net.dll!ARSoft.Tools.Net.Dns.DnsServer.StartUdpListen() + 0xa5 bytes   
    ARSoft.Tools.Net.dll!ARSoft.Tools.Net.Dns.DnsServer.HandleUdpException(System.Exception e) + 0x77 bytes 
    ARSoft.Tools.Net.dll!ARSoft.Tools.Net.Dns.DnsServer.StartUdpListen() + 0x121 bytes  
    ARSoft.Tools.Net.dll!ARSoft.Tools.Net.Dns.DnsServer.HandleUdpException(System.Exception e) + 0x77 bytes 
    ... many many more HandleUdpException/StartUdpListen pairs removed ...
    ARSoft.Tools.Net.dll!ARSoft.Tools.Net.Dns.DnsServer.HandleUdpException(System.Exception e) + 0x77 bytes 
    ARSoft.Tools.Net.dll!ARSoft.Tools.Net.Dns.DnsServer.EndUdpReceive(System.IAsyncResult ar) + 0x935 bytes 
    ARSoft.Tools.Net.dll!ARSoft.Tools.Net.Socket.UdpListener.OnSocketCallback(System.IAsyncResult asyncResult) + 0x66 bytes 
    System.dll!System.Net.LazyAsyncResult.Complete(System.IntPtr userToken) + 0x6c bytes    
    System.dll!System.Net.ContextAwareResult.CompleteCallback(object state) + 0x1a bytes    
    mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object userData) + 0x51 bytes 
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6a bytes    
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0x7e bytes    
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes    
    System.dll!System.Net.ContextAwareResult.Complete(System.IntPtr userToken) + 0x91 bytes 
    System.dll!System.Net.LazyAsyncResult.ProtectedInvokeCallback(object result, System.IntPtr userToken) + 0xbb bytes  
    System.dll!System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* nativeOverlapped) + 0x114 bytes    
    mscorlib.dll!System.Threading._IOCompletionCallback.PerformIOCompletionCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* pOVERLAP) + 0x74 bytes    
    [Native to Managed Transition]  
Possibly triggered by wi-fi disconnect and the interface it is listening on going down.
Dec 13, 2013 at 10:09 PM
Okay I don't know whether or not this is the best way to solve this but I told both exception handlers if it's an ObjectDisposedException then don't restart the listener. Fixed both Stop() not working and the CPU hogging.
        private void HandleUdpException(Exception e)
        {
            if (!(e is ObjectDisposedException))
            {
                lock (_udpListener)
                {
                    _availableUdpListener++;
                }
                StartUdpListen();
            }

            OnExceptionThrown(e);
        }

        private void HandleTcpException(Exception e, NetworkStream stream, TcpClient client)
        {
            try
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            catch {}

            try
            {
                if (client != null)
                {
                    client.Close();
                }
            }
            catch {}

            if (!(e is ObjectDisposedException))
            {
                lock (_tcpListener)
                {
                    _availableTcpListener++;
                }
                StartTcpAcceptConnection();
            }

            if (e != null)
                OnExceptionThrown(e);
        }