From 7cb548e45b846683d74dac3c2108fedceb5d146a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 21 Nov 2026 18:21:32 +0000 Subject: [PATCH] fix(core): a Windows rename onto a folder that exists fails at once instead of waiting 1.3 s (#8389) * test(core): a Windows rename onto an existing folder gives up at once * fix(core): a Windows rename onto a folder that exists fails at once instead of waiting 1.3 s * test(core): a Windows rename onto an empty or unreadable folder target still waits * fix(core): give up at once only on a folder target with files in it, and keep the wait otherwise --- packages/core/src/renameWithRetry.test.ts | 56 +++++++++++++++++++++++ packages/core/src/renameWithRetry.ts | 28 +++++++++--- 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/packages/core/src/renameWithRetry.test.ts b/packages/core/src/renameWithRetry.test.ts index 883b1a1f16..85877c172b 100644 --- a/packages/core/src/renameWithRetry.test.ts +++ b/packages/core/src/renameWithRetry.test.ts @@ -1,4 +1,7 @@ // @vitest-environment node +import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { catchHeldRename, renameWithRetry, renameWithRetryAsync } from "./renameWithRetry.js"; @@ -41,6 +44,59 @@ describe("renameWithRetry", () => { expect(missing.calls()).toBe(1); }); + it("gives up at once on win32 when the target is a folder with files in it, which no wait clears", async () => { + const folder = mkdtempSync(join(tmpdir(), "hf-rename-onto-folder-")); + writeFileSync(join(folder, "frame_00001.jpg"), "winner"); + try { + const rename = failing("EPERM", 99); + expect(() => renameWithRetry("a", folder, { platform: "win32", rename: rename.fn })).toThrow( + /EPERM/, + ); + expect(rename.calls()).toBe(1); + const later = failing("EPERM", 99); + await expect( + renameWithRetryAsync("a", folder, { platform: "win32", rename: async () => later.fn() }), + ).rejects.toThrow(/EPERM/); + expect(later.calls()).toBe(1); + } finally { + rmSync(folder, { recursive: true, force: true }); + } + }); + + it("still waits on win32 for an empty folder target, which may be a removed folder still held open", () => { + const folder = mkdtempSync(join(tmpdir(), "hf-rename-onto-empty-")); + try { + const rename = failing("EPERM", 2); + renameWithRetry("a", folder, { platform: "win32", rename: rename.fn }); + expect(rename.calls()).toBe(3); + } finally { + rmSync(folder, { recursive: true, force: true }); + } + }); + + it.skipIf(process.platform === "win32" || process.getuid?.() === 0)( + "keeps waiting and keeps the rename's own error when the target cannot be looked at", + () => { + const parent = mkdtempSync(join(tmpdir(), "hf-rename-unreadable-")); + const folder = join(parent, "entry"); + mkdirSync(folder); + writeFileSync(join(folder, "frame_00001.jpg"), "winner"); + chmodSync(parent, 0); + try { + const rename = failing("EPERM", 2); + renameWithRetry("a", folder, { platform: "win32", rename: rename.fn }); + expect(rename.calls()).toBe(3); + const always = failing("EPERM", 99); + expect(() => + renameWithRetry("a", folder, { platform: "win32", rename: always.fn }), + ).toThrow(/EPERM: rename/); + } finally { + chmodSync(parent, 0o700); + rmSync(parent, { recursive: true, force: true }); + } + }, + ); + it("async twin retries EPERM on win32", async () => { const rename = failing("EPERM", 2); await renameWithRetryAsync("a", "b", { platform: "win32", rename: async () => rename.fn() }); diff --git a/packages/core/src/renameWithRetry.ts b/packages/core/src/renameWithRetry.ts index 93677f60c7..268f57864b 100644 --- a/packages/core/src/renameWithRetry.ts +++ b/packages/core/src/renameWithRetry.ts @@ -1,10 +1,10 @@ import { AsyncLocalStorage } from "node:async_hooks"; -import { copyFileSync, renameSync } from "node:fs"; +import { copyFileSync, readdirSync, renameSync } from "node:fs"; import { copyFile, rename } from "node:fs/promises"; import { removeScratch } from "./removeScratch.js"; -// Windows reports EPERM/EACCES/EBUSY while antivirus or a just-exited process still holds the -// file; the hold clears within milliseconds. Total wait before giving up: about 1.3 s. +// Windows reports EPERM/EACCES/EBUSY while antivirus or a just-exited process holds the file (clears in ms; ~1.3 s of +// waits in all). EPERM onto a folder with files in it never clears; an empty one may be a removed folder still held. const RETRYABLE = new Set(["EPERM", "EACCES", "EBUSY"]); const BACKOFF_MS = [10, 20, 40, 80, 160, 320, 640]; @@ -46,12 +46,26 @@ function handOverHold(error: unknown): void { } /** What a refused rename does next: wait and try again, fall back to a copy, or give up with the error. */ -function nextStep(error: unknown, attempt: number, options: Options, platform: NodeJS.Platform) { - if (!isWindowsHold(error, platform)) return "throw"; +function nextStep( + error: unknown, + attempt: number, + options: Options, + platform: NodeJS.Platform, + to: string, +) { + if (!isWindowsHold(error, platform) || isFilledFolder(to)) return "throw"; if (attempt < BACKOFF_MS.length) return "wait"; return options.copyFallback ? "copy" : "throw"; } +function isFilledFolder(path: string): boolean { + try { + return readdirSync(path).length > 0; + } catch { + return false; + } +} + function sleepSync(ms: number): void { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); } @@ -73,7 +87,7 @@ export function renameWithRetry( doRename(from, to); return; } catch (error) { - const step = nextStep(error, attempt, options, platform); + const step = nextStep(error, attempt, options, platform, to); if (step === "throw") throw error; handOverHold(error); if (step === "wait") { @@ -108,7 +122,7 @@ export async function renameWithRetryAsync( await doRename(from, to); return; } catch (error) { - const step = nextStep(error, attempt, options, platform); + const step = nextStep(error, attempt, options, platform, to); if (step === "throw") throw error; if (step === "wait") { await new Promise((resolve) => setTimeout(resolve, BACKOFF_MS[attempt]));