Skip to content

Commit eeb32df

Browse files
committed
fix(server): cap the git processes the metadata reader starts on its own
1 parent c1ad0be commit eeb32df

2 files changed

Lines changed: 103 additions & 25 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vite-p
77

88
import {
99
gitAnswerMemoKey,
10+
makeTaskLimiter,
1011
parseGitConfig,
1112
rememberGitAnswer,
1213
resetGitFastPathCaches,
@@ -509,6 +510,9 @@ describe("GitMetadataFastPath on repositories git treats differently", () => {
509510
it("stays inside the caller's time and output budget", async () => {
510511
const cwd = repos.plain!;
511512
expect(await tryAnswerGitCommand({ cwd, args: ["remote", "-v"], timeoutMs: 0 })).toBeNull();
513+
// Same with everything already cached, when the reads alone might beat a zero timer.
514+
expect(await tryAnswerGitCommand({ cwd, args: ["remote", "-v"] })).not.toBeNull();
515+
expect(await tryAnswerGitCommand({ cwd, args: ["remote", "-v"], timeoutMs: 0 })).toBeNull();
512516
expect(
513517
await tryAnswerGitCommand({ cwd, args: ["remote", "-v"], timeoutMs: null }),
514518
).not.toBeNull();
@@ -740,6 +744,43 @@ describe("GitMetadataFastPath on repositories git treats differently", () => {
740744
});
741745
});
742746

747+
describe("makeTaskLimiter", () => {
748+
it("runs a bounded number of tasks at once and frees the slot of a failed one", async () => {
749+
const limit = makeTaskLimiter(3);
750+
const release: Array<(fail: boolean) => void> = [];
751+
let running = 0;
752+
let mostRunning = 0;
753+
const results = Array.from({ length: 10 }, (_, index) =>
754+
limit(async () => {
755+
mostRunning = Math.max(mostRunning, ++running);
756+
const failed = await new Promise<boolean>((resolve) => release.push(resolve));
757+
running--;
758+
if (failed) throw new Error(`task ${index}`);
759+
return index;
760+
}).catch((error: Error) => error.message),
761+
);
762+
// Release in start order; every third task fails.
763+
for (let done = 0; done < 10; done++) {
764+
while (release.length <= done) await new Promise((resolve) => setImmediate(resolve));
765+
expect(running).toBeLessThanOrEqual(3);
766+
release[done]!(done % 3 === 0);
767+
}
768+
expect(await Promise.all(results)).toEqual([
769+
"task 0",
770+
1,
771+
2,
772+
"task 3",
773+
4,
774+
5,
775+
"task 6",
776+
7,
777+
8,
778+
"task 9",
779+
]);
780+
expect(mostRunning).toBe(3);
781+
});
782+
});
783+
743784
describe("parseGitConfig", () => {
744785
it("handles quoting, escapes, comments and continuations like git", () => {
745786
const entries = parseGitConfig(

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

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,31 @@ function movesGitOrItsConfig(env: NodeJS.ProcessEnv | undefined): boolean {
115115
return false;
116116
}
117117

118+
/**
119+
* Runs at most `max` of the given tasks at once, the rest in arrival order.
120+
* Exported for tests.
121+
*/
122+
export function makeTaskLimiter(max: number) {
123+
let running = 0;
124+
const waiting: Array<() => void> = [];
125+
return async <T>(task: () => Promise<T>): Promise<T> => {
126+
// A finishing task hands its slot to the next one, so `running` stays put.
127+
if (running >= max) await new Promise<void>((resolve) => waiting.push(resolve));
128+
else running++;
129+
try {
130+
return await task();
131+
} finally {
132+
const next = waiting.shift();
133+
if (next) next();
134+
else running--;
135+
}
136+
};
137+
}
138+
139+
// The git processes started here run outside the drivers' process permits. A
140+
// sweep over many repositories asks for one verdict each, all at once.
141+
const withOwnGitProcess = makeTaskLimiter(4);
142+
118143
const toGitPath = (value: string) => (NodePath.sep === "\\" ? value.replaceAll("\\", "/") : value);
119144

120145
async function statOrNull(target: string) {
@@ -328,14 +353,17 @@ async function fingerprintFiles(files: ReadonlyArray<string>): Promise<string> {
328353
}
329354

330355
function listOuterConfig(): Promise<string> {
331-
return new Promise((resolve, reject) => {
332-
NodeChildProcess.execFile(
333-
"git",
334-
["config", "--list", "--show-scope", "--show-origin", "-z"],
335-
{ cwd: NodeOS.tmpdir(), windowsHide: true, timeout: 10_000, maxBuffer: 4 * 1024 * 1024 },
336-
(error, stdout) => (error ? reject(error) : resolve(stdout)),
337-
);
338-
});
356+
return withOwnGitProcess(
357+
() =>
358+
new Promise((resolve, reject) => {
359+
NodeChildProcess.execFile(
360+
"git",
361+
["config", "--list", "--show-scope", "--show-origin", "-z"],
362+
{ cwd: NodeOS.tmpdir(), windowsHide: true, timeout: 10_000, maxBuffer: 4 * 1024 * 1024 },
363+
(error, stdout) => (error ? reject(error) : resolve(stdout)),
364+
);
365+
}),
366+
);
339367
}
340368

341369
/** Where git looks for an `include.path` value found in `originFile`. */
@@ -555,23 +583,30 @@ interface GitVerdict {
555583
const verdicts = new Map<string, { readonly at: number; readonly verdict: Promise<GitVerdict> }>();
556584

557585
function askGit(args: ReadonlyArray<string>): Promise<GitVerdict> {
558-
return new Promise((resolve, reject) => {
559-
NodeChildProcess.execFile(
560-
"git",
561-
[...args],
562-
{
563-
cwd: NodeOS.tmpdir(),
564-
env: { ...process.env, LC_ALL: "C" },
565-
windowsHide: true,
566-
timeout: 10_000,
567-
maxBuffer: 64 * 1024,
568-
},
569-
(error, stdout, stderr) =>
570-
error && typeof error.code !== "number"
571-
? reject(error)
572-
: resolve({ exitCode: typeof error?.code === "number" ? error.code : 0, stdout, stderr }),
573-
);
574-
});
586+
return withOwnGitProcess(
587+
() =>
588+
new Promise((resolve, reject) => {
589+
NodeChildProcess.execFile(
590+
"git",
591+
[...args],
592+
{
593+
cwd: NodeOS.tmpdir(),
594+
env: { ...process.env, LC_ALL: "C" },
595+
windowsHide: true,
596+
timeout: 10_000,
597+
maxBuffer: 64 * 1024,
598+
},
599+
(error, stdout, stderr) =>
600+
error && typeof error.code !== "number"
601+
? reject(error)
602+
: resolve({
603+
exitCode: typeof error?.code === "number" ? error.code : 0,
604+
stdout,
605+
stderr,
606+
}),
607+
);
608+
}),
609+
);
575610
}
576611

577612
function gitVerdict(args: ReadonlyArray<string>): Promise<GitVerdict> {
@@ -1237,6 +1272,8 @@ export async function tryAnswerGitCommand(
12371272
if (!isGitFastPathEnabled() || !isGitFastPathEnabled(input.env ?? {})) return null;
12381273
if (hasGitEnvOverride(process.env) || hasGitEnvOverride(input.env)) return null;
12391274
if (movesGitOrItsConfig(input.env)) return null;
1275+
// No budget at all: nothing may be answered, however fast the reads turn out.
1276+
if (typeof input.timeoutMs === "number" && input.timeoutMs <= 0) return null;
12401277
const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
12411278
let timer: NodeJS.Timeout | undefined;
12421279
try {

0 commit comments

Comments
 (0)