Skip to content

Commit 2861360

Browse files
author
Dara Adedeji
committed
fix(server): share one process snapshot across terminal subprocess polls
The terminal subprocess poller ran pgrep and a full ps table dump per terminal every second, each spawned by bare name so every call walked the full PATH with one failed posix_spawn per directory. With many terminals this sustains hundreds of process creations per second and can wrap the macOS PID space (#6332). Now one ps -eo pid=,ppid=,comm= snapshot (resolved to an absolute path at startup) is taken per poll tick and every terminal derives its child state from that table, matching the shape the Windows path already used.
1 parent 9666b87 commit 2861360

2 files changed

Lines changed: 225 additions & 219 deletions

File tree

‎apps/server/src/terminal/Manager.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as Ref from "effect/Ref";
2424
import * as Schedule from "effect/Schedule";
2525
import * as Scope from "effect/Scope";
2626
import * as TestClock from "effect/testing/TestClock";
27+
import { ChildProcessSpawner } from "effect/unstable/process";
2728
import { expect } from "vite-plus/test";
2829

2930
import * as ProcessRunner from "../processRunner.ts";
@@ -953,6 +954,69 @@ it.layer(
953954
}),
954955
);
955956

957+
it.effect("derives subprocess activity for every terminal from one shared process snapshot", () =>
958+
Effect.gen(function* () {
959+
const runCalls: Array<{ command: string; args: ReadonlyArray<string> }> = [];
960+
// FakePtyAdapter assigns pids starting at 9000, so the two terminals
961+
// opened below run as pids 9000 and 9001.
962+
const psStdout = [" 100 9000 vim", " 101 100 git", " 200 9001 /usr/bin/python3"].join(
963+
"\n",
964+
);
965+
const processRunner: ProcessRunner.ProcessRunner["Service"] = {
966+
run: (input) =>
967+
Effect.sync(() => {
968+
runCalls.push({ command: input.command, args: input.args });
969+
return {
970+
stdout: psStdout,
971+
stderr: "",
972+
code: ChildProcessSpawner.ExitCode(0),
973+
timedOut: false,
974+
stdoutTruncated: false,
975+
stderrTruncated: false,
976+
};
977+
}),
978+
};
979+
980+
const { manager, getEvents } = yield* createManager(5, {
981+
subprocessPollIntervalMs: 20,
982+
}).pipe(
983+
Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
984+
Effect.provide(withHostPlatform("linux")),
985+
);
986+
987+
yield* manager.open(openInput());
988+
yield* manager.open(openInput({ threadId: "thread-2" }));
989+
990+
yield* waitFor(
991+
Effect.map(
992+
getEvents,
993+
(events) =>
994+
events.some(
995+
(event) =>
996+
event.type === "activity" &&
997+
event.hasRunningSubprocess === true &&
998+
event.label === "vim",
999+
) &&
1000+
events.some(
1001+
(event) =>
1002+
event.type === "activity" &&
1003+
event.hasRunningSubprocess === true &&
1004+
event.label === "python3",
1005+
),
1006+
),
1007+
"1200 millis",
1008+
);
1009+
yield* waitFor(
1010+
Effect.sync(() => runCalls.length >= 3),
1011+
"1200 millis",
1012+
);
1013+
1014+
// Every spawn is the shared table snapshot — no per-terminal `pgrep`
1015+
// or per-child `ps -p` invocations.
1016+
expect(runCalls.every((call) => call.args.join(" ") === "-eo pid=,ppid=,comm=")).toBe(true);
1017+
}),
1018+
);
1019+
9561020
it.effect("caps persisted history to configured line limit", () =>
9571021
Effect.gen(function* () {
9581022
const { manager, ptyAdapter } = yield* createManager(3);

0 commit comments

Comments
 (0)