From 413b45e8e5ccbaf746b86de7c0b9669dbc0464e4 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 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/stores/filesystem.js b/lib/stores/filesystem.js index 576da202..9dfc9def 100644 --- a/lib/stores/filesystem.js +++ b/lib/stores/filesystem.js @@ -377,14 +377,14 @@ 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)); + while (parts.length) { + await fs.rmdir(path.join(bucketPath, ...parts)).catch(err => { + if (err.code !== 'ENOENT' && err.code !== 'ENOTEMPTY') throw err; + parts.length = 0; + }); parts.pop(); } - } +} async initiateUpload(bucket, key, uploadId, metadata) { const uploadDir = path.join(