diff --git a/src/main/git/command-runner/process-capture/exec-file-capture-windows-worker.test.ts b/src/main/git/command-runner/process-capture/exec-file-capture-windows-worker.test.ts index 68e6f448d19..1d3477f214b 100644 --- a/src/main/git/command-runner/process-capture/exec-file-capture-windows-worker.test.ts +++ b/src/main/git/command-runner/process-capture/exec-file-capture-windows-worker.test.ts @@ -210,7 +210,7 @@ describe('Windows process creation off the calling loop', () => { code: WORKER_CHILD_ABANDONED_CODE } }) - expect(treeKills.via).toEqual(['worker', 'main']) + await vi.waitFor(() => expect(treeKills.via).toEqual(['worker', 'main'])) expect(treeKills.args[1]).toEqual(['/pid', '4242', '/t', '/f']) treeKills.killers[1]!.emit('close', 0) await expect(pending).rejects.toMatchObject({ code: 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' }) diff --git a/src/main/git/command-runner/process-capture/spawned-command-tree-kill.test.ts b/src/main/git/command-runner/process-capture/spawned-command-tree-kill.test.ts index 613711edfac..6e0ef14f3b9 100644 --- a/src/main/git/command-runner/process-capture/spawned-command-tree-kill.test.ts +++ b/src/main/git/command-runner/process-capture/spawned-command-tree-kill.test.ts @@ -200,6 +200,31 @@ describe('Git command tree termination', () => { expect(spawnMock).not.toHaveBeenCalled() expect(rootKill).toHaveBeenCalledOnce() }) + it('reruns every taskkill a dying worker never started with one launch from main', async () => { + workerTaskkill.available = true + const children = [1234, 1235].map((pid) => childWithPid(pid)) + const pending = Promise.all(children.map((child) => killSpawnedCommandTree(child))) + const killer = childWithPid(5678) + spawnMock.mockReturnValue(killer) + for (const queued of workerTaskkill.killers) { + queued.receive({ + type: 'error', + id: 99, + error: { + message: 'Child process worker exited with code 1', + code: WORKER_CHILD_ABANDONED_CODE + } + }) + } + await vi.waitFor(() => expect(spawnMock).toHaveBeenCalledOnce()) + expect(spawnMock).toHaveBeenCalledWith( + 'taskkill', + ['/pid', '1234', '/pid', '1235', '/t', '/f'], + { stdio: 'ignore', windowsHide: true } + ) + killer.emit('close', 0) + await pending + }) it('still kills the tree when the worker reports the PID after the wait', async () => { vi.useFakeTimers() try { diff --git a/src/main/git/command-runner/process-capture/spawned-command-tree-kill.ts b/src/main/git/command-runner/process-capture/spawned-command-tree-kill.ts index 7502462f3f8..577449eb12f 100644 --- a/src/main/git/command-runner/process-capture/spawned-command-tree-kill.ts +++ b/src/main/git/command-runner/process-capture/spawned-command-tree-kill.ts @@ -13,9 +13,27 @@ const WORKER_PID_WAIT_MS = 5_000 const WORKER_TASKKILL_LAUNCH_ALLOWANCE_MS = 30_000 // One tree kill per worker child: a second could walk a reused PID after the first ended the tree. const workerTreeKills = new WeakMap>() -// PIDs a dying worker abandons in one turn share a taskkill, so main pays one launch per worker death. -let abandonedPids: number[] | null = null -let abandonedTreeKill: Promise = Promise.resolve() +// Tree kills a dying worker leaves in one turn share one taskkill per host, so main pays at most one launch. +const taskkillBatches = new Map; ended: Promise }>() + +function batchTaskkill(pids: readonly number[], onWorker: boolean): Promise { + let batch = taskkillBatches.get(onWorker) + if (!batch) { + const collected = new Set() + const ended = new Promise((resolve) => { + queueMicrotask(() => { + taskkillBatches.delete(onWorker) + void runTaskkill([...collected], onWorker).then(resolve) + }) + }) + batch = { pids: collected, ended } + taskkillBatches.set(onWorker, batch) + } + for (const pid of pids) { + batch.pids.add(pid) + } + return batch.ended +} /** A dead worker can no longer stop its children; end a still-running Windows tree whose PID it reported. */ export function killAbandonedWorkerChildTree( @@ -35,21 +53,11 @@ export function killAbandonedWorkerChildTree( ) { return Promise.resolve() } - if (!abandonedPids) { - const pids: number[] = [] - abandonedPids = pids - abandonedTreeKill = new Promise((resolve) => { - queueMicrotask(() => { - abandonedPids = null - void runTaskkill(pids).then(() => resolve()) - }) - }) - } - abandonedPids.push(pid) + const kill = batchTaskkill([pid], canSpawnOnWorkerThread()).then(() => {}) if (child instanceof WorkerThreadChildProcess) { - workerTreeKills.set(child, abandonedTreeKill) + workerTreeKills.set(child, kill) } - return abandonedTreeKill + return kill } export function killSpawnedCommandTree( @@ -141,7 +149,7 @@ function runTaskkill(pids: number[], onWorker = canSpawnOnWorkerThread()): Promi clearTimeout(timer) } killer.removeAllListeners() - resolve(runTaskkill(pids, false)) + resolve(batchTaskkill(pids, false)) return } finish(false)