From a5edd91f916b77991469c7816a503004a84c6969 Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 03:26:42 +0000 Subject: [PATCH] Delete a stopped terminal worker's copy on Windows (#10039) * fix(terminal): delete a stopped worker's copy on Windows Windows keeps a just-exited worker's files open for a moment, so the single rm in pruneTerminalWorkerRuntimes failed and left the copy until the next daemon start. Retry the delete for up to 5.5s. Co-Authored-By: Claude Opus 5.5 * fix(terminal): bound the Windows retry of a worker copy delete rm's maxRetries repeats at every directory level, so a file that stays locked deep in node-pty would hold daemon start and stop far past the intended window. Retry one rm per attempt for up to 3s instead. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../src/terminal/terminal-worker-runtime.ts | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/server/src/terminal/terminal-worker-runtime.ts b/packages/server/src/terminal/terminal-worker-runtime.ts index 7ee069647..1f24e73b0 100644 --- a/packages/server/src/terminal/terminal-worker-runtime.ts +++ b/packages/server/src/terminal/terminal-worker-runtime.ts @@ -13,6 +13,7 @@ import { import { createRequire } from "node:module"; import { access, readdir, rm } from "node:fs/promises"; import path from "node:path"; +import { setTimeout as delay } from "node:timers/promises"; import { fileURLToPath } from "node:url"; import { ensurePrivateDirectory } from "../server/private-files.js"; import { listTerminalWorkerRecords, terminalWorkerDirectory } from "./terminal-worker-record.js"; @@ -193,6 +194,28 @@ export function prepareTerminalWorkerRuntime(input: { return entry; } +// Windows keeps a just-exited worker's files open for a moment, so a delete that fails with one +// of these codes is retried for a short window. Each attempt is a single rm: rm's own maxRetries +// repeats at every directory level, so a file that stays locked deep in node-pty would hold +// daemon start and stop far past any window. +const TRANSIENT_REMOVE_ERRORS = new Set(["EPERM", "EBUSY", "EACCES", "ENOTEMPTY"]); +const TRANSIENT_REMOVE_WINDOW_MS = 3_000; +const TRANSIENT_REMOVE_PAUSE_MS = 100; + +async function removeCopy(directory: string): Promise { + const deadline = Date.now() + TRANSIENT_REMOVE_WINDOW_MS; + for (;;) { + try { + await rm(directory, { recursive: true, force: true }); + return; + } catch (error) { + const code = (error as NodeJS.ErrnoException).code; + if (!code || !TRANSIENT_REMOVE_ERRORS.has(code) || Date.now() >= deadline) throw error; + await delay(TRANSIENT_REMOVE_PAUSE_MS); + } + } +} + export interface PruneTerminalWorkerRuntimesResult { removed: string[]; /** Copies that could not be deleted (a worker that just exited can still hold files on Windows). */ @@ -236,7 +259,7 @@ export async function pruneTerminalWorkerRuntimes( ) continue; try { - await rm(directory, { recursive: true, force: true }); + await removeCopy(directory); result.removed.push(directory); } catch (error) { result.failed.push({