Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve memory leak when using cursor operations like $count in mongo…
…db@4-6 Starting in Mongo Node driver `mongodb@4`, up through the latest version `mongodb@6`, using `cursor.count()` results in a memory leak. `mongodb@3` doesn't leak in the same situation. The driver creates an implicit client session, but it doesn't automatically close the session when getting the results back from the server. Those unclosed sessions build up over time, causing a memory leak. sharedb-mongo exposes document counting via a user passing the `$count: true` property on query objects, and it currently uses `cursor.count()`. There are a couple ways sharedb-mongo could address the leak: - Switch to `Collection#countDocuments()`, which is the recommended replacement for the deprecated `FindCursor#count()`. This is better long-term, but it's more work since we have to map things like `$limit` from using chained cursor calls over to the equivalent property in CountOptions, where appropriate. - Explicitly close the cursor. Easy and safe, since the cursor is created inside sharedb-mongo and not exposed externally. To resolve the leak more quickly, this change opts for the latter, explicitly closing the cursor for the "cursor operations" `$count`, `$explain`, and `$map`.
- Loading branch information