From a6df33299a231fa0dd092f4d04b9f11564d61e81 Mon Sep 17 00:00:00 2001 From: williamlardier Date: Fri, 29 Mar 2024 12:17:05 +0100 Subject: [PATCH] S3UTILS-157: explicitly close the mongodb cursors --- utils/S3UtilsMongoClient.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/utils/S3UtilsMongoClient.js b/utils/S3UtilsMongoClient.js index 6722bcfe..cc08a2a8 100644 --- a/utils/S3UtilsMongoClient.js +++ b/utils/S3UtilsMongoClient.js @@ -15,9 +15,10 @@ const __COUNT_ITEMS = 'countitems'; class S3UtilsMongoClient extends MongoClientInterface { async getObjectMDStats(bucketName, bucketInfo, isTransient, log, callback) { + let cursor; try { const c = this.getCollection(bucketName); - const cursor = c.find({}, { + cursor = c.find({}, { projection: { '_id': 1, 'value.last-modified': 1, @@ -44,11 +45,15 @@ class S3UtilsMongoClient extends MongoClientInterface { const locationConfig = getLocationConfig(log); - const usersBucketCreationDatesArray = await this.getCollection(USERSBUCKET).find({}, { + const cursorUsersBucketCreationDates = await this.getCollection(USERSBUCKET).find({}, { projection: { 'value.creationDate': 1, }, - }).toArray(); + }); + + const usersBucketCreationDatesArray = cursorUsersBucketCreationDates.toArray(); + + await cursorUsersBucketCreationDates.close(); const usersBucketCreationDatesMap = usersBucketCreationDatesArray .reduce((map, obj) => ({ ...map, [obj._id]: obj.value.creationDate }), {}); @@ -196,6 +201,14 @@ class S3UtilsMongoClient extends MongoClientInterface { errorString: err.toString(), }); return callback(err); + } finally { + if (cursor && !cursor.closed) { + log.info('Finished processing cursor', { + method: 'getObjectMDStats', + bucketName, + }); + cursor.close(); + } } }