Skip to content

Commit

Permalink
fs: retry unlink operations in rimraf
Browse files Browse the repository at this point in the history
This commit adds synchronous retry logic to
the unlinkSync() calls in rimraf.

PR-URL: #30569
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ben Coe <bencoe@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
cjihrig authored and targos committed Jan 14, 2020
1 parent 76affb6 commit fb2253e
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/internal/fs/rimraf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -270,7 +289,7 @@ function fixWinEPERMSync(path, options, originalErr) {
if (stats.isDirectory())
_rmdirSync(path, options, originalErr);
else
unlinkSync(path);
_unlinkSync(path, options);
}


Expand Down

0 comments on commit fb2253e

Please sign in to comment.