Search code examples
c#dockersocketsswaggermicroservices

How to do a proper cleanup of TCP socket connections


As explained this morning, I'm dealing with unstable TCP socket communication. I thought this might be related to multithreading, but now I'm going on another track: when my application is finished, there still seem to be some sockets open, as you can see from this netstat result ("10.1.0.160" is the IP address of the remote machine I'm connecting to and the status equals TIME_WAIT or ESTABLISHED):

Prompt> netstat -aon | findstr "10.1.0.160"
  TCP    My_IP:56246      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56247      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56248      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56249      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56250      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56251      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56252      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56253      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56254      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56255      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56256      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56257      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56258      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56259      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56260      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56261      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56262      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:56263      10.1.0.160:50001       TIME_WAIT       0
  TCP    My_IP:57185      10.1.0.160:3389        ESTABLISHED     31220
  TCP    My_IP:61121      10.1.0.160:445         ESTABLISHED     4

In order to solve this, I first thought of implementing a destructor, but apparently this is not called, so I went declaring my TCP socket related classes as implementing the IDisposable interface, more or less as follows (for the class, containing the System.Net.Sockets.Socket):

public void Dispose()
{
    Debug.WriteLine(
      $"T[{System.Threading.Thread.CurrentThread.ManagedThreadId}], " + 
      $"{DateTime.UtcNow}: TcpConnection.Dispose()");
    StateChanged = null; // this event is called when the state changes.
    Disconnect(); // this contains "socket.Close();"
}

However, when I close my application, this Dispose() is not called.
My application is a Swagger handled microservice. In order to close it, I just close my Swagger window.

What can I do in order to get the Dispose() methods called when closing the Swagger browser?

Thanks in advance


Solution

  • Try to use using statement in C#. It'll automatically call dispose() method when the object goes out of scope.

    Here is an example:

    using (var swaggerBrowser = new SwaggerBrowser())
    {
        // Use the Swagger browser here
    }