Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions apps/desktop/src/backend/DesktopBackendManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,33 @@ describe("DesktopBackendManager", () => {
}),
);

it.effect("reports a backend killed by a signal as a signal exit", () =>
Effect.gen(function* () {
// The shape Effect's Node spawner fails `exitCode` with when the child
// exits with a null code and a signal.
const exitCause = PlatformError.systemError({
_tag: "Unknown",
module: "ChildProcess",
method: "exitCode",
cause: new Error("Process interrupted due to receipt of signal: 'SIGKILL'"),
});
const spawnerLayer = Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make(() =>
Effect.succeed(makeProcess({ exitCode: Effect.fail(exitCause) })),
),
);

const exit = yield* DesktopBackendManager.runBackendProcess({
...baseConfig,
desktopTelemetryStream: Stream.empty,
}).pipe(Effect.scoped, Effect.provide(Layer.merge(spawnerLayer, healthyHttpClientLayer)));

assert.isTrue(Option.isNone(exit.code));
assert.equal(exit.reason, "killed by SIGKILL");
}),
);

it.effect("reports output stream failures with process and stream context", () =>
Effect.gen(function* () {
const outputCause = PlatformError.systemError({
Expand Down
49 changes: 33 additions & 16 deletions apps/desktop/src/backend/DesktopBackendManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ export class BackendProcessExitStatusError extends Schema.TaggedError<BackendPro
}
}

// Node reports no exit code for a child killed by a signal, so Effect's
// spawner fails `exitCode` and leaves the signal name only in the cause's
// message. Returns that signal, or undefined for a genuine read failure.
const SIGNAL_EXIT_PATTERN = /receipt of signal: '(SIG[A-Z0-9]+)'/;
function signalFromExitCodeFailure(error: PlatformError.PlatformError): string | undefined {
return error.cause instanceof Error
? SIGNAL_EXIT_PATTERN.exec(error.cause.message)?.[1]
: undefined;
}

export const BackendProcessError = Schema.Union([
BackendProcessBootstrapEncodeError,
BackendProcessSpawnError,
Expand Down Expand Up @@ -596,17 +606,28 @@ export const runBackendProcess = Effect.fn("runBackendProcess")(function* (
yield* probeReadiness().pipe(Effect.repeat({ while: (ready) => !ready }), Effect.forkScoped);

const exit = yield* handle.exitCode.pipe(
Effect.mapError(
(cause) =>
new BackendProcessExitStatusError({
executablePath: options.executablePath,
entryPath: options.entryPath,
cwd: options.cwd,
httpBaseUrl: options.httpBaseUrl,
pid: Number(handle.pid),
cause,
}),
),
Effect.map((exitCode): BackendProcessExit => ({
code: Option.some(exitCode),
reason: `code=${exitCode}`,
})),
Effect.catch((cause) => {
const signal = signalFromExitCodeFailure(cause);
return signal === undefined
? Effect.fail(
new BackendProcessExitStatusError({
executablePath: options.executablePath,
entryPath: options.entryPath,
cwd: options.cwd,
httpBaseUrl: options.httpBaseUrl,
pid: Number(handle.pid),
cause,
}),
)
: Effect.succeed<BackendProcessExit>({
code: Option.none(),
reason: `killed by ${signal}`,
});
}),
Effect.exit,
);
yield* options.onExitObserved?.() ?? Effect.void;
Expand All @@ -620,11 +641,7 @@ export const runBackendProcess = Effect.fn("runBackendProcess")(function* (
if (Exit.isFailure(exit)) {
return yield* Effect.failCause(exit.cause);
}
const exitCode = exit.value;
return {
code: Option.some(exitCode),
reason: `code=${exitCode}`,
} satisfies BackendProcessExit;
return exit.value;
});

// Factory for one pooled backend instance. The returned instance owns
Expand Down
Loading