From 7e1dee334729beb65db52586f90703385b40da61 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 8 Dec 2019 10:50:04 -0500 Subject: [PATCH] fs: reduce unnecessary sync rimraf retries rimraf should only retry if certain errors are encountered. Additionally, there is no point sleeping if an error occurs on the last try. PR-URL: https://github.com/nodejs/node/pull/30785 Fixes: https://github.com/nodejs/node/issues/30580 Refs: https://github.com/nodejs/node/pull/30569 Reviewed-By: Rich Trott Reviewed-By: Richard Lau --- lib/internal/fs/rimraf.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/internal/fs/rimraf.js b/lib/internal/fs/rimraf.js index d6330fbe4399a0..6d6b19efcc98f2 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -209,12 +209,19 @@ function _rmdirSync(path, options, originalErr) { rimrafSync(join(path, child), options); }); - for (let i = 1; i <= options.maxRetries + 1; i++) { + const tries = options.maxRetries + 1; + + for (let i = 1; i <= tries; i++) { try { return rmdirSync(path, options); - } catch { - if (options.retryDelay > 0) + } 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); + } } } }