Skip to content

Commit 18758ef

Browse files
cjihrigtargos
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 <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ben Coe <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 5e98de1 commit 18758ef

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
@@ -184,7 +184,7 @@ function rimrafSync(path, options) {
184184
if (stats !== undefined && stats.isDirectory())
185185
_rmdirSync(path, options, null);
186186
else
187-
unlinkSync(path);
187+
_unlinkSync(path, options);
188188
} catch (err) {
189189
if (err.code === 'ENOENT')
190190
return;
@@ -198,6 +198,25 @@ function rimrafSync(path, options) {
198198
}
199199

200200

201+
function _unlinkSync(path, options) {
202+
const tries = options.maxRetries + 1;
203+
204+
for (let i = 1; i <= tries; i++) {
205+
try {
206+
return unlinkSync(path);
207+
} catch (err) {
208+
// Only sleep if this is not the last try, and the delay is greater
209+
// than zero, and an error was encountered that warrants a retry.
210+
if (retryErrorCodes.has(err.code) &&
211+
i < tries &&
212+
options.retryDelay > 0) {
213+
sleep(i * options.retryDelay);
214+
}
215+
}
216+
}
217+
}
218+
219+
201220
function _rmdirSync(path, options, originalErr) {
202221
try {
203222
rmdirSync(path);
@@ -264,7 +283,7 @@ function fixWinEPERMSync(path, options, originalErr) {
264283
if (stats.isDirectory())
265284
_rmdirSync(path, options, originalErr);
266285
else
267-
unlinkSync(path);
286+
_unlinkSync(path, options);
268287
}
269288

270289

0 commit comments

Comments
 (0)