fix(server): stop OpenCode servers left behind by a crashed server #13879
Mergedjuliusmarmingemerged 2 commits into
t3code/codex-turn-mapping fromrefs/pull/13879/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -0,0 +158,21 @@158+ if (platform === "darwin")159+ return observeDarwin(pid).pipe(Effect.orElseSucceed(() => undefined));160+ return Effect.succeed(undefined);161+ };162+163+ const isRunning = (identity: ProcessIdentity) =>164+ observe(identity.pid).pipe(165+ Effect.map(166+ (observed) =>167+ observed !== undefined && !observed.zombie && observed.startTime === identity.startTime,168+ ),169+ );170+171+ const ownerPid = input.ownerPid ?? process.pid;172+ const ownerProcess = yield* observe(ownerPid);173+ const owner =174+ ownerProcess === undefined ? undefined : { pid: ownerPid, startTime: ownerProcess.startTime };175+176+ const track: OpenCodeServerLedgerShape["track"] = (server) =>177+ Effect.gen(function* () {178+ const observed = yield* observe(server.pid);apps/server/src/provider/OpenCodeServerLedger.ts line 179OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +159,21 @@159+ return observeDarwin(pid).pipe(Effect.orElseSucceed(() => undefined));160+ return Effect.succeed(undefined);161+ };162+163+ const isRunning = (identity: ProcessIdentity) =>164+ observe(identity.pid).pipe(165+ Effect.map(166+ (observed) =>167+ observed !== undefined && !observed.zombie && observed.startTime === identity.startTime,168+ ),169+ );170+171+ const ownerPid = input.ownerPid ?? process.pid;172+ const ownerProcess = yield* observe(ownerPid);173+ const owner =174+ ownerProcess === undefined ? undefined : { pid: ownerPid, startTime: ownerProcess.startTime };175+176+ const track: OpenCodeServerLedgerShape["track"] = (server) =>177+ Effect.gen(function* () {178+ const observed = yield* observe(server.pid);179+ if (owner === undefined || observed === undefined) return Effect.void;@@ -0,0 +129,21 @@129+ .string(130+ ChildProcess.make(131+ "/bin/ps",132+ ["-ww", "-o", "pgid=", "-o", "stat=", "-o", "lstart=", "-o", "args=", "-p", String(pid)],133+ {134+ env: { LC_ALL: "C", TZ: "UTC" },135+ extendEnv: true,136+ stdin: "ignore",137+ stderr: "ignore",138+ },139+ ),140+ )141+ .pipe(142+ Effect.timeout("5 seconds"),143+ Effect.map((output): ObservedProcess | undefined => {144+ const match = DARWIN_PS_LINE.exec(output.trim());145+ if (match === null) return undefined;146+ return {147+ pgid: Number(match[1]),148+ startTime: match[3]!,149+ command: match[4]!,
- apps/server/src/provider/OpenCodeServerLedger.ts line 32OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +12,21 @@12+13+/**14+ * Local `opencode serve` processes run in their own process group so T3 can15+ * stop the whole group, which also means they outlive a T3 server that is16+ * SIGKILLed or crashes. Each spawn is recorded under the state directory and17+ * removed on a graceful stop; the next server start stops whatever a dead18+ * server left behind.19+ */20+export interface OpenCodeServerLedgerShape {21+ /** Records a spawned server group and returns the effect that forgets it after a graceful stop. */22+ readonly track: (server: OpenCodeServerSpawn) => Effect.Effect<Effect.Effect<void>>;23+}24+25+interface OpenCodeServerSpawn {26+ readonly pid: number;27+ readonly port: number;28+ /** The argv after the binary, e.g. `["serve", "--hostname=127.0.0.1", "--port=4096"]`. */29+ readonly args: ReadonlyArray<string>;30+}31+32+export class OpenCodeServerLedger extends Context.Reference<OpenCodeServerLedgerShape>(
- @@ -0,0 +77,21 @@77+ // The group may already be gone.78+ }79+};80+81+const groupExists = (pgid: number) => {82+ try {83+ process.kill(-pgid, 0);84+ return true;85+ } catch (cause) {86+ return (cause as NodeJS.ErrnoException | undefined)?.code !== "ESRCH";87+ }88+};89+90+const isOpenCodeServeCommand = (command: string, args: ReadonlyArray<string>) =>91+ args[0] === "serve" && command.endsWith(` ${args.join(" ")}`);92+93+/**94+ * Builds a ledger for one state directory. `ownerPid` is the T3 server that95+ * owns the servers it tracks; it defaults to this process.96+ */97+export const makeOpenCodeServerLedger = Effect.fn("OpenCodeServerLedger.make")(function* (input: {
juliusmarminge merged thisfe6553e
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed