Skip to content

Commit 23ba2b6

Browse files
committed
fix(server): keep long Git commands outside the burst limit
1 parent 192ff9f commit 23ba2b6

2 files changed

Lines changed: 61 additions & 8 deletions

File tree

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

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,29 +172,79 @@ it.effect("bounds Git bursts across drivers without timing out queued commands",
172172
operation: "test.gitBurst",
173173
cwd: "/repo",
174174
args: ["rev-parse", "HEAD"],
175-
timeoutMs: index < 4 ? null : 1_000,
175+
...(index < 4 ? {} : { timeoutMs: index < 8 ? 30_000 : 1_000 }),
176176
}),
177177
{ concurrency: "unbounded" },
178178
).pipe(Effect.forkChild);
179179

180-
yield* Effect.all(Array.from({ length: 4 }, () => Queue.take(starts)));
181180
yield* TestClock.adjust("2 seconds");
182-
assert.equal(yield* Queue.size(starts), 0);
183-
assert.equal(peak, 4);
181+
assert.equal(yield* Queue.size(starts), 8);
182+
assert.equal(peak, 8);
184183
yield* Deferred.succeed(gate, undefined);
185184
const results = yield* Fiber.join(burst);
186185
assert.equal(results.length, 16);
187186
assert.isTrue(results.every((result) => result.stdout === "ok" && result.exitCode === 0));
188-
assert.equal(peak, 4);
187+
assert.equal(peak, 8);
189188
assert.equal(active, 0);
190189
const duration = yield* Metric.value(
191190
Metric.withAttributes(gitCommandDuration, [["operation", "test.gitBurst"]]),
192191
);
193192
assert.equal(duration.count, 16);
194-
assert.equal(duration.sum, 8_000);
193+
assert.equal(duration.sum, 16_000);
195194
}).pipe(Effect.provide(ServerConfigLayer.pipe(Layer.provideMerge(NodeServices.layer)))),
196195
);
197196

197+
it.effect.each([{ timeoutMs: null }, { timeoutMs: 30_001 }])(
198+
"keeps all Git slots available with a pending command whose timeout is $timeoutMs",
199+
({ timeoutMs }) =>
200+
Effect.gen(function* () {
201+
const slowGate = yield* Deferred.make<void>();
202+
const fastGate = yield* Deferred.make<void>();
203+
const starts = yield* Queue.unbounded<void>();
204+
let active = 0;
205+
const spawner = ChildProcessSpawner.make((command) =>
206+
Effect.acquireRelease(
207+
Effect.gen(function* () {
208+
active++;
209+
yield* Queue.offer(starts, undefined);
210+
const gate =
211+
ChildProcess.isStandardCommand(command) && command.args[0] === "push"
212+
? slowGate
213+
: fastGate;
214+
return ChildProcessSpawner.makeHandle({
215+
...makeSuccessfulHandle("ok"),
216+
exitCode: Deferred.await(gate).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
217+
});
218+
}),
219+
() => Effect.sync(() => active--),
220+
),
221+
);
222+
const driver = yield* makeGitVcsDriverCore().pipe(
223+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
224+
);
225+
const slow = yield* driver
226+
.execute({ operation: "test.slowGit", cwd: "/repo", args: ["push"], timeoutMs })
227+
.pipe(Effect.forkChild);
228+
yield* Queue.take(starts);
229+
const burst = yield* Effect.all(
230+
Array.from({ length: 8 }, () =>
231+
driver.execute({ operation: "test.fastGit", cwd: "/repo", args: ["status"] }),
232+
),
233+
{ concurrency: "unbounded" },
234+
).pipe(Effect.forkChild);
235+
236+
yield* TestClock.adjust("0 seconds");
237+
assert.equal(yield* Queue.size(starts), 8);
238+
assert.equal(active, 9);
239+
yield* Deferred.succeed(fastGate, undefined);
240+
assert.equal((yield* Fiber.join(burst)).length, 8);
241+
assert.equal(active, 1);
242+
yield* Deferred.succeed(slowGate, undefined);
243+
assert.equal((yield* Fiber.join(slow)).stdout, "ok");
244+
assert.equal(active, 0);
245+
}).pipe(Effect.provide(ServerConfigLayer.pipe(Layer.provideMerge(NodeServices.layer)))),
246+
);
247+
198248
for (const location of ["root", "nested", "worktree"] as const) {
199249
it.effect(
200250
`skips clean filters while the ${location} index is locked and resumes after unlock`,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
import { ServerConfig } from "../config.ts";
4040

4141
const DEFAULT_TIMEOUT_MS = 30_000;
42-
const gitProcesses = Semaphore.makeUnsafe(4);
42+
const gitProcesses = Semaphore.makeUnsafe(8);
4343
// `git worktree add` checks out the full tree, so on large repositories it can
4444
// take well beyond the default 30s (e.g. a 375k-file repo takes ~40s on an idle
4545
// machine). Give it generous headroom while still bounding a genuinely hung git.
@@ -904,7 +904,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
904904
operation: input.operation,
905905
},
906906
}),
907-
gitProcesses.withPermits(1),
907+
(execution) =>
908+
input.timeoutMs === null || (input.timeoutMs ?? DEFAULT_TIMEOUT_MS) > DEFAULT_TIMEOUT_MS
909+
? execution
910+
: gitProcesses.withPermits(1)(execution),
908911
Effect.withSpan(input.operation, {
909912
kind: "client",
910913
attributes: {

0 commit comments

Comments
 (0)