[CSHARP-1774] Race condition in CommandEventHelper ConnectionFailed method Created: 03/Oct/16  Updated: 19/Oct/16  Resolved: 14/Oct/16

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

Type: Bug Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The following test failure was observed on Jenkins:

MESSAGE:
Expected MongoDB.Driver.MongoConnectionException, but found (aggregated) System.ArgumentException with message "The index is equal to or greater than the length of the array, or the number of elements in the dictionary is greater than the available space from index to the end of the destination array."\n
     at System.Collections.Concurrent.ConcurrentDictionary`2.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair`2[] array, Int32 index)\r\n
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n
     at MongoDB.Driver.Core.Connections.CommandEventHelper.ConnectionFailed(ConnectionId connectionId, Exception exception) in D:\jenkins\workspace\mongo-csharp-driver-build\src\MongoDB.Driver.Core\Core\Connections\CommandEventHelper.cs:line 218\r\n
     at MongoDB.Driver.Core.Connections.BinaryConnection.ConnectionFailed(Exception exception) in D:\jenkins\workspace\mongo-csharp-driver-build\src\MongoDB.Driver.Core\Core\Connections\BinaryConnection.cs:line 173\r\n
     at MongoDB.Driver.Core.Connections.BinaryConnection.SendBuffer(IByteBuffer buffer, CancellationToken cancellationToken) in D:\jenkins\workspace\mongo-csharp-driver-build\src\MongoDB.Driver.Core\Core\Connections\BinaryConnection.cs:line 505\r\n
     at MongoDB.Driver.Core.Connections.BinaryConnection.SendMessages(IEnumerable`1 messages, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken) in D:\jenkins\workspace\mongo-csharp-driver-build\src\MongoDB.Driver.Core\Core\Connections\BinaryConnection.cs:line 562\r\n
     at MongoDB.Driver.Core.Connections.ConnectionExtensions.SendMessage(IConnection connection, RequestMessage message, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken) in D:\jenkins\workspace\mongo-csharp-driver-build\src\MongoDB.Driver.Core\Core\Connections\ConnectionExtensions.cs:line 39\r\n
     at MongoDB.Driver.Core.Connections.BinaryConnectionTests.<>c__DisplayClass24_0.<SendMessageshould_throw_MongoConnectionClosedException_for_waiting_tasks>b__2() in D:\jenkins\workspace\mongo-csharp-driver-build\tests\MongoDB.Driver.Core.Tests\Core\Connections\BinaryConnectionTests.cs:line 694\r\n
     at System.Threading.Tasks.Task.Execute()\r\n.
+++++++++++++++++++
STACK TRACE:
   at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions.Net45\Execution\XUnit2TestFramework.cs:line 35
   at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args) in C:\projects\fluentassertions-vf06b\Src\Core\Execution\AssertionScope.cs:line 197
   at FluentAssertions.Specialized.AsyncFunctionAssertions.ShouldThrow[TException](String because, Object[] becauseArgs) in C:\projects\fluentassertions-vf06b\Src\Core\Specialized\AsyncFunctionAssertions.cs:line 55
   at MongoDB.Driver.Core.Connections.BinaryConnectionTests.SendMessageshould_throw_MongoConnectionClosedException_for_waiting_tasks(Boolean async1, Boolean async2) in D:\jenkins\workspace\mongo-csharp-driver-build\tests\MongoDB.Driver.Core.Tests\Core\Connections\BinaryConnectionTests.cs:line 714

It appears to be that calling ToList() on a ConcurrentDictionary is not thread safe. The ConcurrentDictionary might change while ToList is being processed which could result in exceptions.



 Comments   
Comment by Githook User [ 07/Oct/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1774: Fix race condition in CommandEventHelper.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/d410f5d5cf3f3c475fef71901005f8a4c7ab229a

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