Skip to content

Commit dc7f172

Browse files
arubi9claude
andcommitted
perf(server): bound how many git processes the VCS layer spawns at once
A workspace refresh asks for refs and status across every project and worktree at once, and each request spawns several git processes. With no bound, one observed refresh issued 1055 git spawns; individual commands that take about a second in isolation took 6.5s under that load, listRefs calls ran 51s, and the local environment endpoint took 14s -- past the client's 10s connection-setup timeout, so the client reconnected and triggered the storm again. Gate the spawn behind a semaphore sized from availableParallelism (4 to 16 permits). The permit is taken around the spawn only, so time spent queued never counts against the command's own timeout. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 43d8c11 commit dc7f172

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎apps/server/src/vcs/VcsProcess.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { describe, expect, it } from "@effect/vitest";
3+
import * as Deferred from "effect/Deferred";
34
import * as Duration from "effect/Duration";
45
import * as Effect from "effect/Effect";
56
import * as Fiber from "effect/Fiber";
@@ -45,6 +46,49 @@ const captureProcessResult = (
4546
);
4647

4748
describe("VcsProcess.run", () => {
49+
it.effect("bounds how many processes it spawns at once", () =>
50+
Effect.gen(function* () {
51+
const release = yield* Deferred.make<void>();
52+
let inFlight = 0;
53+
let peakInFlight = 0;
54+
55+
const service = yield* VcsProcess.make.pipe(
56+
Effect.provideService(
57+
ProcessRunner.ProcessRunner,
58+
ProcessRunner.ProcessRunner.of({
59+
run: () =>
60+
Effect.gen(function* () {
61+
inFlight += 1;
62+
peakInFlight = Math.max(peakInFlight, inFlight);
63+
yield* Deferred.await(release);
64+
inFlight -= 1;
65+
return {
66+
code: 0,
67+
stdout: "",
68+
stderr: "",
69+
stdoutTruncated: false,
70+
stderrTruncated: false,
71+
} as ProcessRunner.ProcessRunOutput;
72+
}),
73+
}),
74+
),
75+
);
76+
77+
const fibers = yield* Effect.forEach(
78+
Array.from({ length: 40 }, (_, index) => index),
79+
() => Effect.forkChild(service.run(baseInput), { startImmediately: true }),
80+
);
81+
// Every fiber started eagerly and ran until it blocked, either on a
82+
// permit or on the release latch.
83+
expect(peakInFlight).toBeLessThanOrEqual(16);
84+
expect(peakInFlight).toBeLessThan(40);
85+
86+
yield* Deferred.succeed(release, undefined);
87+
yield* Fiber.awaitAll(fibers);
88+
expect(peakInFlight).toBeLessThanOrEqual(16);
89+
}),
90+
);
91+
4892
it.effect("collects stdout", () =>
4993
Effect.gen(function* () {
5094
const result = yield* run({

‎apps/server/src/vcs/VcsProcess.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import * as NodeOS from "node:os";
2+
13
import * as Context from "effect/Context";
24
import * as Effect from "effect/Effect";
35
import * as Layer from "effect/Layer";
46
import * as Match from "effect/Match";
7+
import * as Semaphore from "effect/Semaphore";
58
import { ChildProcessSpawner } from "effect/unstable/process";
69

710
import {
@@ -86,8 +89,24 @@ const classifyNonZeroExit = (command: string, stderr: string): VcsProcessExitFai
8689
return "command-failed";
8790
};
8891

92+
// A single workspace refresh can ask for refs across every worktree at once, and
93+
// each request spawns several git processes. Unbounded, that starves the event
94+
// loop badly enough that unrelated work (provider health checks, HTTP requests)
95+
// blows past its own timeouts. Bound the spawns instead; queued commands wait
96+
// for a permit rather than competing.
97+
// ponytail: one global limit, make it per-repository if a busy repo ever starves
98+
// the others.
99+
// The bound scales with the host: a single git command costs ~1s on a large
100+
// repository, so too low a limit turns a refresh into a queue, while too high a
101+
// limit is what caused the starvation in the first place.
102+
const MAX_CONCURRENT_VCS_PROCESSES = Math.max(
103+
4,
104+
Math.min(16, Math.floor(NodeOS.availableParallelism() / 4)),
105+
);
106+
89107
export const make = Effect.gen(function* () {
90108
const processRunner = yield* ProcessRunner.ProcessRunner;
109+
const spawnPermits = yield* Semaphore.make(MAX_CONCURRENT_VCS_PROCESSES);
91110

92111
const run = Effect.fn("VcsProcess.run")(function* (input: VcsProcessInput) {
93112
const baseError = {
@@ -112,6 +131,9 @@ export const make = Effect.gen(function* () {
112131
timeoutBehavior: "error",
113132
})
114133
.pipe(
134+
// Take the permit around the spawn only, so waiting in the queue never
135+
// counts against the command's own timeout.
136+
spawnPermits.withPermits(1),
115137
Effect.mapError(
116138
Match.valueTags({
117139
ProcessSpawnError: (error) =>

0 commit comments

Comments
 (0)