Skip to content
TypeScript

fix(pi): send SIGTERM before closing Pi's stdin on teardown (#30025)

yaacovcorcos5c72faa7731a
diff --git a/packages/provider-pi/src/server/adapter.test.ts b/packages/provider-pi/src/server/adapter.test.ts
index e52505ce249..58d38fd03ad 100644
--- a/packages/provider-pi/src/server/adapter.test.ts
+++ b/packages/provider-pi/src/server/adapter.test.ts
@@ -4738,3 +4738,35 @@ describe("PiRpc early process exit", () => {
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
});
+
+describe("PiRpc teardown", () => {
+ // Like pi, quits once stdin ends; it survives SIGTERM, so teardown's SIGKILL ends it otherwise.
+ const recordShutdownCues = `
+const fs = require("node:fs");
+const note = (cue) => fs.appendFileSync(process.argv[1], cue + "\\n");
+process.on("SIGTERM", () => note("SIGTERM"));
+process.stdin.on("end", () => note("stdin end")).resume();
+process.stdout.write('{"type":"ready"}\\n');
+`;
+
+ it.live.skipIf(HostProcess.Platform.defaultValue() === "win32")(
+ "signals pi before its stdin reaches EOF",
+ () =>
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const cuesPath = `${yield* fileSystem.makeTempDirectoryScoped()}/cues`;
+ yield* Effect.gen(function* () {
+ const connection = yield* makePiRpcConnection({
+ command: process.execPath,
+ args: ["-e", recordShutdownCues, cuesPath],
+ cwd: undefined,
+ env: {},
+ });
+ assert.equal((yield* Queue.take(connection.events))["type"], "ready");
+ }).pipe(Effect.scoped);
+
+ const cues = (yield* fileSystem.readFileString(cuesPath)).split("\n").filter(Boolean);
+ assert.deepEqual(cues, ["SIGTERM"]);
+ }).pipe(Effect.provide(NodeServices.layer)),
+ );
+});
diff --git a/packages/provider-pi/src/server/rpc.ts b/packages/provider-pi/src/server/rpc.ts
index 0af3fade61c..98eacd79013 100644
--- a/packages/provider-pi/src/server/rpc.ts
+++ b/packages/provider-pi/src/server/rpc.ts
@@ -199,6 +199,8 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
extendEnv: false,
shell: spawnCommand.shell,
detached: platform !== "win32",
+ // On stdin EOF Pi quits without its SIGTERM handler, so the later SIGTERM kills it mid-cleanup.
+ stdin: { stream: "pipe", endOnDone: false },
}),
)
.pipe(Effect.mapError((cause) => new PiRpcError({ operation: "spawn", cause })));