From 6ba7c6e91a1a15d58ffed3cca3a651a3cdc2fa29 Mon Sep 17 00:00:00 2001 From: Frank Hamand Date: Wed, 1 Jul 2026 14:39:33 +0100 Subject: [PATCH] fix(worktree): bound git fetch during worktree creation with a timeout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #3054 added kill-timeouts to `git worktree add` and the post-checkout hook, and #3053 restructured the saga to clear the provisioning spinner and keep the task for retry when provisioning fails. Both landed on main and supersede the worktree-add/hook-timeout and spinner-clearing parts of this PR, so those are dropped here. The one hang path neither covered is the `git fetch` that precedes worktree creation — the trigger both fixes cited. It still runs unbounded inside the per-repo write lock, so a blocked fetch (network stall, unreachable remote) never settles, the lock is never released, and every later worktree creation for that repo queues behind it forever. Bound the two fetch call sites (resolveFreshBaseRef and createWorktreeForRemoteBranch) with a timeout. The fetch runs through simple-git rather than a raw spawn, so it can't use armProcessTimeout; instead it aborts via the AbortSignal executeWrite already forwards to its scoped git client, which kills the fetch subprocess and releases the write lock. On timeout the trunk path degrades to the local ref and the remote-branch path fails with a retryable error, rather than hanging. --- packages/git/src/worktree.ts | 37 ++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/git/src/worktree.ts b/packages/git/src/worktree.ts index 9636ddc1e6..b5f9e0b7e3 100644 --- a/packages/git/src/worktree.ts +++ b/packages/git/src/worktree.ts @@ -32,6 +32,7 @@ const WORKTREE_FOLDER_NAME = ".posthog-code"; const WORKTREE_ADD_TIMEOUT_MS = 120_000; const POST_CHECKOUT_HOOK_TIMEOUT_MS = 300_000; +const GIT_FETCH_TIMEOUT_MS = 120_000; export const KILL_GRACE_MS = 5_000; export function armProcessTimeout( @@ -259,9 +260,7 @@ export class WorktreeManager { const remoteRef = `${remote}/${branch}`; options?.onOutput?.(`Fetching ${remoteRef}...\n`); - const fetched = await manager.executeWrite(this.mainRepoPath, (git) => - fetchRef(git, remote, branch), - ); + const fetched = await this.fetchRefWithTimeout(remote, branch); if (!fetched) { throw new Error(`Failed to fetch branch '${branch}' from ${remote}`); } @@ -413,9 +412,7 @@ export class WorktreeManager { const remoteRef = `${remote}/${baseBranch}`; onOutput?.(`Fetching ${remoteRef}...\n`); - const fetched = await manager.executeWrite(this.mainRepoPath, (git) => - fetchRef(git, remote, baseBranch), - ); + const fetched = await this.fetchRefWithTimeout(remote, baseBranch); if (!fetched) { onOutput?.( @@ -438,6 +435,34 @@ export class WorktreeManager { return remoteRef; } + /** + * Runs `git fetch ` under the write lock with a hard timeout. + * The fetch (unlike `git worktree add`) runs through simple-git, so it can't + * use `armProcessTimeout`; instead we abort via the AbortSignal that + * `executeWrite` forwards to its scoped git client, which kills the fetch + * subprocess and releases the write lock. A blocked fetch (network stall, + * unreachable remote) would otherwise hold the lock forever and strand every + * later worktree creation for the repo. Returns false on failure or timeout + * so callers degrade gracefully rather than hang. + */ + private async fetchRefWithTimeout( + remote: string, + ref: string, + ): Promise { + const manager = getGitOperationManager(); + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(), GIT_FETCH_TIMEOUT_MS); + try { + return await manager.executeWrite( + this.mainRepoPath, + (git) => fetchRef(git, remote, ref), + { signal: controller.signal }, + ); + } finally { + clearTimeout(timer); + } + } + private spawnWorktreeAdd( args: string[], options?: { onOutput?: (data: string) => void },