Permalink
Browse files

Update source/MongoDB/Connections/Connection.cs

make ReplaceInvalidCollection done while holding lock

Without holding lock, there is race condition between threads on replacing connections, i.e. "_connection"s created but only one of them used. This creates many unclosed TCPClients.
  • Loading branch information...
1 parent 6397a0f commit 276b991e2ef7a55e6f3636555029047cfa6fec0b @ghk committed Oct 1, 2012
Showing with 25 additions and 25 deletions.
  1. +25 −25 source/MongoDB/Connections/Connection.cs
@@ -97,20 +97,20 @@ public Connection(IConnectionFactory factory)
{
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;
+ }
}
}
@@ -133,20 +133,20 @@ public Connection(IConnectionFactory factory)
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>

0 comments on commit 276b991

Please sign in to comment.