mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
fix(windows): release launches a dead child-process worker never started, and skip the no-op release message
This commit is contained in:
@@ -27,7 +27,7 @@ src/main/emulator/simctl-simulator-devices.ts
|
||||
src/main/emulator/simulator-app-visibility.ts
|
||||
src/main/external-editors/external-editor-launch.ts
|
||||
src/main/git/command-runner/process-capture/exec-file-capture.ts
|
||||
src/main/git/command-runner/git-stream-stdout.ts
|
||||
src/main/git/command-runner/git-stream-child-launch.ts
|
||||
src/main/git/command-runner/process-capture/spawn-command-capture.ts
|
||||
src/main/git/command-runner/process-capture/spawned-command-tree-kill.ts
|
||||
src/main/git/command-runner/host-routing/wsl-aware-spawn.ts
|
||||
|
||||
@@ -39,4 +39,12 @@ export type WorkerThreadChildEvent =
|
||||
error: WorkerThreadChildErrorPayload
|
||||
stdin?: true
|
||||
}
|
||||
| { type: 'exit' | 'close'; id: number; code: number | null; signal: NodeJS.Signals | null }
|
||||
| { type: 'exit'; id: number; code: number | null; signal: NodeJS.Signals | null }
|
||||
// 'retained': the worker still tracks the child (an owned POSIX group) until main sends 'release'.
|
||||
| {
|
||||
type: 'close'
|
||||
id: number
|
||||
code: number | null
|
||||
signal: NodeJS.Signals | null
|
||||
retained?: true
|
||||
}
|
||||
|
||||
@@ -53,6 +53,10 @@ export function serveWorkerThreadChildProcesses(port: WorkerThreadChildPort): vo
|
||||
port.postMessage({ type: 'spawn-error', id, error: toErrorPayload(error) })
|
||||
return
|
||||
}
|
||||
// Report the pid as soon as the process exists, so a worker that dies next can't leave it unaccounted for.
|
||||
if (child.pid !== undefined) {
|
||||
port.postMessage({ type: 'spawn', id, pid: child.pid })
|
||||
}
|
||||
const state: NonNullable<ReturnType<typeof children.get>> = {
|
||||
child,
|
||||
pendingOutputBytes: 0,
|
||||
@@ -84,7 +88,6 @@ export function serveWorkerThreadChildProcesses(port: WorkerThreadChildPort): vo
|
||||
port.postMessage({ type: 'error', id, error: toErrorPayload(error) })
|
||||
const postStdinError = (error: Error): void =>
|
||||
port.postMessage({ type: 'error', id, error: toErrorPayload(error), stdin: true })
|
||||
child.on('spawn', () => port.postMessage({ type: 'spawn', id, pid: child.pid }))
|
||||
child.stdout?.on('data', (chunk: Buffer) => postOutput('stdout', chunk))
|
||||
child.stderr?.on('data', (chunk: Buffer) => postOutput('stderr', chunk))
|
||||
// An unhandled stream error would take the whole worker, and every other child, down.
|
||||
@@ -95,10 +98,11 @@ export function serveWorkerThreadChildProcesses(port: WorkerThreadChildPort): vo
|
||||
child.on('exit', (code, signal) => port.postMessage({ type: 'exit', id, code, signal }))
|
||||
child.on('close', (code, signal) => {
|
||||
state.closed = true
|
||||
if (!state.group || state.groupTerminated) {
|
||||
const retained = Boolean(state.group) && !state.groupTerminated
|
||||
if (!retained) {
|
||||
children.delete(id)
|
||||
}
|
||||
port.postMessage({ type: 'close', id, code, signal })
|
||||
port.postMessage({ type: 'close', id, code, signal, ...(retained ? { retained: true } : {}) })
|
||||
if (state.stopping) {
|
||||
state.group?.close()
|
||||
}
|
||||
|
||||
@@ -255,6 +255,30 @@ describe('WorkerThreadChildProcessHost with a stalled worker', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('WorkerThreadChildProcessHost when the worker dies', () => {
|
||||
it('fails a launch the worker never started, so its caller is released', async () => {
|
||||
const worker = Object.assign(new EventEmitter(), {
|
||||
postMessage: () => {},
|
||||
unref: () => {},
|
||||
terminate: async () => 0
|
||||
})
|
||||
const host = new WorkerThreadChildProcessHost(() => worker)
|
||||
const child = host.trySpawn({ program: process.execPath, args: ['-e', ''] })
|
||||
if (!child) {
|
||||
throw new Error('worker launch was refused')
|
||||
}
|
||||
const errors: string[] = []
|
||||
child.on('error', (error) => errors.push(error.message))
|
||||
const closed = waitForClose(child)
|
||||
|
||||
worker.emit('error', new Error('boom'))
|
||||
worker.emit('exit', 1)
|
||||
|
||||
await expect(closed).resolves.toEqual([-1, null])
|
||||
expect(errors).toEqual(['Child process worker failed: boom'])
|
||||
})
|
||||
})
|
||||
|
||||
// Real thread death closes child pipes; abandoned probes depend on that graceful stop instead of PID-addressed killing.
|
||||
describe('WorkerThreadChildProcessHost on a real worker thread', () => {
|
||||
let directory = ''
|
||||
|
||||
@@ -146,9 +146,14 @@ export class WorkerThreadChildProcess extends EventEmitter {
|
||||
|
||||
/** The worker is gone: a native-like child closes only once a tree kill proves it ended, so callers keep their locks while it may still run; others keep the error-only contract. */
|
||||
abandon(message: string): void {
|
||||
this.emitErrorIfObserved({ message })
|
||||
const pid = this.pid
|
||||
// No pid proves nothing (the worker may have created the process unreported); an exited root's pid may be reused.
|
||||
if (this.nativeLike && !this.closed && pid === undefined) {
|
||||
// The worker posts the pid as soon as the process exists and its messages drain before exit, so no pid means no process.
|
||||
this.receive({ type: 'spawn-error', id: this.id, error: { message } })
|
||||
return
|
||||
}
|
||||
this.emitErrorIfObserved({ message })
|
||||
// An exited root's pid may be reused.
|
||||
if (
|
||||
!this.nativeLike ||
|
||||
this.closed ||
|
||||
@@ -189,14 +194,26 @@ export class WorkerThreadChildProcessHost {
|
||||
// A worker that dies before answering any launch is broken (e.g. a bad entry); stop routing optional launches to it.
|
||||
private servedBy: WorkerRequestTransport | null = null
|
||||
private brokenBeforeServing = false
|
||||
private workerError: string | null = null
|
||||
|
||||
constructor(factory: WorkerThreadFactory) {
|
||||
this.host = new LazyWorkerThreadHost<WorkerThreadChildEvent>({
|
||||
factory,
|
||||
idleTeardownMs: IDLE_TEARDOWN_MS,
|
||||
onMessage: (event) => this.route(event),
|
||||
onError: (error) => this.abandonAll(`Child process worker failed: ${error.message}`),
|
||||
onExit: (code) => this.abandonAll(`Child process worker exited with code ${code}`),
|
||||
// 'exit' follows 'error' after the worker's queued messages are delivered; abandoning on 'error' would drop them.
|
||||
onError: (error) => {
|
||||
this.workerError ??= error.message
|
||||
},
|
||||
onExit: (code) => {
|
||||
const error = this.workerError
|
||||
this.workerError = null
|
||||
this.abandonAll(
|
||||
error === null
|
||||
? `Child process worker exited with code ${code}`
|
||||
: `Child process worker failed: ${error}`
|
||||
)
|
||||
},
|
||||
isIdle: () => this.children.size === 0,
|
||||
onUnavailable: (error) =>
|
||||
console.warn(
|
||||
@@ -229,7 +246,11 @@ export class WorkerThreadChildProcessHost {
|
||||
|
||||
/** A native-like child, or null when the worker cannot start or proved broken, so the caller spawns in-thread instead. */
|
||||
trySpawn(spec: WorkerThreadChildSpec): WorkerThreadChildProcess | null {
|
||||
if (this.brokenBeforeServing || this.pendingLaunches() >= MAX_PENDING_OPTIONAL_LAUNCHES) {
|
||||
if (
|
||||
this.brokenBeforeServing ||
|
||||
this.workerError !== null ||
|
||||
this.pendingLaunches() >= MAX_PENDING_OPTIONAL_LAUNCHES
|
||||
) {
|
||||
return null
|
||||
}
|
||||
return this.host.ensure() ? this.spawn(spec, true) : null
|
||||
@@ -265,7 +286,7 @@ export class WorkerThreadChildProcessHost {
|
||||
return
|
||||
}
|
||||
child.receive(event)
|
||||
if (event.type === 'close') {
|
||||
if (event.type === 'close' && event.retained) {
|
||||
this.post({ type: 'release', id: event.id })
|
||||
}
|
||||
if (
|
||||
|
||||
Reference in New Issue
Block a user