From 18758ef1837beae107bc763cb5a034710c122142 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 38bbb66a47df9b..3402af60a4022f 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -184,7 +184,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; @@ -198,6 +198,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); @@ -264,7 +283,7 @@ function fixWinEPERMSync(path, options, originalErr) { if (stats.isDirectory()) _rmdirSync(path, options, originalErr); else - unlinkSync(path); + _unlinkSync(path, options); }