@devries-cameron20 / t3code Mirror · read-only / commits / d0957bc4b0
Fork 0

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, 17:15 UTC

fix(opencode): restarted server stops an orphaned OpenCode server started through a wrapper (#34959)

Julius Marminge committed Nov 13, 2026 at 08:47 UTC d0957bc4b0495789e28769bad62834f5f912d94e
2 files changed +53 -15
packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts
+41 -10
@@ -56,7 +56,6 @@ const spawnGroupLeader = (args: ReadonlyArray<string>, script: string) =>
56 if (pid === undefined) return yield* Effect.die("spawn failed");
57 const grouped = yield* Deferred.make<true | string>();
58 const exited = yield* Deferred.make<NodeJS.Signals | null>();
59 - const printed = yield* Deferred.make<void>();
59 child.once("exit", (_code, signal) => Deferred.doneUnsafe(exited, Effect.succeed(signal)));
60 // "close" fires after stdout drains, so a guard line always lands first. It only fires before
61 // "grouped" when the shell died without starting the script, such as from a signal.
@@ -77,12 +76,9 @@ const spawnGroupLeader = (args: ReadonlyArray<string>, script: string) =>
76 Effect.succeed(`\`ps -o pgid= -p ${pid}\` printed ${JSON.stringify(output)}`),
77 );
78 }
80 - if (line === "ready") Deferred.doneUnsafe(printed, Effect.void);
79 });
80 const result = yield* Deferred.await(grouped);
83 - return result === true
84 - ? { pid, exited: Deferred.await(exited), printed: Deferred.await(printed) }
85 - : result;
81 + return result === true ? { pid, exited: Deferred.await(exited) } : result;
82 });
83
84 // A pid colliding with a live group id is rare, so a few retries clear it. Repeated failures
@@ -152,18 +148,21 @@ describe.each(observedPlatforms)("OpenCodeServerLedger observing as %s", (platfo
148 const fs = yield* FileSystem.FileSystem;
149 const path = yield* Path.Path;
150 const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" });
155 - // The inner shell prints once it has exec'd. Until then it still carries the wrapper's
156 - // command line, and recording that would make the reaper skip the group as recycled.
151 + const gate = path.join(stateDir, "gate");
152 + const ready = path.join(stateDir, "ready");
153 + NodeChildProcess.execFileSync("mkfifo", [gate, ready]);
154 + // The wrapper's child is recorded before it execs and stopped after.
155 const wrapper = yield* spawnGroup(
156 SERVE_ARGS,
159 - '/bin/sh -c "echo ready; sleep 600 & wait" "$0" "$@" & exit 0',
157 + `(read _ < '${gate}'; exec /bin/sh -c 'echo > "${ready}"; sleep 600 & wait' "$0" "$@") & exit 0`,
158 );
159 yield* wrapper.exited;
162 - yield* wrapper.printed;
160 expect(groupExists(wrapper.pid)).toBe(true);
161
162 yield* recordFromDeadServer(stateDir, wrapper);
163 expect(yield* fs.readDirectory(path.join(stateDir, "opencode-servers"))).toHaveLength(1);
164 + yield* fs.writeFileString(gate, "\n");
165 + yield* fs.readFileString(ready);
166
167 const restarted = yield* OpenCodeServerLedger.make({ stateDir });
168 yield* restarted.reapOrphans;
@@ -173,6 +172,38 @@ describe.each(observedPlatforms)("OpenCodeServerLedger observing as %s", (platfo
172 }).pipe(provideHost),
173 );
174
175 + it.live("stops a group recorded by an older server without serve arguments", () =>
176 + Effect.gen(function* () {
177 + const fs = yield* FileSystem.FileSystem;
178 + const path = yield* Path.Path;
179 + const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" });
180 + const orphan = yield* spawnGroup(SERVE_ARGS);
181 + yield* recordFromDeadServer(stateDir, orphan);
182 + const entryPath = path.join(stateDir, "opencode-servers", `${orphan.pid}.json`);
183 + const { args: _args, ...legacy } = JSON.parse(yield* fs.readFileString(entryPath));
184 + yield* fs.writeFileString(entryPath, JSON.stringify(legacy));
185 +
186 + const restarted = yield* OpenCodeServerLedger.make({ stateDir });
187 + yield* restarted.reapOrphans;
188 +
189 + expect(groupExists(orphan.pid)).toBe(false);
190 + }).pipe(provideHost),
191 + );
192 +
193 + it.live("stops a group whose wrapper appends its own options", () =>
194 + Effect.gen(function* () {
195 + const fs = yield* FileSystem.FileSystem;
196 + const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" });
197 + const orphan = yield* spawnGroup([...SERVE_ARGS, "--log-level=ERROR"]);
198 + yield* recordFromDeadServer(stateDir, orphan);
199 +
200 + const restarted = yield* OpenCodeServerLedger.make({ stateDir });
201 + yield* restarted.reapOrphans;
202 +
203 + expect(groupExists(orphan.pid)).toBe(false);
204 + }).pipe(provideHost),
205 + );
206 +
207 it.live("leaves a recycled pid alone when its start time does not match", () =>
208 Effect.gen(function* () {
209 const fs = yield* FileSystem.FileSystem;
@@ -201,7 +232,7 @@ describe.each(observedPlatforms)("OpenCodeServerLedger observing as %s", (platfo
232 yield* recordFromDeadServer(stateDir, unrelated);
233 const entryPath = path.join(stateDir, "opencode-servers", `${unrelated.pid}.json`);
234 const entry = yield* fs.readFileString(entryPath);
204 - yield* fs.writeFileString(entryPath, entry.replace("--port=4096", "--port=4097"));
235 + yield* fs.writeFileString(entryPath, entry.replaceAll("--port=4096", "--port=4097"));
236
237 const restarted = yield* OpenCodeServerLedger.make({ stateDir });
238 yield* restarted.reapOrphans;
packages/provider-opencode/src/server/OpenCodeServerLedger.ts
+12 -5
@@ -21,6 +21,7 @@ const OpenCodeServerEntry = Schema.Struct({
21 pid: Schema.Int,
22 startTime: Schema.String,
23 command: Schema.String,
24 + args: Schema.optionalKey(Schema.Array(Schema.String)),
25 port: Schema.Int,
26 stateDir: Schema.String,
27 owner: ProcessIdentity,
@@ -73,6 +74,11 @@ const parseDarwinPs = (output: string): ReadonlyArray<ObservedProcess> =>
74 ];
75 });
76
77 +// A wrapper's forked child keeps the wrapper's command line until it execs; the
78 +// serve arguments survive the exec, so they identify the server either way.
79 +const runsServe = (observed: ObservedProcess, args: ReadonlyArray<string>) =>
80 + observed.command.endsWith(` ${args.join(" ")}`);
81 +
82 const signalGroup = (pgid: number, signal: NodeJS.Signals) => {
83 try {
84 signalProcessGroup(pgid, signal);
@@ -193,9 +199,7 @@ export const make = Effect.fn("OpenCodeServerLedger.make")(function* (input: {
199 leader !== undefined && !leader.zombie && leader.pgid === pgid
200 ? [leader]
201 : (yield* observeGroup(pgid)).filter((candidate) => !candidate.zombie);
196 - const member =
197 - members.find((candidate) => candidate.command.endsWith(` ${server.args.join(" ")}`)) ??
198 - members[0];
202 + const member = members.find((candidate) => runsServe(candidate, server.args)) ?? members[0];
203 if (owner === undefined || member === undefined) {
204 if (groupExists(pgid)) {
205 yield* Effect.logWarning(
@@ -212,6 +216,7 @@ export const make = Effect.fn("OpenCodeServerLedger.make")(function* (input: {
216 pid: member.pid,
217 startTime: member.startTime,
218 command: member.command,
219 + args: server.args,
220 port: server.port,
221 stateDir: input.stateDir,
222 owner,
@@ -239,7 +244,8 @@ export const make = Effect.fn("OpenCodeServerLedger.make")(function* (input: {
244 );
245
246 // Signals only a group whose recorded member still has the same pid, start
242 - // time, group, and full command line, so a recycled pid is never touched.
247 + // time, and group and still has the same command line or serve arguments, so
248 + // a recycled pid is never touched.
249 const stopOrphan = Effect.fn("OpenCodeServerLedger.stopOrphan")(function* (
250 entry: OpenCodeServerEntry,
251 ) {
@@ -249,7 +255,8 @@ export const make = Effect.fn("OpenCodeServerLedger.make")(function* (input: {
255 observed.zombie ||
256 observed.startTime !== entry.startTime ||
257 observed.pgid !== entry.pgid ||
252 - observed.command !== entry.command
258 + (observed.command !== entry.command &&
259 + (entry.args === undefined || !runsServe(observed, entry.args)))
260 ) {
261 return;
262 }