Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1704

TcpStreamFactory is leaking socket handlers on exception

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 2.3
    • 2.2.4
    • None
    • Windows 2012\2008 server

    Description

      TcpStreamFactory both connect methods throw exception without disposing socket. Sockets collect in finalizer queue and stays there. After some time undisposed sockets consume all ephemeral ports.

      Connect 103 line.
      ConnectAsync 149 line.

      Can be reproduced with this snippet:

      static void Main(string[] args) 
              {  
                  Task.Run(() => 
                  { 
                      var listener = new TcpListener(IPAddress.Any, 15547); 
                      listener.Start(); 
                      var cc = new List<TcpClient>(); 
                      while (true
                      { 
                          cc.Add(listener.AcceptTcpClient()); 
                          Console.WriteLine("accepted"); 
                      } 
                  }); 
                  List<TcpClient> clients = new List<TcpClient>(); 
                  int count = 1000000; 
                  Parallel.ForEach(Enumerable.Range(0, count), v => 
                  { 
                      while (true
                      { 
                          try 
                          { 
                              var client = new TcpClient(); 
                              client.Connect("localhost", 15547); 
                              clients.Add(client); 
                              break
                          } 
                          catch (Exception e) 
                          { 
                              Console.WriteLine("clap"); 
                              Task.Delay(TimeSpan.FromSeconds(1)).Wait(); 
                          } 
                      } 
                  }); 
                  Console.ReadLine(); 
              }
      

      Attachments

        Activity

          People

            craig.wilson@mongodb.com Craig Wilson
            floatas Šar?nas
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: