Skip to content

Commit 5c72faa

Browse files
yaacovcorcosclaude
andauthored
fix(pi): send SIGTERM before closing Pi's stdin on teardown (pingdotgg#30025)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent ebfca09 commit 5c72faa

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎packages/provider-pi/src/server/adapter.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4738,3 +4738,35 @@ describe("PiRpc early process exit", () => {
47384738
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
47394739
);
47404740
});
4741+
4742+
describe("PiRpc teardown", () => {
4743+
// Like pi, quits once stdin ends; it survives SIGTERM, so teardown's SIGKILL ends it otherwise.
4744+
const recordShutdownCues = `
4745+
const fs = require("node:fs");
4746+
const note = (cue) => fs.appendFileSync(process.argv[1], cue + "\\n");
4747+
process.on("SIGTERM", () => note("SIGTERM"));
4748+
process.stdin.on("end", () => note("stdin end")).resume();
4749+
process.stdout.write('{"type":"ready"}\\n');
4750+
`;
4751+
4752+
it.live.skipIf(HostProcess.Platform.defaultValue() === "win32")(
4753+
"signals pi before its stdin reaches EOF",
4754+
() =>
4755+
Effect.gen(function* () {
4756+
const fileSystem = yield* FileSystem.FileSystem;
4757+
const cuesPath = `${yield* fileSystem.makeTempDirectoryScoped()}/cues`;
4758+
yield* Effect.gen(function* () {
4759+
const connection = yield* makePiRpcConnection({
4760+
command: process.execPath,
4761+
args: ["-e", recordShutdownCues, cuesPath],
4762+
cwd: undefined,
4763+
env: {},
4764+
});
4765+
assert.equal((yield* Queue.take(connection.events))["type"], "ready");
4766+
}).pipe(Effect.scoped);
4767+
4768+
const cues = (yield* fileSystem.readFileString(cuesPath)).split("\n").filter(Boolean);
4769+
assert.deepEqual(cues, ["SIGTERM"]);
4770+
}).pipe(Effect.provide(NodeServices.layer)),
4771+
);
4772+
});

‎packages/provider-pi/src/server/rpc.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
199199
extendEnv: false,
200200
shell: spawnCommand.shell,
201201
detached: platform !== "win32",
202+
// On stdin EOF Pi quits without its SIGTERM handler, so the later SIGTERM kills it mid-cleanup.
203+
stdin: { stream: "pipe", endOnDone: false },
202204
}),
203205
)
204206
.pipe(Effect.mapError((cause) => new PiRpcError({ operation: "spawn", cause })));

0 commit comments

Comments
 (0)