[CDRIVER-1873] mongoc_collection_aggregate() leaks a mongoc_read_concern_t/mongoc_write_concern_t, if read/write concern unspecified by the user Created: 20/Oct/16  Updated: 21/Oct/16  Resolved: 21/Oct/16

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: 1.5.0
Fix Version/s: 1.5.0

Type: Bug Priority: Major - P3
Reporter: J Rassi Assignee: Hannes Magnusson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CDRIVER-1874 Re-enable debug-compile-valgrind suite Closed

 Description   

Calling mongoc_collection_aggregate() will cause a leak of a mongoc_read_concern_t object, if the user does not specify a "readConcern" field in the command options. In addition, calling mongoc_collection_aggregate() will cause a leak of a mongoc_write_concern_t object, if the user does not specify a "writeConcern" field in the command options and the given pipeline includes a $out stage.

Regression introduced in 1.5.0-rc3 by https://github.com/mongodb/mongo-c-driver/commit/adfff3aa7fadd61063f82d6c0e5d35d655552d74 (CDRIVER-1704).

Reproduce with the following shell script:

git checkout 1.5.0-rc3
./configure --enable-debug --with-libbson=bundled
make -j32
./aggregation1
valgrind --leak-check=full .libs/lt-aggregation1

After running the above, valgrind outputs the following:

==1291== HEAP SUMMARY:
==1291==     in use at exit: 92,280 bytes in 3,085 blocks
==1291==   total heap usage: 3,532 allocs, 447 frees, 268,114 bytes allocated
==1291==
==1291== 256 bytes in 1 blocks are definitely lost in loss record 536 of 582
==1291==    at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1291==    by 0x50CC79E: bson_malloc0 (bson-memory.c:105)
==1291==    by 0x4E6F263: mongoc_read_concern_new (mongoc-read-concern.c:40)
==1291==    by 0x4E5BEBB: _mongoc_cursor_new_with_opts (mongoc-cursor.c:301)
==1291==    by 0x4E5C5F4: _mongoc_cursor_new (mongoc-cursor.c:470)
==1291==    by 0x4E55031: _mongoc_collection_cursor_new (mongoc-collection.c:65)
==1291==    by 0x4E55675: mongoc_collection_aggregate (mongoc-collection.c:329)
==1291==    by 0x400EBB: print_pipeline (aggregation1.c:18)
==1291==    by 0x400FF0: main (aggregation1.c:47)
==1291==
==1291== LEAK SUMMARY:
==1291==    definitely lost: 256 bytes in 1 blocks
==1291==    indirectly lost: 0 bytes in 0 blocks
==1291==      possibly lost: 0 bytes in 0 blocks
==1291==    still reachable: 92,024 bytes in 3,084 blocks
==1291==         suppressed: 0 bytes in 0 blocks



 Comments   
Comment by Hannes Magnusson [ 21/Oct/16 ]

Thanks @rassi for pinpointing the issue exactly!

Comment by Githook User [ 21/Oct/16 ]

Author:

{u'username': u'bjori', u'name': u'Hannes Magnusson', u'email': u'bjori@php.net'}

Message: CDRIVER-1873 mongoc_collection_aggregate() leaks mongoc_[read|write]_concern_t
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/466cd53acaa6651731b182b4a269ac83058d21e6

Comment by J Rassi [ 20/Oct/16 ]

Filed CDRIVER-1874 to track re-enabling the valgrind suite.

Comment by Hannes Magnusson [ 20/Oct/16 ]

Thanks rassi. Valgrind does indeed complain about the same in our tests. We should look into enabling valgrind in evergreen again.

Comment by J Rassi [ 20/Oct/16 ]

The leaks occur on mongoc-collection.c:418 and mongoc-collection.c:422 of the above linked commit (the objects associated with the previous pointer values are not destroyed):

416   /* Only inherit WriteConcern when for aggregate with $out */
417   if (!bson_has_field (&command, "writeConcern") && has_out_key) {
418      cursor->write_concern = mongoc_write_concern_copy (mongoc_collection_get_write_concern (collection));
419   }
420
421   if (!bson_has_field (&command, "readConcern")) {
422      cursor->read_concern = mongoc_read_concern_copy (mongoc_collection_get_read_concern (collection));
423   }

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