[SERVER-1827] Memory leak when there's multiple query plans with empty result Created: 22/Sep/10  Updated: 12/Jul/16  Resolved: 26/Oct/10

Status: Closed
Project: Core Server
Component/s: Stability
Affects Version/s: 1.6.2
Fix Version/s: 1.6.4, 1.7.1

Type: Bug Priority: Major - P3
Reporter: Constantine Verutin Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File patch.diff     Text File val.log    
Operating System: ALL
Participants:

 Description   

Steps to reproduce:
on empty DB do

> db.tst.insert({_id: "foo", k: "bar"})
> db.tst.find({$or: [{_id: "a", k: "b"}, {_id: "c", k: "d"}]})

Every such find() statement produces a leak of 32k bytes(see the very last record in attached valgrind log)

The problem is that in UserQueryOp::finish(), when appending data to message, there's no check if data length is 0. Here's the code:
– db/query.cpp@818
if ( _pq.isExplain())

{ _eb.noteScan( _c.get(), _nscanned, _nscannedObjects, _n, scanAndOrderRequired(), _curop.elapsedMillis(), useHints && !_pq.getHint().eoo() ); }

else

{ _response.appendData( _buf.buf(), _buf.len() ); _buf.decouple(); }


In case when _buf.len() is 0, the _response doesn't take ownership over _buf data, so after calling _buf.decouple() allocated buffer is lost and will be never freed.

I was able to reproduce this on 1.6.2 and git master, patch against master is attached.



 Comments   
Comment by auto [ 26/Oct/10 ]

Author:

{'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: Plug memory leak SERVER-1827
http://github.com/mongodb/mongo/commit/4f5c02f8d92ff213b71b88f5eb643b7f62b50abc

Comment by Dwight Merriman [ 18/Oct/10 ]

i assume so given fix version isn't marked as 1.6.3 above...

Comment by Fabio Ferrari [ 08/Oct/10 ]

Using valgrind I can see leaks of 32k in queries using the 1.6.3. Same problem?

Comment by Mathias Stearn [ 22/Sep/10 ]

Thanks for the patch

Comment by auto [ 22/Sep/10 ]

Author:

{'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: Plug memory leak SERVER-1827
http://github.com/mongodb/mongo/commit/65b31a0ba67a479340e3f5e3ca243dcc932b2b0d

Generated at Thu Feb 08 02:58:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.