From 55c5baf4139be418922382a898838e9de4de600b Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 9 Dec 2019 10:49:34 -0500 Subject: [PATCH] fs: retry unlink operations in rimraf This commit adds synchronous retry logic to the unlinkSync() calls in rimraf. PR-URL: https://github.com/nodejs/node/pull/30569 Reviewed-By: Jiawen Geng Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: Ben Coe Reviewed-By: Ruben Bridgewater --- lib/internal/fs/rimraf.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/internal/fs/rimraf.js b/lib/internal/fs/rimraf.js index 590b86dddacf65..e88dd9697b1938 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -190,7 +190,7 @@ function rimrafSync(path, options) { if (stats !== undefined && stats.isDirectory()) _rmdirSync(path, options, null); else - unlinkSync(path); + _unlinkSync(path, options); } catch (err) { if (err.code === 'ENOENT') return; @@ -204,6 +204,25 @@ function rimrafSync(path, options) { } +function _unlinkSync(path, options) { + const tries = options.maxRetries + 1; + + for (let i = 1; i <= tries; i++) { + try { + return unlinkSync(path); + } catch (err) { + // Only sleep if this is not the last try, and the delay is greater + // than zero, and an error was encountered that warrants a retry. + if (retryErrorCodes.has(err.code) && + i < tries && + options.retryDelay > 0) { + sleep(i * options.retryDelay); + } + } + } +} + + function _rmdirSync(path, options, originalErr) { try { rmdirSync(path); @@ -270,7 +289,7 @@ function fixWinEPERMSync(path, options, originalErr) { if (stats.isDirectory()) _rmdirSync(path, options, originalErr); else - unlinkSync(path); + _unlinkSync(path, options); }