.Net Driver - 'The size of the message is invalid' when querying many small documents

I recently updated from the MongoDB Driver from 2.7.2 to 2.10.4. After updating some of my larger queries throws this exception ‘The size of the message is invalid’. After digging through the Net driver source I found that more validation was added around July 1 2019 (CSHARP-1501: Drivers must raise an error if response messageLength > …). I’ve been trying to figure out how to set the MaxMessageSize. I found some information that makes me think its coming from the server (Azure CosmosDB). Is it possible to change this value?

I’ve checked the size of some data coming back from the older version and it’s only ~19 megs.

Here’s the method that throws the exception:
src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs

private void EnsureMessageSizeIsValid(int messageSize)
{
     var maxMessageSize = _description?.MaxMessageSize ?? 48000000;

     if (messageSize < 0 || messageSize > maxMessageSize)
     {
          throw new FormatException("The size of the message is invalid.");
     }
}

I’ve followed the _description.MaxMessageSize back to the src/MongoDB.Driver.Core/Core/Connections/IsMasterResult.cs

 public int MaxMessageSize
 {
      get
      {
            BsonValue value;
            if (_wrapped.TryGetValue("maxMessageSizeBytes", out value))
            {
                 return value.ToInt32();
            }

            return Math.Max(MaxDocumentSize + 1024, 16000000);
       }
 }

Any help would be greatly appreciated.

1 Like

I faced the same problem. Downgrading version of MongoDB Driver to 2.7.2 “solves” the issue. I also guess that CosmoDB is part of this problem/issue, however did not find out how to solve it on the server site, yet.

Welcome to the community @Jamie_Etcheson and @Marco_Lewandowski!

The maxMessageSizeBytes is a server value determined during the driver connection handshake via the isMaster command. You can check the server value in the mongo shell via db.isMaster().maxMessageSizeBytes, but drivers are expected to follow the server response rather than overriding this value. The default value for MongoDB servers is 48000000 bytes.

The intent of the exception is to guard against sending an invalid message size, so it sounds like this might be an issue with Cosmos DB. I suggest following up with Cosmos support.

Please note that Cosmos’ API is an emulation of MongoDB which differs in features, compatibility, and implementation from an actual MongoDB deployment. Cosmos’ suggestion of API version support (eg 3.6) is referring to the wire protocol rather than the full MongoDB feature set for that version. Official MongoDB drivers (like .NET) are only tested against actual MongoDB deployments.

Regards,
Stennie

2 Likes

I also met this issue, actually it’s a new validation on message size introduced from C# driver 2.9,
https://statics.teams.cdn.office.net/evergreen-assets/safelinks/1/atp-safelinks.html

Currently the only safe solution is downgrading the driver to 2.8-