[CSHARP-1704] TcpStreamFactory is leaking socket handlers on exception Created: 12/Jul/16  Updated: 23/Sep/16  Resolved: 10/Aug/16

Status: Closed
Project: C# Driver
Component/s: Connectivity, Error Handling
Affects Version/s: 2.2.4
Fix Version/s: 2.3

Type: Bug Priority: Major - P3
Reporter: Šar?nas Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

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(); 
        }



 Comments   
Comment by Githook User [ 11/Aug/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1704: disposing of sockets in failure scenarios.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/25d1ee88621f82ceebd771d6dae814713c2fefc7

Comment by Githook User [ 11/Aug/16 ]

Author:

{u'username': u'floatas', u'name': u'floatas', u'email': u'floatas@outlook.com'}

Message: CSHARP-1704: TcpStreamFactory is leaking socket handlers on exception
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/5498c7017c34f3762a5e3c5757b485be2d173e9e

Comment by Githook User [ 10/Aug/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1704: disposing of sockets in failure scenarios.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/25d1ee88621f82ceebd771d6dae814713c2fefc7

Comment by Githook User [ 10/Aug/16 ]

Author:

{u'username': u'floatas', u'name': u'floatas', u'email': u'floatas@outlook.com'}

Message: CSHARP-1704: TcpStreamFactory is leaking socket handlers on exception
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/5498c7017c34f3762a5e3c5757b485be2d173e9e

Generated at Wed Feb 07 21:40:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.