From e6b6ce10278ff6a7d119d2cadcdd0f7880add339 Mon Sep 17 00:00:00 2001 From: aissam irhir Date: Thu, 4 Dec 2025 15:52:13 +0100 Subject: [PATCH] fix: clear setTimeout in buffered syncCollectionMethods with callbacks When syncCollectionMethods (find, watch, aggregate) were called with a callback while buffering was enabled, the buffer timeout was never cleared after the operation completed successfully. This caused setTimeout references to accumulate, leading to a memory leak. The fix wraps the callback to call clearTimeout() when the operation completes, matching the behavior of all other buffered operation paths. Fixes #15851 --- lib/drivers/node-mongodb-native/collection.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/drivers/node-mongodb-native/collection.js b/lib/drivers/node-mongodb-native/collection.js index 1e4678ff93f..b3500d77538 100644 --- a/lib/drivers/node-mongodb-native/collection.js +++ b/lib/drivers/node-mongodb-native/collection.js @@ -138,8 +138,14 @@ function iter(i) { let promise = null; let timeout = null; if (syncCollectionMethods[i] && typeof lastArg === 'function') { + callback = function collectionOperationCallback() { + if (timeout != null) { + clearTimeout(timeout); + } + return lastArg.apply(this, arguments); + }; + _args = args.slice(0, args.length - 1).concat([callback]); this.addQueue(i, _args); - callback = lastArg; } else if (syncCollectionMethods[i]) { promise = new this.Promise((resolve, reject) => { callback = function collectionOperationCallback(err, res) {