Skip to content

Commit ed1b895

Browse files
yaacovcorcosclaude
andcommitted
fix(pi): send SIGTERM before closing Pi's stdin on teardown
Since the Effect 4.1.2 upgrade, interrupting the stdin writer also ends the child's stdin. Pi treats stdin EOF as a quit that removes its SIGTERM handler, so the SIGTERM sent next killed Pi mid-shutdown: its session_shutdown extension handlers never finished and the shell of a running bash command was left behind. Keep stdin open on teardown so Pi sees SIGTERM first again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cefb544 commit ed1b895

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
@@ -4728,3 +4728,35 @@ describe("PiRpc early process exit", () => {
47284728
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
47294729
);
47304730
});
4731+
4732+
describe("PiRpc teardown", () => {
4733+
// Like pi, quits once stdin ends; it survives SIGTERM, so teardown's SIGKILL ends it otherwise.
4734+
const recordShutdownCues = `
4735+
const fs = require("node:fs");
4736+
const note = (cue) => fs.appendFileSync(process.argv[1], cue + "\\n");
4737+
process.on("SIGTERM", () => note("SIGTERM"));
4738+
process.stdin.on("end", () => note("stdin end")).resume();
4739+
process.stdout.write('{"type":"ready"}\\n');
4740+
`;
4741+
4742+
it.live.skipIf(HostProcess.Platform.defaultValue() === "win32")(
4743+
"signals pi before its stdin reaches EOF",
4744+
() =>
4745+
Effect.gen(function* () {
4746+
const fileSystem = yield* FileSystem.FileSystem;
4747+
const cuesPath = `${yield* fileSystem.makeTempDirectoryScoped()}/cues`;
4748+
yield* Effect.gen(function* () {
4749+
const connection = yield* makePiRpcConnection({
4750+
command: process.execPath,
4751+
args: ["-e", recordShutdownCues, cuesPath],
4752+
cwd: undefined,
4753+
env: {},
4754+
});
4755+
assert.equal((yield* Queue.take(connection.events))["type"], "ready");
4756+
}).pipe(Effect.scoped);
4757+
4758+
const cues = (yield* fileSystem.readFileString(cuesPath)).split("\n").filter(Boolean);
4759+
assert.deepEqual(cues, ["SIGTERM"]);
4760+
}).pipe(Effect.provide(NodeServices.layer)),
4761+
);
4762+
});

‎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)