Skip to content

Commit be168e7

Browse files
committed
windows - wait to file to unlock for removal
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.
1 parent c1ea7b2 commit be168e7

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/deno_ral/fs.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { fromFileUrl } from "./path.ts";
88
import { resolve, SEP as SEPARATOR } from "./path.ts";
99
import { copySync } from "fs/copy";
1010
import { existsSync } from "fs/exists";
11+
import { isWindows } from "./platform.ts";
1112

1213
export { ensureDir, ensureDirSync } from "fs/ensure-dir";
1314
export { existsSync } from "fs/exists";
@@ -100,8 +101,25 @@ export function safeRemoveSync(
100101
try {
101102
Deno.removeSync(file, options);
102103
} catch (e) {
104+
let lastError = e;
105+
// WINDOWS ONLY: Retry on windows to let time to file to unlock
106+
if (isWindows && e.code === "EBUSY") {
107+
let nTry: number = 1;
108+
// high number to prevent infinite loop
109+
const maxTry: number = 500;
110+
let eCode = e.code;
111+
while (eCode === "EBUSY" && nTry <= maxTry) {
112+
try {
113+
Deno.removeSync(file, options);
114+
} catch (e) {
115+
lastError = e;
116+
eCode = e.code;
117+
nTry++;
118+
}
119+
}
120+
}
103121
if (existsSync(file)) {
104-
throw e;
122+
throw lastError;
105123
}
106124
}
107125
}

0 commit comments

Comments
 (0)