[CSHARP-1798] MongoConnection.Close() should always call TcpClient.Close() Created: 12/Oct/16  Updated: 29/Oct/17  Resolved: 29/Oct/17

Status: Closed
Project: C# Driver
Component/s: Connectivity
Affects Version/s: 1.11
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Andrew Young Assignee: Robert Stam
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Case:

 Description   

TcpClient creates a socket when the TcpClient is first constructed but doesn't open it right away.

So it might be possible for a disconnected TcpClient to hold on to an ephemeral port even though it's not connected.

Because of this, MongoConnection.Close() should always call TcpClient.Close().



 Comments   
Comment by Robert Stam [ 29/Oct/17 ]

The 1.x versions are no longer under development.

The 2.x versions no longer use TcpClient so are not affected by this issue.

Comment by Andrew Young [ 12/Oct/16 ]

Email from Microsoft regarding the issue:


This is the area of concern inside Mongo driver:

internal void Close()
{
  lock (this._connectionLock)
  {
    if (this._state == MongoConnectionState.Closed)
      return;
    if (this._stream != null)
    {
      try
      {
        this._stream.Close();
      }
      catch
      {
      }
      this._stream = (Stream) null;
    }
    if (this._tcpClient != null)
    {
      **if (this._tcpClient.Connected)
      {
        try
        {
          this._tcpClient.Close();**
        }
        catch
        {
        }
      }
      this._tcpClient = (TcpClient) null;
    }
    this._state = MongoConnectionState.Closed;
  }
}

This is MongoConnection.Close() code. Note this is calling TcpClient.Close() ONLY if TcpClient is Connected….

Otherwise, it’ll just null it out – which then would end up with TcpClient on Finalizer queue.

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