diff --git a/lib/drivers/node-mongodb-native/collection.js b/lib/drivers/node-mongodb-native/collection.js index 1e4678ff93..b4a61fe075 100644 --- a/lib/drivers/node-mongodb-native/collection.js +++ b/lib/drivers/node-mongodb-native/collection.js @@ -124,6 +124,7 @@ function iter(i) { let _args = args; let callback = null; + let timeout = null; if (this._shouldBufferCommands() && this.buffer) { this.conn.emit('buffer', { _id: opId, @@ -136,7 +137,6 @@ function iter(i) { let callback; let _args = args; let promise = null; - let timeout = null; if (syncCollectionMethods[i] && typeof lastArg === 'function') { this.addQueue(i, _args); callback = lastArg; @@ -239,6 +239,9 @@ function iter(i) { if (syncCollectionMethods[i] && typeof lastArg === 'function') { const result = collection[i].apply(collection, _args.slice(0, _args.length - 1)); + if (timeout != null) { + clearTimeout(timeout); + } this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, result }); return lastArg.call(this, null, result); } @@ -247,6 +250,9 @@ function iter(i) { if (ret != null && typeof ret.then === 'function') { return ret.then( result => { + if (timeout != null) { + clearTimeout(timeout); + } if (typeof lastArg === 'function') { lastArg(null, result); } else { @@ -255,6 +261,9 @@ function iter(i) { return result; }, error => { + if (timeout != null) { + clearTimeout(timeout); + } if (typeof lastArg === 'function') { lastArg(error); return; @@ -265,10 +274,16 @@ function iter(i) { } ); } + if (timeout != null) { + clearTimeout(timeout); + } return ret; } catch (error) { // Collection operation may throw because of max bson size, catch it here // See gh-3906 + if (timeout != null) { + clearTimeout(timeout); + } if (typeof lastArg === 'function') { return lastArg(error); } else {