Skip to content

Commit 9786b82

Browse files
fix(provider-acp): wait_for_exit answers when a background child holds the output pipes (pingdotgg#34825)
1 parent 7fc9aa6 commit 9786b82

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎packages/provider-acp/src/server/clientTerminals.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,46 @@ describe("AcpClientTerminals", () => {
200200
),
201201
);
202202

203+
it.effect("answers wait_for_exit when a background child keeps the output pipes open", () =>
204+
Effect.gen(function* () {
205+
// Models `sh -c "setsid server & echo done"`: sh exits, the orphan inherits stdout/stderr.
206+
const orphanExited = yield* Deferred.make<void>();
207+
const heldOpen = Stream.fromEffectDrain(Deferred.await(orphanExited));
208+
const child = ChildProcessSpawner.makeHandle({
209+
pid: ChildProcessSpawner.ProcessId(1),
210+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
211+
isRunning: Effect.succeed(false),
212+
kill: () => Effect.void,
213+
stdin: Sink.drain,
214+
stdout: Stream.make(new TextEncoder().encode("done\n")).pipe(Stream.concat(heldOpen)),
215+
stderr: heldOpen,
216+
all: Stream.empty,
217+
getInputFd: () => Sink.drain,
218+
getOutputFd: () => Stream.empty,
219+
unref: Effect.succeed(Effect.void),
220+
});
221+
const terminals = yield* makeAcpClientTerminals({ defaultCwd: process.cwd() }).pipe(
222+
Effect.provideService(
223+
ChildProcessSpawner.ChildProcessSpawner,
224+
ChildProcessSpawner.make(() => Effect.succeed(child)),
225+
),
226+
);
227+
yield* Effect.addFinalizer(() =>
228+
Deferred.succeed(orphanExited, undefined).pipe(Effect.andThen(terminals.disposeAll)),
229+
);
230+
const terminal = yield* terminals.create({ sessionId: "session", command: "fixture" });
231+
const request = { sessionId: "session", ...terminal };
232+
const waiting = yield* terminals.waitForExit(request).pipe(Effect.forkChild);
233+
234+
yield* TestClock.adjust("5 seconds");
235+
expect(waiting.pollUnsafe()).toBeDefined();
236+
expect(yield* Fiber.join(waiting)).toEqual({ exitCode: 0, signal: null });
237+
const output = yield* terminals.output(request);
238+
expect(output.output).toBe("done\n");
239+
expect(output.exitStatus).toEqual({ exitCode: 0, signal: null });
240+
}).pipe(Effect.scoped),
241+
);
242+
203243
it.effect.each([
204244
{ deadline: "default", forceKillAfter: undefined, deadlineMillis: 5000 },
205245
{ deadline: "configured", forceKillAfter: "250 millis" as const, deadlineMillis: 250 },

‎packages/provider-acp/src/server/clientTerminals.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const MAX_RETAINED_TERMINALS = 32;
1919
const MAX_RETAINED_OUTPUT_BYTES = 16 * 1024 * 1024;
2020
const DEFAULT_OUTPUT_BYTE_LIMIT = 4 * 1024 * 1024;
2121
const MAX_OUTPUT_BYTE_LIMIT = 8 * 1024 * 1024;
22+
const OUTPUT_DRAIN_GRACE = "1 second";
2223

2324
const terminalRequestError = (message: string, cause?: unknown) =>
2425
EffectAcpErrors.AcpRequestError.internalError(
@@ -301,6 +302,8 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi
301302
Effect.all([Fiber.join(stdoutPump), Fiber.join(stderrPump)], {
302303
discard: true,
303304
}).pipe(
305+
// A background child can inherit the pipes and hold them open indefinitely.
306+
Effect.timeoutOption(OUTPUT_DRAIN_GRACE),
304307
Effect.andThen(
305308
Effect.sync(() => {
306309
record.exitStatus = status;

0 commit comments

Comments
 (0)