1

I'm trying to find strange behaviour in a socket library that has been used in production for a while.

Actually the Receive and send are processed with BeginReceive/EndReceive and BeginSend/EndSend calls , so everything is processed in an asynchronous fashion.

Moreover, I've a MonitorThread that is polling each second for the state of the socket:

void ControlThreadCallback()
{
    if (!IsConnected && m_ConnectionIP != null && !m_ConnectionIP.Equals(String.Empty) && m_ConnectionPort > 0)
    {
        //No connected, perform reconnection, recreate serversocket or dispose client socket if needed 
    }
}

The IsConnected property is defined in the same class:

public bool IsConnected
{
    get
    {
        try
        {
            return m_Socket != null && m_Socket.Connected &&
            !(m_Socket.Poll(1, SelectMode.SelectRead) && m_Socket.Available == 0);
        }
        catch (Exception ex)
        {
            //Log error
            //Object Disposed exception is being catched here sometimes
            return false;
        }
    }
}

Sometimes, objectdisposedexception is being catched, thus returning false to IsConnected property and ControlThread thinks that socket is not connected. However, I've activity in the socket, so is not disconnected at all.

This happens only when high volume of data is being sent and received on the socket (that is, more than 20 messages each second or so).

In which strange cases the Socket.Poll is giving objectDisposedException? and, could be some race condition between socket.Poll && socket.Available?

If you need more code I can paste some more snippets.

najarciyo
  • 11
  • 1
  • 1
  • ODE only ever means one thing, you called the Socket.Close() or Dispose() methods. You have a "dispose client socket if needed" comment, surely that created a threading race bug. – Hans Passant Jul 06 '16 at 09:37
  • Have you ever study Master/Slave relationshsips? Using Async method with master slave can cause issues. A client is a master and and a server a slave. The master sends commands and the server responds to the commands. So when working asynchronously you must have an algorithm to handle responses that don't come in the same order than you sent the commands, or have message queue that expects responses to get returned in same order that they are sent. – jdweng Jul 06 '16 at 09:43
  • @HansPassant Thanks for your suggestion. Actually, having this only on high traffic could be because of a race condition. I'm trying to have a look focused on this. – najarciyo Jul 06 '16 at 12:20
  • @jdweng Actually i'm managing this relationships with some waitHandles, but this is working as expected. – najarciyo Jul 06 '16 at 12:21
  • I'm not sure he relationship are correct. Make sure you are getting responses in correct order under high volume. TCP on windows is known for high volume issues. Check for memory leak on both server and client. Under high volume TCP will resend packets because of missing acks (timeout issues the acks come back late). Then once resends occur which add to traffic further reducing bandwidth causing more resends. Eventually the table that tracks acks becomes huge and PC crashes. – jdweng Jul 06 '16 at 12:55
  • @jdweng there are no mem leaks on the whole soft nor crashes, so based on all your comments, the only way of having od exception is actually calling dispose. I'll check for possible race conditions out of the scope of the question.. seems strange but why not? – najarciyo Jul 07 '16 at 05:25

0 Answers0