Skip to content

Commit f36186b

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 af0bb42 commit f36186b

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";
@@ -116,8 +117,25 @@ export function safeRemoveSync(
116117
try {
117118
Deno.removeSync(file, options);
118119
} catch (e) {
120+
let lastError = e;
121+
// WINDOWS ONLY: Retry on windows to let time to file to unlock
122+
if (isWindows && e.code === "EBUSY") {
123+
let nTry: number = 1;
124+
// high number to prevent infinite loop
125+
const maxTry: number = 500;
126+
let eCode = e.code;
127+
while (eCode === "EBUSY" && nTry <= maxTry) {
128+
try {
129+
Deno.removeSync(file, options);
130+
} catch (e) {
131+
lastError = e;
132+
eCode = e.code;
133+
nTry++;
134+
}
135+
}
136+
}
119137
if (existsSync(file)) {
120-
throw e;
138+
throw lastError;
121139
}
122140
}
123141
}

0 commit comments

Comments
 (0)