Skip to content

Commit 55c5baf

Browse files
cjihrigBethGriggs
authored andcommitted
fs: retry unlink operations in rimraf
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>
1 parent edc9efa commit 55c5baf

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

lib/internal/fs/rimraf.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function rimrafSync(path, options) {
190190
if (stats !== undefined && stats.isDirectory())
191191
_rmdirSync(path, options, null);
192192
else
193-
unlinkSync(path);
193+
_unlinkSync(path, options);
194194
} catch (err) {
195195
if (err.code === 'ENOENT')
196196
return;
@@ -204,6 +204,25 @@ function rimrafSync(path, options) {
204204
}
205205

206206

207+
function _unlinkSync(path, options) {
208+
const tries = options.maxRetries + 1;
209+
210+
for (let i = 1; i <= tries; i++) {
211+
try {
212+
return unlinkSync(path);
213+
} catch (err) {
214+
// Only sleep if this is not the last try, and the delay is greater
215+
// than zero, and an error was encountered that warrants a retry.
216+
if (retryErrorCodes.has(err.code) &&
217+
i < tries &&
218+
options.retryDelay > 0) {
219+
sleep(i * options.retryDelay);
220+
}
221+
}
222+
}
223+
}
224+
225+
207226
function _rmdirSync(path, options, originalErr) {
208227
try {
209228
rmdirSync(path);
@@ -270,7 +289,7 @@ function fixWinEPERMSync(path, options, originalErr) {
270289
if (stats.isDirectory())
271290
_rmdirSync(path, options, originalErr);
272291
else
273-
unlinkSync(path);
292+
_unlinkSync(path, options);
274293
}
275294

276295

0 commit comments

Comments
 (0)