Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Fix memory leak when replacing invalid connections on multi threaded environment. #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions source/MongoDB/Connections/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ internal ReplyMessage<T> SendTwoWayMessageCore<T>(IRequestMessage message, BsonR
{
EnsureOpenConnection();

try
{
var reply = new ReplyMessage<T>(readerSettings);
lock(_connection)
{
message.Write(_connection.GetStream());
reply.Read(_connection.GetStream());
}
return reply;
}
catch(IOException)
lock(_connection)
{
ReplaceInvalidConnection();
throw;
try
{
var reply = new ReplyMessage<T>(readerSettings);
message.Write(_connection.GetStream());
reply.Read(_connection.GetStream());
return reply;
}
catch(IOException)
{
ReplaceInvalidConnection();
throw;
}
}
}

Expand All @@ -133,20 +133,20 @@ public void SendMessage(IRequestMessage message, string database){
internal void SendMessageCore(IRequestMessage message)
{
EnsureOpenConnection();

try
{
lock(_connection)
{
message.Write(_connection.GetStream());
}
}
catch(IOException)
lock(_connection)
{
//Sending doesn't seem to always trigger the detection of a closed socket.
ReplaceInvalidConnection();
throw;
try
{
message.Write(_connection.GetStream());
}
catch(IOException)
{
//Sending doesn't seem to always trigger the detection of a closed socket.
ReplaceInvalidConnection();
throw;
}
}

}

/// <summary>
Expand Down