From 276b991e2ef7a55e6f3636555029047cfa6fec0b Mon Sep 17 00:00:00 2001 From: Gozali Kumara Date: Mon, 1 Oct 2012 12:32:53 +0700 Subject: [PATCH] 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. --- source/MongoDB/Connections/Connection.cs | 50 ++++++++++++------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/source/MongoDB/Connections/Connection.cs b/source/MongoDB/Connections/Connection.cs index c0d80a07..da085cca 100644 --- a/source/MongoDB/Connections/Connection.cs +++ b/source/MongoDB/Connections/Connection.cs @@ -97,20 +97,20 @@ internal ReplyMessage SendTwoWayMessageCore(IRequestMessage message, BsonR { EnsureOpenConnection(); - try - { - var reply = new ReplyMessage(readerSettings); - lock(_connection) - { - message.Write(_connection.GetStream()); - reply.Read(_connection.GetStream()); - } - return reply; - } - catch(IOException) + lock(_connection) { - ReplaceInvalidConnection(); - throw; + try + { + var reply = new ReplyMessage(readerSettings); + message.Write(_connection.GetStream()); + reply.Read(_connection.GetStream()); + return reply; + } + catch(IOException) + { + ReplaceInvalidConnection(); + throw; + } } } @@ -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; + } } + } ///