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

TcpStreamFactory is leaking socket handlers on exception

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.3
    • Affects Version/s: 2.2.4
    • Labels:
      None
    • Environment:
      Windows 2012\2008 server

      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:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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(); 
              }
      

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

              Created:
              Updated:
              Resolved: