Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: retry copyFileGuarded on transient Windows file locks (#12886)
Adam Eivyfb273624e22f
diff --git a/server/lib/fileCore.js b/server/lib/fileCore.jsindex 9981ae2207..1138f62b57 100644--- a/server/lib/fileCore.js+++ b/server/lib/fileCore.js@@ -39,6 +39,9 @@ const WIN_RETRY_DELAY_MS = 10;// rename(2) failures that mean "the destination is momentarily locked", not// "this rename can never work".const WIN_RENAME_LOCK_CODES = ['EPERM', 'EACCES', 'EEXIST', 'EBUSY'];+// copyfile(2) has no EEXIST case (the destination is overwritten); a held+// source or destination surfaces as one of these.+const WIN_COPY_LOCK_CODES = ['EPERM', 'EACCES', 'EBUSY'];const FILE_WATCH_FALLBACK_POLL_MS = 5000;// Directories whose fsync already failed once this process, so a filesystem@@ -369,10 +372,24 @@ export async function appendFileGuarded(filePath, data, options) {return appendFile(filePath, data, options);}-/** Guarded `fs/promises.copyFile` — the DESTINATION is what gets written. */-export async function copyFileGuarded(src, dest, mode) {+/**+ * Guarded `fs/promises.copyFile` — the DESTINATION is what gets written. On+ * Windows a transient lock (AV scan, indexer) on either file is retried a+ * bounded number of times; a persistent lock rethrows the original error.+ */+export async function copyFileGuarded(src, dest, mode, {+ attempts = WIN_RETRY_ATTEMPTS,+ delayMs = WIN_RETRY_DELAY_MS,+} = {}) {if (isVitestRunner()) (await loadGuard()).assertNotRealDataWrite(dest, 'copyFile');- return copyFile(src, dest, mode);+ for (let attempt = 1; ; attempt += 1) {+ try {+ return await copyFile(src, dest, mode);+ } catch (err) {+ if (!isWindows() || attempt >= attempts || !WIN_COPY_LOCK_CODES.includes(err.code)) throw err;+ await sleep(delayMs);+ }+ }}/** Guarded `fs/promises.rename` — the DESTINATION is what gets replaced. */diff --git a/server/lib/fileCore.test.js b/server/lib/fileCore.test.jsindex 77e871654d..e7066061d7 100644--- a/server/lib/fileCore.test.js+++ b/server/lib/fileCore.test.js@@ -1,5 +1,17 @@-import { describe, expect, it } from 'vitest';-import { parseFilesystemStats } from './fileCore.js';+import { afterEach, describe, expect, it, vi } from 'vitest';+import { mkdtemp, readFile, rm, writeFile } from 'fs/promises';+import * as fsPromises from 'fs/promises';+import { tmpdir } from 'os';+import { join } from 'path';+import { pinPlatform } from './testHelper.js';+import { copyFileGuarded, parseFilesystemStats } from './fileCore.js';++// Lets a test force `copyFile` to throw a Windows lock errno, which cannot be+// provoked portably; delegates to the real implementation by default.+vi.mock('fs/promises', async (importOriginal) => {+ const actual = await importOriginal();+ return { ...actual, copyFile: vi.fn((...args) => actual.copyFile(...args)) };+});describe('parseFilesystemStats', () => {it('derives canonical byte metrics and compatibility aliases from statfs counts', () => {@@ -42,3 +54,64 @@ describe('parseFilesystemStats', () => {expect(parseFilesystemStats({ blocks: 1000, bavail: 2, bsize: 1 })?.usagePercent).toBe(100);});});++describe('copyFileGuarded Windows lock retry', () => {+ let restorePlatform = () => {};+ afterEach(async () => {+ restorePlatform();+ const real = await vi.importActual('fs/promises');+ fsPromises.copyFile.mockReset();+ fsPromises.copyFile.mockImplementation((...args) => real.copyFile(...args));+ });++ const lockErr = (code) => Object.assign(new Error(`${code}: locked`), { code });+ const withFiles = async (fn) => {+ const dir = await mkdtemp(join(tmpdir(), 'portos-copy-retry-'));+ try {+ await writeFile(join(dir, 'src.bin'), 'payload');+ return await fn(join(dir, 'src.bin'), join(dir, 'dest.bin'));+ } finally {+ await rm(dir, { recursive: true, force: true });+ }+ };++ it('succeeds after transient EBUSY locks on win32', async () => {+ restorePlatform = pinPlatform('win32');+ await withFiles(async (src, dest) => {+ fsPromises.copyFile.mockRejectedValueOnce(lockErr('EBUSY')).mockRejectedValueOnce(lockErr('EBUSY'));+ await copyFileGuarded(src, dest, undefined, { delayMs: 0 });+ expect(fsPromises.copyFile).toHaveBeenCalledTimes(3);+ expect(await readFile(dest, 'utf8')).toBe('payload');+ });+ });++ it('rethrows the original error when the lock persists past the bound', async () => {+ restorePlatform = pinPlatform('win32');+ await withFiles(async (src, dest) => {+ const err = lockErr('EBUSY');+ fsPromises.copyFile.mockRejectedValue(err);+ await expect(copyFileGuarded(src, dest, undefined, { attempts: 3, delayMs: 0 })).rejects.toBe(err);+ expect(fsPromises.copyFile).toHaveBeenCalledTimes(3);+ });+ });++ it('does not retry non-lock errors', async () => {+ restorePlatform = pinPlatform('win32');+ await withFiles(async (src, dest) => {+ const err = lockErr('ENOENT');+ fsPromises.copyFile.mockRejectedValue(err);+ await expect(copyFileGuarded(src, dest, undefined, { delayMs: 0 })).rejects.toBe(err);+ expect(fsPromises.copyFile).toHaveBeenCalledTimes(1);+ });+ });++ it('does not retry lock errors off win32', async () => {+ restorePlatform = pinPlatform('linux');+ await withFiles(async (src, dest) => {+ const err = lockErr('EBUSY');+ fsPromises.copyFile.mockRejectedValue(err);+ await expect(copyFileGuarded(src, dest, undefined, { delayMs: 0 })).rejects.toBe(err);+ expect(fsPromises.copyFile).toHaveBeenCalledTimes(1);+ });+ });+});