The best way to control Claude Code, Codex and more from any computer or phone
perf(opencode): stopping an OpenCode server no longer always waits a full second (#33621)
Ishaan Kothari committed
Nov 11, 2026 at 15:10 UTC
2b6eb63a4dd15b0974646bf9fb52ffdd725a5f6d
2 files changed
+65
-13
packages/provider-opencode/src/server/OpenCodeRuntime.ts
+6
-13
@@ -84,6 +84,7 @@ const DEFAULT_OPENCODE_SERVER_TIMEOUT_MS = 30_000;
84
const DEFAULT_HOSTNAME = "127.0.0.1";
85
const OPENCODE_SERVER_STARTUP_MAX_OUTPUT_CHARS = 64 * 1024;
86
const OPENCODE_SERVER_EXIT_DRAIN_TIMEOUT = "500 millis";
87
+const OPENCODE_SERVER_TERMINATE_GRACE = "1 second";
88
export interface OpenCodeServerProcess {
89
readonly url: string;
90
readonly serverPassword?: string;
@@ -593,19 +594,11 @@ const makeOpenCodeRuntime = Effect.gen(function* () {
594
),
595
);
596
596
- const killOpenCodeProcessGroup = (signal: NodeJS.Signals) =>
597
- hostPlatform === "win32"
598
- ? child.kill({ killSignal: signal, forceKillAfter: "1 second" }).pipe(Effect.asVoid)
599
- : Effect.sync(() => {
600
- try {
601
- signalProcessGroup(Number(child.pid), signal);
602
- } catch {}
603
- });
604
- const terminateChild = killOpenCodeProcessGroup("SIGTERM").pipe(
605
- Effect.andThen(Effect.sleep("1 second")),
606
- Effect.andThen(killOpenCodeProcessGroup("SIGKILL")),
607
- Effect.ignore,
608
- );
597
+ // Signals the server's own group and force-kills only what outlives the grace,
598
+ // so a group that exits promptly does not hold up the scope that closes it.
599
+ const terminateChild = child
600
+ .kill({ killSignal: "SIGTERM", forceKillAfter: OPENCODE_SERVER_TERMINATE_GRACE })
601
+ .pipe(Effect.ignore);
602
// Registered before recording, so an interrupt while the ledger writes
603
// still stops the group.
604
yield* Scope.addFinalizer(runtimeScope, terminateChild);
packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts
+59
@@ -288,4 +288,63 @@ describe.skipIf(observedPlatforms.length === 0)("OpenCode server startup", () =>
288
expect(groupExists(pgid)).toBe(false);
289
}).pipe(Effect.provide(NodeServices.layer)),
290
);
291
+
292
+ // Runs on the test clock, which never advances here: a teardown that sleeps
293
+ // out its grace instead of watching the group would never finish.
294
+ it.effect("stops a running server as soon as its group exits", () =>
295
+ Effect.gen(function* () {
296
+ const fs = yield* FileSystem.FileSystem;
297
+ const path = yield* Path.Path;
298
+ const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-stop-" });
299
+ const binaryPath = path.join(tempDir, "opencode");
300
+ // The background child shares the leader's group. The leader reaps it before exiting, so no
301
+ // zombie keeps the group alive under an init that does not reap. The trap is set after the
302
+ // fork: a child forked with the trap already set can swallow the signal before it execs.
303
+ yield* fs.writeFileString(
304
+ binaryPath,
305
+ [
306
+ "#!/bin/sh",
307
+ "sleep 600 &",
308
+ "trap 'wait; exit 0' TERM",
309
+ 'echo "opencode server listening on http://127.0.0.1:4096"',
310
+ "wait",
311
+ "",
312
+ ].join("\n"),
313
+ );
314
+ yield* fs.chmod(binaryPath, 0o755);
315
+
316
+ const tracked = yield* Deferred.make<number>();
317
+ const layerRuntime = OpenCodeRuntime.layer.pipe(
318
+ Layer.provide(
319
+ Layer.succeed(
320
+ OpenCodeServerLedger.OpenCodeServerLedger,
321
+ OpenCodeServerLedger.OpenCodeServerLedger.of({
322
+ track: (server) => Deferred.succeed(tracked, server.pid).pipe(Effect.as(Effect.void)),
323
+ }),
324
+ ),
325
+ ),
326
+ );
327
+
328
+ const serverScope = yield* Scope.make();
329
+ const server = yield* OpenCodeRuntime.OpenCodeRuntime.pipe(
330
+ Effect.flatMap((runtime) =>
331
+ runtime.startOpenCodeServerProcess({
332
+ binaryPath,
333
+ directory: tempDir,
334
+ port: 4096,
335
+ verify: () => Effect.succeed("1.0.0"),
336
+ }),
337
+ ),
338
+ Effect.provideService(Scope.Scope, serverScope),
339
+ Effect.provide(layerRuntime),
340
+ );
341
+ const pgid = yield* Deferred.await(tracked);
342
+ yield* Effect.addFinalizer(() => killGroup(pgid));
343
+ expect(yield* server.isRunning).toBe(true);
344
+
345
+ yield* Scope.close(serverScope, Exit.void);
346
+
347
+ expect(groupExists(pgid)).toBe(false);
348
+ }).pipe(Effect.provide(NodeServices.layer)),
349
+ );
350
});