Compare commits

...
Author SHA1 Message Date
Mohamed Boudra 184328e6fb test(server): skip the POSIX-only plugin EPIPE test on Windows 2026-11-23 09:30:15 +01:00
Mohamed Boudra 07235fe2ea fix(server): keep the daemon alive when a plugin session closes after its process dies
PluginSessionSocket.close() sent paseo_close to the plugin child without a
callback. Node does not throw when that send fails: it emits "error" on the
ChildProcess, and the runtime has no error listener, so the failure became an
uncaught exception. The try/catch around the send never ran.

When a plugin process dies, a frame write to its session fails with EPIPE, the
websocket server's outbound transport closes the socket, and the close's own
send then crashed the daemon with "write EPIPE" (or ERR_IPC_CHANNEL_CLOSED once
Node has seen the disconnect). Passing a callback makes the failed send a
handled result.
2026-11-23 09:24:29 +01:00
3 changed files with 97 additions and 11 deletions
+5
View File
@@ -340,6 +340,11 @@ jobs:
src/server/plugins/index.posix.test.ts \
-t "reaps compiler children" --bail=1
- name: Verify plugin session sockets survive a dead plugin child
run: |
npm run test:unit --workspace=@getpaseo/server -- \
src/server/plugins/session-socket.posix.test.ts --bail=1
desktop-tests-ubuntu:
name: desktop-tests (ubuntu-latest)
needs: changes
@@ -1,16 +1,46 @@
import { fork } from "node:child_process";
import { type ChildProcess, execFileSync, fork } from "node:child_process";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import type { PluginProcessMessage } from "./plugin-process-protocol.js";
import { PluginSessionSocket } from "./session-socket.js";
function forkEchoChild(): ChildProcess {
return fork(
fileURLToPath(new URL("./test-fixtures/plugin-session-echo.cjs", import.meta.url)),
[],
{ serialization: "advanced", stdio: ["ignore", "ignore", "ignore", "ipc"] },
);
}
async function waitForSpawn(child: ChildProcess): Promise<number> {
await new Promise<void>((resolve) => child.once("spawn", () => resolve()));
if (child.pid === undefined) throw new Error("Child process has no pid");
return child.pid;
}
/**
* Blocks the event loop until the child is dead, so the parent has not yet read the
* IPC hang-up: `child.connected` is still true and the next write fails with EPIPE.
* A busy daemon sits in the same window between a plugin dying and Node noticing.
*/
function killAndWaitUnnoticed(child: ChildProcess, pid: number): void {
child.kill("SIGKILL");
execFileSync("sh", [
"-c",
'while kill -0 "$1" 2>/dev/null && [ "$(ps -o stat= -p "$1" | cut -c1)" != Z ]; do sleep 0.01; done',
"sh",
String(pid),
]);
}
async function settle(): Promise<void> {
await new Promise((resolve) => setImmediate(resolve));
await new Promise((resolve) => setImmediate(resolve));
}
describe("PluginSessionSocket child-process transport", () => {
it("round-trips ordered text and binary frames through a real child process", async () => {
const child = fork(
fileURLToPath(new URL("./test-fixtures/plugin-session-echo.cjs", import.meta.url)),
[],
{ serialization: "advanced", stdio: ["ignore", "ignore", "ignore", "ipc"] },
);
const child = forkEchoChild();
const socket = new PluginSessionSocket(child);
const received: Array<string | Uint8Array> = [];
socket.on("message", (data) => received.push(data as string | Uint8Array));
@@ -29,4 +59,57 @@ describe("PluginSessionSocket child-process transport", () => {
child.kill();
}
});
// Without an "error" listener, an error event on the child is an uncaught exception
// that crashes the daemon. The listeners below only record what the daemon would throw.
// Waiting for the child without running the event loop needs `sh`, `kill` and `ps`.
it.skipIf(process.platform === "win32")(
"closes after a frame write to a dead child fails with EPIPE",
async () => {
const child = forkEchoChild();
const pid = await waitForSpawn(child);
const childErrors: unknown[] = [];
child.on("error", (error) => childErrors.push(error));
const socket = new PluginSessionSocket(child);
let closed = false;
socket.on("close", () => {
closed = true;
});
let sendError: Error | undefined;
killAndWaitUnnoticed(child, pid);
// The websocket server's outbound transport closes the socket when a write fails.
socket.send("frame", (error) => {
sendError = error;
socket.close();
});
await settle();
expect(sendError).toMatchObject({ code: "EPIPE" });
expect(closed).toBe(true);
expect(childErrors).toEqual([]);
},
);
it("closes after the child's IPC channel has disconnected", async () => {
const child = forkEchoChild();
await waitForSpawn(child);
const childErrors: unknown[] = [];
child.on("error", (error) => childErrors.push(error));
const socket = new PluginSessionSocket(child);
let closed = false;
socket.on("close", () => {
closed = true;
});
const disconnected = new Promise<void>((resolve) => child.once("disconnect", () => resolve()));
child.kill("SIGKILL");
await disconnected;
socket.close();
await settle();
expect(child.connected).toBe(false);
expect(closed).toBe(true);
expect(childErrors).toEqual([]);
});
});
@@ -57,11 +57,9 @@ export class PluginSessionSocket {
close(code?: number, reason?: string): void {
if (this.readyState === 3) return;
this.readyState = 3;
try {
this.child.send({ type: "paseo_close" });
} catch {
// The child has already gone; local session cleanup still has to run.
}
// Node reports a failed send to the callback, or else as an "error" event on the child,
// which nothing handles. The child may already be gone; local cleanup still has to run.
this.child.send({ type: "paseo_close" }, () => undefined);
this.emit("close", code, reason);
}