Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1774

Race condition in CommandEventHelper ConnectionFailed method

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.2.4
    • Component/s: Connectivity
    • Labels:
      None

      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.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: