Skip to content

Commit

Permalink
windows - wait to file to unlock for removal
Browse files Browse the repository at this point in the history
while removing dir or file, it happens that EBUSY error could happen with file being used and lock. This is a simple way to wait for file to be unlocked, with a fail safe of max try to avoid infinite loop.
  • Loading branch information
cderv committed Aug 28, 2024
1 parent c1a0e38 commit 76ce3ca
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/core/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as ld from "./lodash.ts";

import { getenv } from "./env.ts";
import { execProcess } from "./process.ts";
import { isWindows } from "./platform.ts";

export const kSkipHidden = /[/\\][\.]/;

Expand All @@ -50,8 +51,25 @@ export function safeRemoveSync(
try {
Deno.removeSync(file, options);
} catch (e) {
let lastError = e;
// WINDOWS ONLY: Retry on windows to let time to file to unlock
if (isWindows() && e.code === "EBUSY") {
let nTry: number = 1;
// high number to prevent infinite loop
const maxTry: number = 500;
let eCode = e.code;
while (eCode === "EBUSY" && nTry <= maxTry) {
try {
Deno.removeSync(file, options);
} catch (e) {
lastError = e;
eCode = e.code;
nTry++;
}
}
}
if (existsSync(file)) {
throw e;
throw lastError;
}
}
}
Expand Down

0 comments on commit 76ce3ca

Please sign in to comment.