Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
fix(desktop): retry transient Windows runtime file locks
Leggett2ebfdb86d0d2
diff --git a/apps/desktop/scripts/install-creator-runtime-dependencies.mjs b/apps/desktop/scripts/install-creator-runtime-dependencies.mjsindex cf31c29d..a2a65546 100644--- a/apps/desktop/scripts/install-creator-runtime-dependencies.mjs+++ b/apps/desktop/scripts/install-creator-runtime-dependencies.mjs@@ -21,6 +21,7 @@ import { fileURLToPath } from 'node:url';import { creatorRuntimeReleases, resolveYtDlpRelease } from './creator-runtime-releases.mjs';import { verifyRuntimeExecutable } from './runtime-executable-check.mjs';import { installCreatorNlpDependencies } from './creator-nlp-runtime.mjs';+import { retryWindowsFileOperation } from './windows-file-retry.mjs';const scriptDir = dirname(fileURLToPath(import.meta.url));const rootDir = resolve(scriptDir, '../../..');@@ -130,16 +131,25 @@ function installAsset(name, asset, path) {}const temporary = `${path}.${process.pid}.partial`;- rmSync(temporary, { force: true });+ retryWindowsFileOperation(() => rmSync(temporary, { force: true }));console.log(`[creator-runtime] Downloading ${name} ${asset.version}`);+ let installationError;try {downloadAsset(name, asset, temporary);if (asset.executable) chmodExecutable(temporary);if (asset.verify) verifyExecutable(name, asset, temporary);- rmSync(path, { recursive: true, force: true });- renameSync(temporary, path);+ retryWindowsFileOperation(() => rmSync(path, { recursive: true, force: true }));+ retryWindowsFileOperation(() => renameSync(temporary, path));+ } catch (error) {+ installationError = error;+ throw error;} finally {- rmSync(temporary, { force: true });+ try {+ retryWindowsFileOperation(() => rmSync(temporary, { force: true }));+ } catch (error) {+ if (!installationError) throw error;+ console.warn(`[creator-runtime] Unable to remove temporary download ${temporary}: ${error.message}`);+ }}}diff --git a/apps/desktop/scripts/windows-file-retry.mjs b/apps/desktop/scripts/windows-file-retry.mjsnew file mode 100644index 00000000..bf803f75--- /dev/null+++ b/apps/desktop/scripts/windows-file-retry.mjs@@ -0,0 +1,19 @@+const transientCodes = new Set(['EBUSY', 'EPERM', 'EACCES']);+const waitBuffer = new Int32Array(new SharedArrayBuffer(4));++export function retryWindowsFileOperation(operation, {+ platform = process.platform,+ wait = milliseconds => Atomics.wait(waitBuffer, 0, 0, milliseconds)+} = {}) {+ for (let attempt = 0; ; attempt += 1) {+ try {+ return operation();+ } catch (error) {+ // Executable checks and Windows scanners can briefly retain file handles.+ if (platform !== 'win32' || !transientCodes.has(error?.code) || attempt >= 8) {+ throw error;+ }+ wait(100 * (attempt + 1));+ }+ }+}diff --git a/apps/desktop/test/creator-runtime-updates.test.mjs b/apps/desktop/test/creator-runtime-updates.test.mjsindex bd46ad71..e61f9e75 100644--- a/apps/desktop/test/creator-runtime-updates.test.mjs+++ b/apps/desktop/test/creator-runtime-updates.test.mjs@@ -131,7 +131,9 @@ describe('Creator Runtime dependency updates', () => {});afterEach(() => {+ vi.doUnmock('node:fs');vi.doUnmock('node:child_process');+ vi.doUnmock('../scripts/windows-file-retry.mjs');vi.doUnmock('../scripts/creator-runtime-releases.mjs');vi.doUnmock('../scripts/creator-nlp-runtime.mjs');vi.unstubAllEnvs();@@ -141,6 +143,44 @@ describe('Creator Runtime dependency updates', () => {const install = () => import('../scripts/install-creator-runtime-dependencies.mjs');+ it('installs the verified download after a Windows rename lock is released', async () => {+ const rename = vi.fn();+ vi.doMock('node:fs', async importOriginal => {+ const fs = await importOriginal();+ rename.mockImplementationOnce(() => { throw Object.assign(new Error('locked'), { code: 'EBUSY' }); })+ .mockImplementation(fs.renameSync);+ return { ...fs, renameSync: rename };+ });+ vi.doMock('../scripts/windows-file-retry.mjs', async importOriginal => {+ const { retryWindowsFileOperation } = await importOriginal();+ return { retryWindowsFileOperation: operation => retryWindowsFileOperation(operation, { platform: 'win32', wait: () => {} }) };+ });+ await install();+ expect(rename).toHaveBeenCalledTimes(2);+ expect(readFileSync(join(root, 'yt-dlp'), 'utf8')).toBe(contents(newVersion));+ expect(JSON.parse(readFileSync(join(root, 'versions.json'), 'utf8')).dependencies.ytDlp).toEqual(cache(newVersion));+ expect(readdirSync(root).some(name => name.endsWith('.partial'))).toBe(false);+ });++ it('preserves the checksum error and previous cache when partial-file cleanup is locked', async () => {+ downloadContents = 'corrupt download';+ const previous = readFileSync(join(root, 'versions.json'), 'utf8');+ const warning = vi.spyOn(console, 'warn').mockImplementation(() => undefined);+ vi.doMock('node:fs', async importOriginal => {+ const fs = await importOriginal();+ return { ...fs, rmSync: (path, options) => {+ if (String(path).endsWith('.partial') && fs.existsSync(path)) {+ throw Object.assign(new Error('temporary executable remains locked'), { code: 'EBUSY' });+ }+ return fs.rmSync(path, options);+ } };+ });+ await expect(install()).rejects.toThrow(/SHA-256 mismatch/);+ expect(readFileSync(join(root, 'yt-dlp'), 'utf8')).toBe(contents(oldVersion));+ expect(readFileSync(join(root, 'versions.json'), 'utf8')).toBe(previous);+ expect(warning).toHaveBeenCalledWith(expect.stringContaining('temporary executable remains locked'));+ });+it.each([['ghs_test_primary', 'ghs_test_fallback', 'ghs_test_primary'],['', 'ghs_test_fallback', 'ghs_test_fallback']diff --git a/apps/desktop/test/windows-file-retry.test.mjs b/apps/desktop/test/windows-file-retry.test.mjsnew file mode 100644index 00000000..e4f93852--- /dev/null+++ b/apps/desktop/test/windows-file-retry.test.mjs@@ -0,0 +1,35 @@+import { describe, expect, it, vi } from 'vitest';+import { retryWindowsFileOperation } from '../scripts/windows-file-retry.mjs';++describe('Windows executable file locks', () => {+ it.each(['EBUSY', 'EPERM', 'EACCES'])('retries a temporary %s lock until the operation succeeds', code => {+ const locked = Object.assign(new Error('file is locked'), { code });+ const operation = vi.fn().mockImplementationOnce(() => { throw locked; })+ .mockImplementationOnce(() => { throw locked; }).mockReturnValue('installed');+ const wait = vi.fn();+ expect(retryWindowsFileOperation(operation, { platform: 'win32', wait })).toBe('installed');+ expect(operation).toHaveBeenCalledTimes(3);+ expect(wait.mock.calls).toEqual([[100], [200]]);+ });++ it('stops after bounded retries and retains the original filesystem error', () => {+ const locked = Object.assign(new Error('file remains locked'), { code: 'EBUSY' });+ const operation = vi.fn(() => { throw locked; });+ const wait = vi.fn();+ expect(() => retryWindowsFileOperation(operation, { platform: 'win32', wait })).toThrow(locked);+ expect(operation).toHaveBeenCalledTimes(9);+ expect(wait).toHaveBeenCalledTimes(8);+ expect(wait.mock.calls.reduce((sum, [delay]) => sum + delay, 0)).toBe(3600);+ });++ it.each([+ ['win32', 'ENOSPC'], ['win32', 'EIO'], ['darwin', 'EBUSY'], ['linux', 'EACCES']+ ])('fails immediately for %s / %s', (platform, code) => {+ const error = Object.assign(new Error('filesystem failure'), { code });+ const operation = vi.fn(() => { throw error; });+ const wait = vi.fn();+ expect(() => retryWindowsFileOperation(operation, { platform, wait })).toThrow(error);+ expect(operation).toHaveBeenCalledTimes(1);+ expect(wait).not.toHaveBeenCalled();+ });+});