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

EndOfStreamException: Attempted to read past the end of the stream.

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: None
    • Component/s: Connectivity
    • None
    • Environment:
      C#.NET, Mongodb ATLAS

      When I am trying to run multiple asynchronous requests against the server, I get following error: 

        Name Value Type
      ConnectionId Unknown macro: { { ServerId }

      }

      MongoDB.Driver.Core.Connections.ConnectionId
      Data {System.Collections.ListDictionaryInternal}|System.Collections.IDictionary {System.Collections.ListDictionaryInternal}|
      |▶|ErrorLabels|Count = 0|System.Collections.Generic.IReadOnlyList<string> {System.Collections.Generic.List<string>}|
      | |HResult|-2146233088|int|
      | |HelpLink|null|string|
      |◢|InnerException|{"An exception occurred while receiving a message from the server."}|System.Exception {MongoDB.Driver.MongoConnectionException}|
      | |▶ ConnectionId|{{ ServerId : { ClusterId : 1, EndPoint : "Unspecified/weird0-shard-00-00-h6hi1.mongodb.net:27017" }, LocalValue : 90 }}|MongoDB.Driver.Core.Connections.ConnectionId|
      | |▶ Data|{System.Collections.ListDictionaryInternal}|System.Collections.IDictionary {System.Collections.ListDictionaryInternal}|
      | |▶ ErrorLabels|Count = 0|System.Collections.Generic.IReadOnlyList<string> {System.Collections.Generic.List<string>}|
      | |HResult|-2146233088|int|
      | |HelpLink|null|string|
      | |◢ InnerException|{"Attempted to read past the end of the stream."}|System.Exception {System.IO.EndOfStreamException}|
      | |▶ Data|{System.Collections.ListDictionaryInternal}
      System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
        HResult -2147024858 int
        HelpLink null string
        ▶ InnerException null System.Exception
        Message "Attempted to read past the end of the stream." string
        Source "MongoDB.Driver.Core" string
        StackTrace " at MongoDB.Driver.Core.Misc.StreamExtensionMethods.<ReadBytesAsync>d_2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.Core.Connections.BinaryConnection.<ReceiveBufferAsync>d_51.MoveNext()" string
        ▶ TargetSite {Void MoveNext()}|System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}|
      | |▶ Static members| | |
      | |▶ Non-Public members| | |
      | |Message|"An exception occurred while receiving a message from the server."|string|
      | |Source|"MongoDB.Driver.Core"|string|
      | |StackTrace|" at MongoDB.Driver.Core.Connections.BinaryConnection.<ReceiveBufferAsync>d_51.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.Core.Connections.BinaryConnection.<ReceiveBufferAsync>d52.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.Core.Connections.BinaryConnection.<ReceiveMessageAsync>d54.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.<ExecuteAsync>d14.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.Core.Connections.IsMasterHelper.<GetResultAsync>d5.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.Core.Connections.ConnectionInitializer.<InitializeConnectionAsync>d3.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d_48.MoveNext()"|string|
      | |▶ TargetSite|{Void MoveNext()}|System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}|
      | |▶ Static members| | |
      | |▶ Non-Public members| | |
      | |Message|"An exception occurred while opening a connection to the server."|string|
      | |Source|"MongoDB.Driver.Core"|string|
      | |StackTrace|" at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d_48.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.Core.Servers.Server.<GetChannelAsync>d30.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.Core.Operations.AggregateOperation`1.<ExecuteAsync>d55.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.OperationExecutor.<ExecuteReadOperationAsync>d3`1.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.MongoCollectionImpl`1.<ExecuteReadOperationAsync>d93`1.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.MongoCollectionImpl`1.<AggregateAsync>d22`1.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.MongoCollectionImpl`1.<UsingImplicitSessionAsync>d101`1.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown --\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at MongoDB.Driver.AsyncCursorHelper.<FirstOrDefaultAsync>d2`1.MoveNext()\r\n-- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n at Amazon.Cloud.DataAccess.Layer.VirtualFolderDal.<WalkPath>d_11.MoveNext() in C:\\Users\\Weirdo\\source\\repos\\AmazonExploiter\\Amazon.Cloud.DataAccessLayer
      Layer
      VirtualFolderDal.cs:line 137"|string|
      |▶|TargetSite|{Void MoveNext()}
      System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

       

       

      Affected Library Code:

      // Code-Platzhalter
              private async Task<(bool success, int lastPosition, MongoVirtualFolder folder)> WalkPath(string[] pathParts, VirtualFolder startFolder = null)
              {
                  var folder = await ToMongoVirtualFolder(startFolder) ?? _rootFolder;            for (var i = 0; i < pathParts.Length; i++)
                  {
                      var part = pathParts[i];
      // 
      // The error occurs here:
      //
                      var newFolder = await Collection
                          .Where(x => x.Parent == folder.Id && x.Name == part)
                          .FirstOrDefaultAsync()
                          .ConfigureAwait(false);
                      if (newFolder == null)
                      {
                          return (false, i, folder);
                      }                folder = newFolder;
                  }
                  return (true, pathParts.Length, folder);
              }        public Task<VirtualFolder> CreateFolder(string path, VirtualFolder startFolder = null)
              {
                  var folderParts = path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
                  return CreateFolder(folderParts, startFolder);
              }        public async Task<VirtualFolder> CreateFolder(string[] folderParts, VirtualFolder startFolder = null)
              {
                  var (success, i, folder) = await WalkPath(folderParts, startFolder);
                  if (!success)
                  {
                      int j = i;
                      try
                      {
                          for (j = i; j < folderParts.Length; j++)
                          {
                              var newFolder = new MongoVirtualFolder
                              {
                                  Name = folderParts[j],
                                  Parent = folder.Id,
                              };                        await Collection.InsertOneAsync(newFolder);
                              folder = newFolder;
                          }
                      }
                      catch (MongoWriteException exception) when (exception.InnerException is MongoBulkWriteException InnerException)
                      {
                          if (InnerException.WriteErrors.Any(x => x.Category == ServerErrorCategory.DuplicateKey))
                          {
                              //Duplicate key
                              var searchArray = folderParts.AsEnumerable().Skip(i).Take(folderParts.Length - i).ToArray();
                              Console.WriteLine($"Concurrency warning! Retrying ({string.Join("/", folderParts)})");
                              return await CreateFolder(searchArray, folder);
                          }                    throw;
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine(e);
                          throw;
                      }                folder.Folders = new List<VirtualFolderSlim>();
                      folder.VirtualFiles = new List<VirtualFile>();
                  }
                  else
                  {
                      await LoadFolderContents(folder);
                  }            return folder;
              }
      

      Affected Usercode:

      // Entrypoint:
      async Task<Dictionary<string, (DirectoryInfo, VirtualFolder)>> CreateFolders1(VirtualFolder fold, DirectoryInfo dir)
                  {
                      var result = new Dictionary<string, (DirectoryInfo, VirtualFolder)>();
                      var dirs = dir.EnumerateDirectories("*", SearchOption.TopDirectoryOnly);                //var subFolders = await dirs.Select(x => _virtualFolderDal.CreateFolder(x.Name, fold)).WhenAll();                var subFolders = await dirs.Select(x => ___CreateFolder(fold, x)).WhenAll();
                      foreach (var virtualFolder in subFolders.SelectMany(x => x))
                      {
                          result[virtualFolder.Key] = virtualFolder.Value;
                      }                return result;                async Task<Dictionary<string, (DirectoryInfo, VirtualFolder)>> ___CreateFolder(VirtualFolder __fold, DirectoryInfo __dir)
                      {
                          var subFolder = await _virtualFolderDal.CreateFolder(__dir.Name, __fold);
                          return await CreateFolders1(subFolder, __dir);
                      }
                  }
      
      

       

      If you have any questions please dont hesitate to ask. 

            Assignee:
            wan.bachtiar@mongodb.com Wan Bachtiar
            Reporter:
            weirdo Jonas Kamsker
            Votes:
            2 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: