Socket closure on shutdown

Topics: Developer Forum, Project Management Forum
Feb 25, 2009 at 5:14 AM
Hello,

We've just started looking at SharedCache since Velocity's taking their time- it's great!

We noticed that when restarting the service, it seems to silently fail on occasion. This is due to the Socket connection not being explicitly closed at shutdown in TcpServer.StartListening. If you just wrap the Socket in a using block, all is well.

Are you accepting patches? We'd be happy to submit a fix. We also have some other ideas we'll be implementing locally (client IP whitelist), and we'd be happy to contribute them back as well...

Thanks,

-Matt Davis
Coordinator
Feb 25, 2009 at 5:34 AM
Hi Matt,

thanks for your feedback.

so far we did not received any patches from the community because most of the those who check what happens under the hood they have sent me the code directly so far and i have integrated it. you can send me your details over codeplex details and we can get in touch over msn / gtalk to discuss your features.

regards, roni
Coordinator
Feb 25, 2009 at 7:16 PM
hi matt, for a short term, can you you post here to code for handing this socket issue? i will take it over and release it with 3.0.5.1

thanks, roni    
Feb 25, 2009 at 7:46 PM
Sure. Here's the entire text of TcpServer.StartListening- the only change is wrapping the Socket in a using block so it gets positively disposed on shutdown:

        private void StartListening()
        {
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, this.cacheIpPort);
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                try
                {
                    socket.Bind(endPoint);
                    socket.Listen(this.maxSockets);

                    string msg = string.Format(@"TCP - Listener started on: {0}:{1}", this.cacheIpAdress, this.cacheIpPort);
                    COM.Handler.LogHandler.Force(msg);
                    COM.Handler.LogHandler.Traffic(msg);
                    Console.WriteLine(msg);

                    AcceptConnection = new WaitCallback(AcceptConnection_Handler);

                    while (!this.ShuttingDownServer)
                    {
                        this.allDone.Reset();
                        // start async socket listener for connections
                        // socket.BeginAccept(new AsyncCallback(this.AccecptCallback), socket);

                        this.threadPool.PostRequest(this.AcceptConnection, new object[] { socket });

                        //ThreadPool.QueueUserWorkItem(AcceptConnection, socket);
                        // wait until the connection is established befor we continue
                        this.allDone.WaitOne();
                    }
                }
                catch (SocketException sEx)
                {
                    #region Socket exception handling
                    threadEnd[0].Set();

                    COM.Handler.LogHandler.Fatal(string.Format("Could not bind enpoint {0}:{1} - Socket Excpetion No: {2}", endPoint.Address, endPoint.Port, sEx.ErrorCode));
                    COM.Handler.LogHandler.Error(string.Format("Could not bind enpoint {0}:{1} - Socket Excpetion No: {2}", endPoint.Address, endPoint.Port, sEx.ErrorCode));
                    Console.WriteLine("");
                    Console.WriteLine(@"FATAL ERROR:");
                    Console.WriteLine("");
                    Console.WriteLine("Server cannot start to listen because it could not bind your requested enpoint {0}:{1} - Socket Excpetion No: {2}", endPoint.Address, endPoint.Port, sEx.ErrorCode);
                    Console.WriteLine("use the command: 'netstat -a' to evaluate which current usage of port: {0}", endPoint.Port);
                    #endregion Socket exception handling
                }
                catch (Exception ex)
                {
                    #region Regular Exception Handling
                    threadEnd[0].Set();
                    COM.Handler.LogHandler.Fatal(string.Format("Could not bind enpoint {0}:{1} - Excpetion Message: {2}", endPoint.Address, endPoint.Port, ex.Message));
                    COM.Handler.LogHandler.Error(string.Format("Could not bind enpoint {0}:{1} - Excpetion Message: {2}", endPoint.Address, endPoint.Port, ex.Message));
                    Console.WriteLine("");
                    Console.WriteLine(@"FATAL ERROR:");
                    Console.WriteLine("");
                    Console.WriteLine("Could not bind enpoint {0}:{1} - Excpetion Message: {2}", endPoint.Address, endPoint.Port, ex.Message);
                    #endregion Regular Exception Handling
                }
            }
        }

Coordinator
Feb 27, 2009 at 3:19 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.