[Bug]: Closing a Pi session kills Pi mid-shutdown and can leave a running shell command behind
Area: apps/server (packages/provider-pi)
Steps to reproduce
-
Install Pi 1.5.0 (
@earendil-works/pi-coding-agent). -
Add any Pi extension whose
session_shutdownhandler awaits something. Pi's extension docs recommend doing cleanup there. Even a singlesetTimeout(0)is enough:export default function (pi) { pi.on("session_shutdown", async () => { await new Promise((resolve) => setTimeout(resolve, 0)); // close sockets, stop watchers, flush state... }); } -
Start a Pi session in T3 Code. While a bash command is running (for example
sleep 120), close or stop the session. -
Run
pgrep -fl "sleep 120".
Expected behavior
T3 sends SIGTERM first. Pi's SIGTERM handler kills the shells it started, runs session_shutdown and exits with code 143. No command keeps running after the session is closed.
Actual behavior
Pi's stdin reaches EOF before T3 sends SIGTERM. Pi treats stdin EOF as a quit: it removes its SIGTERM handler and starts an async shutdown. The SIGTERM that T3 sends next hits the default action and kills Pi on the spot. As a result:
- the extension's
session_shutdownhandler never finishes; - Pi never aborts the running command, so its shell (Pi spawns it in its own process group, which T3's group signal does not reach) is left running under PID 1;
- Pi never reaches
exit.
Cause
57cd05904d upgraded Effect to 4.1.2. In 4.1.2, a stdin sink with the default endOnDone: true also ends the child's stdin when its writer is interrupted or its upstream fails. Before the upgrade it only did so when the stream finished normally (see fromWritableChannel in @effect/platform-node-shared's NodeSink.ts). packages/provider-pi/src/server/rpc.ts pipes its write queue into the default child.stdin. Its SIGTERM → 1 s → SIGKILL finalizer is registered first, so it runs last. Teardown therefore now ends Pi's stdin before it sends SIGTERM. The same happens on connection.terminate: failing the write queue ends stdin too.
Pi's side (1.5.0, dist/modes/rpc/rpc-mode.js): process.stdin.on("end", () => shutdown()). shutdown() first removes the SIGTERM/SIGHUP handlers, then awaits runtimeHost.dispose() (which emits session_shutdown, then aborts the agent and any running bash). Only the SIGTERM handler calls killTrackedDetachedChildren().
Impact
Minor bug or occasional failure. It affects users with a Pi extension that does async cleanup. Without such an extension, Pi's EOF shutdown usually finishes before the SIGTERM arrives, and nothing leaks.
Version or commit
main @ cefb544af3
Environment
macOS (Darwin 25.6), Node 26.12.1, Pi 1.5.0. Not checked on Linux. Windows tears Pi down with taskkill /T /F, so it does not depend on the SIGTERM ordering.
Logs
I ran real Pi 1.5.0 through makePiRpcConnection with the extension above (a 300 ms wait, logging to a file). I started sleep 120; true <marker> with the RPC bash command, then closed the connection's scope or called connection.terminate. Three runs of each on main:
scope shell after close: still running log: session_shutdown start (no "done", no exit)
terminate shell after close: still running log: session_shutdown start (no "done", no exit)
With stdin kept open on teardown, three runs of each:
scope shell after close: none log: session_shutdown start / session_shutdown done / exit 143
terminate shell after close: none log: session_shutdown start / session_shutdown done / exit 143
Main also leaked the shell with handler waits of 0, 5 and 20 ms (3 of 3 runs each, both close paths). With no extension, main cleaned up in 5 of 5 runs.
Workaround
None in T3. Removing async work from session_shutdown avoids it.
[!NOTE] 🤖 GPT 6.1 Sol responding on behalf of StiensWout
This fix landed in #30025 and is present on current upstream main (1a43f4870d). Pi now keeps stdin open during teardown (endOnDone: false), so SIGTERM reaches Pi before EOF; the adapter also has a regression case for that ordering. Could you verify on a build containing #30025 and close this issue if shutdown completes and no shell command remains? This check confirms the landed source and test coverage, rather than a fresh reproduction with your extension.
Thanks. Maria's run on #30025 covered this issue's case with real Pi 1.5.0: on main the extension's session_shutdown logged only its start, and with the fix it logged shutdown_start → shutdown_done → exit 143. Stop and a follow-up turn also worked, and nothing was left running. That is the behaviour this issue asked for, so I'm closing it.
Written with Claude Opus 5.5 in Claude Code, on Yaacov's behalf.