The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 07:15 UTC
fix(pi): teardown returns once the Pi process group exits (#38845)
Julius Marminge committed
Nov 18, 2026 at 18:51 UTC
b6d6717df6229db4eec6366135a82f0cc0684283
2 files changed
+65
-2
packages/provider-pi/src/server/adapter.test.ts
+46
@@ -20,6 +20,7 @@ import {
20
type OrchestrationV2ProviderTurn,
21
} from "@t3tools/contracts";
22
import * as Cause from "effect/Cause";
23
+import * as PlatformError from "effect/PlatformError";
24
import * as DateTime from "effect/DateTime";
25
import * as Deferred from "effect/Deferred";
26
import * as Duration from "effect/Duration";
@@ -7182,4 +7183,49 @@ process.stdout.write('{"type":"ready"}\\n');
7183
assert.deepEqual(cues, ["SIGTERM"]);
7184
}).pipe(Effect.provide(NodeServices.layer)),
7185
);
7186
+
7187
+ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
7188
+ "returns once the pi process group exits instead of waiting out the grace",
7189
+ () =>
7190
+ Effect.gen(function* () {
7191
+ const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
7192
+ const childExit = yield* Deferred.make<
7193
+ ChildProcessSpawner.ExitCode,
7194
+ PlatformError.PlatformError
7195
+ >();
7196
+ const spawner = ChildProcessSpawner.make((command) =>
7197
+ realSpawner
7198
+ .spawn(command)
7199
+ .pipe(
7200
+ Effect.tap((handle) =>
7201
+ handle.exitCode.pipe(Deferred.into(childExit), Effect.forkDetach),
7202
+ ),
7203
+ ),
7204
+ );
7205
+ const scope = yield* Scope.make();
7206
+ const connection = yield* makePiRpcConnection({
7207
+ command: process.execPath,
7208
+ args: [
7209
+ "-e",
7210
+ 'process.on("SIGTERM",()=>process.exit(0));process.stdin.resume();' +
7211
+ 'process.stdout.write(\'{"type":"ready"}\\n\');',
7212
+ ],
7213
+ cwd: undefined,
7214
+ env: {},
7215
+ }).pipe(
7216
+ Scope.provide(scope),
7217
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
7218
+ );
7219
+ assert.equal((yield* Queue.take(connection.events))["type"], "ready");
7220
+
7221
+ const closing = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild);
7222
+ yield* Deferred.await(childExit);
7223
+ yield* TestClock.adjust(Duration.millis(999));
7224
+ const closedBeforeGrace = closing.pollUnsafe() !== undefined;
7225
+
7226
+ yield* TestClock.adjust(Duration.seconds(1));
7227
+ yield* Fiber.join(closing);
7228
+ assert.isTrue(closedBeforeGrace);
7229
+ }).pipe(Effect.provide(NodeServices.layer)),
7230
+ );
7231
});
packages/provider-pi/src/server/rpc.ts
+19
-2
@@ -105,6 +105,7 @@ export interface PiRpcConnection {
105
106
const DEFAULT_REQUEST_TIMEOUT_MS = 30_000;
107
const TERMINATION_GRACE = Duration.seconds(1);
108
+const TERMINATION_POLL_INTERVAL = Duration.millis(20);
109
110
interface PendingPiRequest {
111
readonly deferred: Deferred.Deferred<unknown, PiRpcError>;
@@ -196,10 +197,14 @@ function parsePiRecord(line: string): PiRpcRecord | undefined {
197
const terminatePiProcess = Effect.fn("PiRpc.terminatePiProcess")(function* (
198
kill: (signal: NodeJS.Signals) => boolean,
199
hasExited: () => boolean,
200
+ groupGone: () => boolean,
201
) {
202
if (hasExited()) return;
203
if (!kill("SIGTERM")) return;
202
- yield* Effect.sleep(TERMINATION_GRACE);
204
+ yield* Effect.sleep(TERMINATION_POLL_INTERVAL).pipe(
205
+ Effect.repeat({ until: groupGone }),
206
+ Effect.timeoutOption(TERMINATION_GRACE),
207
+ );
208
if (hasExited()) return;
209
kill("SIGKILL");
210
});
@@ -253,6 +258,16 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
258
}
259
};
260
261
+ // EPERM means a group member is still alive, so only ESRCH ends the SIGTERM grace early.
262
+ const processGroupGone = (): boolean => {
263
+ try {
264
+ signalProcessGroup(Number(child.pid), 0);
265
+ return false;
266
+ } catch (cause) {
267
+ return (cause as NodeJS.ErrnoException | undefined)?.code === "ESRCH";
268
+ }
269
+ };
270
+
271
// Windows process.kill leaves extension children and pipes alive; taskkill /T reaps the tree.
272
const terminateWindowsTree = Effect.gen(function* () {
273
if (hasExited()) return;
@@ -263,7 +278,9 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp
278
}).pipe(Effect.scoped, Effect.ignore);
279
280
const terminateProcess =
266
- platform === "win32" ? terminateWindowsTree : terminatePiProcess(killProcessGroup, hasExited);
281
+ platform === "win32"
282
+ ? terminateWindowsTree
283
+ : terminatePiProcess(killProcessGroup, hasExited, processGroupGone);
284
285
// Register teardown immediately after spawn so interrupted setup cannot leak a detached process.
286
yield* Scope.addFinalizer(scope, terminateProcess.pipe(Effect.ignore, Effect.uninterruptible));