Skip to content

Commit c96d092

Browse files
fix(desktop): preserve signals in backend exit logs
1 parent c30b337 commit c96d092

2 files changed

Lines changed: 25 additions & 34 deletions

File tree

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,17 +431,15 @@ describe("DesktopBackendManager", () => {
431431
}),
432432
);
433433

434-
it.effect("reports a backend killed by a signal as a signal exit", () =>
434+
it.effect("reports a SIGKILL exit without an exit code", () =>
435435
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.
438436
const exitCause = PlatformError.systemError({
439437
_tag: "Unknown",
440438
module: "ChildProcess",
441439
method: "exitCode",
442440
cause: new Error("Process interrupted due to receipt of signal: 'SIGKILL'"),
443441
});
444-
const spawnerLayer = Layer.succeed(
442+
const layerSpawner = Layer.succeed(
445443
ChildProcessSpawner.ChildProcessSpawner,
446444
ChildProcessSpawner.make(() =>
447445
Effect.succeed(makeProcess({ exitCode: Effect.fail(exitCause) })),
@@ -451,10 +449,9 @@ describe("DesktopBackendManager", () => {
451449
const exit = yield* DesktopBackendManager.runBackendProcess({
452450
...baseConfig,
453451
desktopTelemetryStream: Stream.empty,
454-
}).pipe(Effect.scoped, Effect.provide(Layer.merge(spawnerLayer, healthyHttpClientLayer)));
452+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerSpawner, layerHealthyHttpClient)));
455453

456-
assert.isTrue(Option.isNone(exit.code));
457-
assert.equal(exit.reason, "killed by SIGKILL");
454+
assert.deepEqual(exit, { code: Option.none(), reason: "killed by SIGKILL" });
458455
}),
459456
);
460457

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,6 @@ export class BackendProcessExitStatusError extends Schema.TaggedError<BackendPro
178178
}
179179
}
180180

181-
// Node reports no exit code for a child killed by a signal, so Effect's
182-
// spawner fails `exitCode` and leaves the signal name only in the cause's
183-
// message. Returns that signal, or undefined for a genuine read failure.
184-
const SIGNAL_EXIT_PATTERN = /receipt of signal: '(SIG[A-Z0-9]+)'/;
185-
function signalFromExitCodeFailure(error: PlatformError.PlatformError): string | undefined {
186-
return error.cause instanceof Error
187-
? SIGNAL_EXIT_PATTERN.exec(error.cause.message)?.[1]
188-
: undefined;
189-
}
190-
191181
export const BackendProcessError = Schema.Union([
192182
BackendProcessBootstrapEncodeError,
193183
BackendProcessSpawnError,
@@ -578,27 +568,31 @@ export const runBackendProcess = Effect.fn("runBackendProcess")(function* (
578568
yield* probeReadiness().pipe(Effect.repeat({ while: (ready) => !ready }), Effect.forkScoped);
579569

580570
const exit = yield* handle.exitCode.pipe(
581-
Effect.map((exitCode): BackendProcessExit => ({
571+
Effect.map((exitCode) => ({
582572
code: Option.some(exitCode),
583573
reason: `code=${exitCode}`,
584574
})),
585575
Effect.catch((cause) => {
586-
const signal = signalFromExitCodeFailure(cause);
587-
return signal === undefined
588-
? Effect.fail(
589-
new BackendProcessExitStatusError({
590-
executablePath: options.executablePath,
591-
entryPath: options.entryPath,
592-
cwd: options.cwd,
593-
httpBaseUrl: options.httpBaseUrl,
594-
pid: Number(handle.pid),
595-
cause,
596-
}),
597-
)
598-
: Effect.succeed<BackendProcessExit>({
599-
code: Option.none(),
600-
reason: `killed by ${signal}`,
601-
});
576+
// Effect's Node spawner exposes signal exits only through this cause message.
577+
const signal =
578+
cause.cause instanceof Error
579+
? /^Process interrupted due to receipt of signal: '(SIG[A-Z0-9]+)'$/.exec(
580+
cause.cause.message,
581+
)?.[1]
582+
: undefined;
583+
if (signal !== undefined) {
584+
return Effect.succeed({ code: Option.none(), reason: `killed by ${signal}` });
585+
}
586+
return Effect.fail(
587+
new BackendProcessExitStatusError({
588+
executablePath: options.executablePath,
589+
entryPath: options.entryPath,
590+
cwd: options.cwd,
591+
httpBaseUrl: options.httpBaseUrl,
592+
pid: Number(handle.pid),
593+
cause,
594+
}),
595+
);
602596
}),
603597
Effect.exit,
604598
);

0 commit comments

Comments
 (0)