-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 2.12.0
-
Component/s: None
-
Environment:Mongo 4.2.6 on MacOS 10.15.4
Mongo 4.2.5 on Centos7
In the 2.11.4 of the Mongo gem, if a document was too large to be inserted, a Mongo::Error::MaxBSONSize exception was raised.
When upgrading the gem to 2.12.1, an Mongo::Error::OperationFailure is raised.
The following code reproduces the issue:
require 'mongo' client = Mongo::Client.new('mongodb://127.0.0.1:27017/test') client['documents'].insert_one({name: 'small', data: 'a' * 16_777_216 }) # raises a Mongo::Error::OperationFailure in 2.12.1, Mongo::Error::MaxBSONSize in 2.11.4
-------
2.11 behavior: there is no document size check, there is a command size check and the limit is 16mib.
2.12 behavior: there is still no document size check, there is a command size check and the limit is 16mib+16kib.
The document being inserted in the above repro is ~16mib+30 bytes.
For a document which is exactly 16mib or slightly less than 16mib in size (where the difference between actual document size and 16mib limit is less than command overhead, e.g. a document that is ~16mib-100 bytes):
2.11 behavior: driver fails (erroneously) the operation claiming the document size exceeds 16mib; this is incorrect because document size does not exceed 16mib, instead command size exceeds 16mib but driver applies document size limit to command size.
2.12 behavior: driver allows (correctly) operation, operation succeeds.
For a document which is slightly over 16mib such as the 16mib+30 document in the above reproduce case:
2.11 behavior: driver fails operation claiming document size exceeds 16mib; this is technically correct but the driver used technically faulty logic to arrive at this determination because it compared the command size to 16mib limit not the document size
2.12 behavior: driver allows operation to proceed because command size is under 16mib+16kib limit, server fails operation (reported exception changes as per this ticket).
Proposed fix: serialize documents and verify their serialized size is under 16mib as required by server.