Skip to content

Commit 70f39a3

Browse files
SunkenInTimeclaude
andcommitted
fix(desktop): a backend killed by a signal is logged as killed, not as a read error
Effect's Node spawner fails `exitCode` when the child exits with a null code and a signal, and runBackendProcess mapped every such failure to BackendProcessExitStatusError. A kill -9 was persisted to server-child.log and the restart event as "Failed to read the exit status", with the signal left only in the rotating trace. Recognize the signal failure and report it as an exit ("killed by SIGKILL"); genuine read failures keep the existing error. Fixes #15359 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 243d1e7 commit 70f39a3

2 files changed

Lines changed: 60 additions & 16 deletions

File tree

‎apps/desktop/src/backend/DesktopBackendManager.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,33 @@ describe("DesktopBackendManager", () => {
431431
}),
432432
);
433433

434+
it.effect("reports a backend killed by a signal as a signal exit", () =>
435+
Effect.gen(function* () {
436+
// The shape Effect's Node spawner fails `exitCode` with when the child
437+
// exits with a null code and a signal.
438+
const exitCause = PlatformError.systemError({
439+
_tag: "Unknown",
440+
module: "ChildProcess",
441+
method: "exitCode",
442+
cause: new Error("Process interrupted due to receipt of signal: 'SIGKILL'"),
443+
});
444+
const spawnerLayer = Layer.succeed(
445+
ChildProcessSpawner.ChildProcessSpawner,
446+
ChildProcessSpawner.make(() =>
447+
Effect.succeed(makeProcess({ exitCode: Effect.fail(exitCause) })),
448+
),
449+
);
450+
451+
const exit = yield* DesktopBackendManager.runBackendProcess({
452+
...baseConfig,
453+
desktopTelemetryStream: Stream.empty,
454+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(spawnerLayer, healthyHttpClientLayer)));
455+
456+
assert.isTrue(Option.isNone(exit.code));
457+
assert.equal(exit.reason, "killed by SIGKILL");
458+
}),
459+
);
460+
434461
it.effect("reports output stream failures with process and stream context", () =>
435462
Effect.gen(function* () {
436463
const outputCause = PlatformError.systemError({

‎apps/desktop/src/backend/DesktopBackendManager.ts‎

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ export class BackendProcessExitStatusError extends Schema.TaggedError<BackendPro
213213
}
214214
}
215215

216+
// Node reports no exit code for a child killed by a signal, so Effect's
217+
// spawner fails `exitCode` and leaves the signal name only in the cause's
218+
// message. Returns that signal, or undefined for a genuine read failure.
219+
const SIGNAL_EXIT_PATTERN = /receipt of signal: '(SIG[A-Z0-9]+)'/;
220+
function signalFromExitCodeFailure(error: PlatformError.PlatformError): string | undefined {
221+
return error.cause instanceof Error
222+
? SIGNAL_EXIT_PATTERN.exec(error.cause.message)?.[1]
223+
: undefined;
224+
}
225+
216226
export const BackendProcessError = Schema.Union([
217227
BackendProcessBootstrapEncodeError,
218228
BackendProcessSpawnError,
@@ -596,17 +606,28 @@ export const runBackendProcess = Effect.fn("runBackendProcess")(function* (
596606
yield* probeReadiness().pipe(Effect.repeat({ while: (ready) => !ready }), Effect.forkScoped);
597607

598608
const exit = yield* handle.exitCode.pipe(
599-
Effect.mapError(
600-
(cause) =>
601-
new BackendProcessExitStatusError({
602-
executablePath: options.executablePath,
603-
entryPath: options.entryPath,
604-
cwd: options.cwd,
605-
httpBaseUrl: options.httpBaseUrl,
606-
pid: Number(handle.pid),
607-
cause,
608-
}),
609-
),
609+
Effect.map((exitCode): BackendProcessExit => ({
610+
code: Option.some(exitCode),
611+
reason: `code=${exitCode}`,
612+
})),
613+
Effect.catch((cause) => {
614+
const signal = signalFromExitCodeFailure(cause);
615+
return signal === undefined
616+
? Effect.fail(
617+
new BackendProcessExitStatusError({
618+
executablePath: options.executablePath,
619+
entryPath: options.entryPath,
620+
cwd: options.cwd,
621+
httpBaseUrl: options.httpBaseUrl,
622+
pid: Number(handle.pid),
623+
cause,
624+
}),
625+
)
626+
: Effect.succeed<BackendProcessExit>({
627+
code: Option.none(),
628+
reason: `killed by ${signal}`,
629+
});
630+
}),
610631
Effect.exit,
611632
);
612633
yield* options.onExitObserved?.() ?? Effect.void;
@@ -620,11 +641,7 @@ export const runBackendProcess = Effect.fn("runBackendProcess")(function* (
620641
if (Exit.isFailure(exit)) {
621642
return yield* Effect.failCause(exit.cause);
622643
}
623-
const exitCode = exit.value;
624-
return {
625-
code: Option.some(exitCode),
626-
reason: `code=${exitCode}`,
627-
} satisfies BackendProcessExit;
644+
return exit.value;
628645
});
629646

630647
// Factory for one pooled backend instance. The returned instance owns

0 commit comments

Comments
 (0)