From ddc1a2f9d42da77342d58f6fd86942a231f5f27f Mon Sep 17 00:00:00 2001 From: Pete Gonzalez Date: Wed, 7 Nov 2018 04:43:31 -0800 Subject: [PATCH] Fix removeSync() to eliminate spurious ENOTEMPTY errors on Windows (#646) Fix removeSync() to measure its retry interval in milliseconds instead of CPU cycles --- lib/remove/rimraf.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/remove/rimraf.js b/lib/remove/rimraf.js index c89c1c01..f287e4e1 100644 --- a/lib/remove/rimraf.js +++ b/lib/remove/rimraf.js @@ -290,24 +290,24 @@ function rmkidsSync (p, options) { assert(options) options.readdirSync(p).forEach(f => rimrafSync(path.join(p, f), options)) - // We only end up here once we got ENOTEMPTY at least once, and - // at this point, we are guaranteed to have removed all the kids. - // So, we know that it won't be ENOENT or ENOTDIR or anything else. - // try really hard to delete stuff on windows, because it has a - // PROFOUNDLY annoying habit of not closing handles promptly when - // files are deleted, resulting in spurious ENOTEMPTY errors. - const retries = isWindows ? 100 : 1 - let i = 0 - do { - let threw = true - try { - const ret = options.rmdirSync(p, options) - threw = false - return ret - } finally { - if (++i < retries && threw) continue // eslint-disable-line - } - } while (true) + if (isWindows) { + // We only end up here once we got ENOTEMPTY at least once, and + // at this point, we are guaranteed to have removed all the kids. + // So, we know that it won't be ENOENT or ENOTDIR or anything else. + // try really hard to delete stuff on windows, because it has a + // PROFOUNDLY annoying habit of not closing handles promptly when + // files are deleted, resulting in spurious ENOTEMPTY errors. + const startTime = Date.now() + do { + try { + const ret = options.rmdirSync(p, options) + return ret + } catch (er) { } + } while (Date.now() - startTime < 500) // give up after 500ms + } else { + const ret = options.rmdirSync(p, options) + return ret + } } module.exports = rimraf