mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
fix(git): rerun unstarted worker taskkills from main as one batched launch
When a dying worker leaves several taskkills it never started, main reruns them together, so a worker death costs one launch on main.
This commit is contained in:
+1
-1
@@ -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' })
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<WorkerThreadChildProcess, Promise<void>>()
|
||||
// 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<void> = 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<boolean, { pids: Set<number>; ended: Promise<boolean> }>()
|
||||
|
||||
function batchTaskkill(pids: readonly number[], onWorker: boolean): Promise<boolean> {
|
||||
let batch = taskkillBatches.get(onWorker)
|
||||
if (!batch) {
|
||||
const collected = new Set<number>()
|
||||
const ended = new Promise<boolean>((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)
|
||||
|
||||
Reference in New Issue
Block a user