From 00367055bcb72cfd4a8ecbefeee09048cc8d5abe Mon Sep 17 00:00:00 2001 From: Vitali Lovich Date: Mon, 3 Jan 2022 07:54:57 -0800 Subject: [PATCH] Fix race condition when deleting files Gracefully handle multiple threads deleting an object at the same time. When two concurrent requests come in to delete the last two remaining objects in a given path, they'll be racing each other trying to delete and cause a spurious deletion failure for one of them. Fixes #788 --- lib/stores/filesystem.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/stores/filesystem.js b/lib/stores/filesystem.js index 576da202..f21368b3 100644 --- a/lib/stores/filesystem.js +++ b/lib/stores/filesystem.js @@ -377,14 +377,13 @@ class FilesystemStore { const parts = key.split('/'); // the last part isn't a directory (it's embedded into the file name) parts.pop(); - while ( - parts.length && - !readdirSync(path.join(bucketPath, ...parts)).length - ) { - await fs.rmdir(path.join(bucketPath, ...parts)); - parts.pop(); + while (parts.length) { + await fs.rmdir(path.join(bucketPath, ...parts)).catch(err => { + if (err.code !== 'ENOENT' && err.code !== 'ENOTEMPTY') throw err; + parts.length = 0; + }); } - } +} async initiateUpload(bucket, key, uploadId, metadata) { const uploadDir = path.join(