diff --git a/config/process-host-direct-import-baseline.txt b/config/process-host-direct-import-baseline.txt index 6bd03d5b125..464b6e9696f 100644 --- a/config/process-host-direct-import-baseline.txt +++ b/config/process-host-direct-import-baseline.txt @@ -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 diff --git a/src/main/worker-thread-child-process/worker-thread-child-process-protocol.ts b/src/main/worker-thread-child-process/worker-thread-child-process-protocol.ts index 7e2492ed5cb..a66256bc156 100644 --- a/src/main/worker-thread-child-process/worker-thread-child-process-protocol.ts +++ b/src/main/worker-thread-child-process/worker-thread-child-process-protocol.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 + } diff --git a/src/main/worker-thread-child-process/worker-thread-child-process-server.ts b/src/main/worker-thread-child-process/worker-thread-child-process-server.ts index 0c8949b5057..9ec5f2b564d 100644 --- a/src/main/worker-thread-child-process/worker-thread-child-process-server.ts +++ b/src/main/worker-thread-child-process/worker-thread-child-process-server.ts @@ -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> = { 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() } diff --git a/src/main/worker-thread-child-process/worker-thread-child-process.test.ts b/src/main/worker-thread-child-process/worker-thread-child-process.test.ts index f2b6c93e6a6..ede05a7d042 100644 --- a/src/main/worker-thread-child-process/worker-thread-child-process.test.ts +++ b/src/main/worker-thread-child-process/worker-thread-child-process.test.ts @@ -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 = '' diff --git a/src/main/worker-thread-child-process/worker-thread-child-process.ts b/src/main/worker-thread-child-process/worker-thread-child-process.ts index c711b7019fd..f10e03a6920 100644 --- a/src/main/worker-thread-child-process/worker-thread-child-process.ts +++ b/src/main/worker-thread-child-process/worker-thread-child-process.ts @@ -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({ 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 (