Skip to content

Commit 6358de0

Browse files
committed
fix(server): keep the rev-list memo lookup inside the caller's time budget
The memo key read the repository files with no limit before git was spawned, so a stuck disk could hold up the timed fallback. It now shares the reader's budget, min(2 s, timeoutMs). The git permit tests drive a virtual clock and opt out of the fast path, whose file reads run on the real one.
1 parent eeb32df commit 6358de0

4 files changed

Lines changed: 58 additions & 17 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ describe("GitMetadataFastPath", () => {
292292
expect(key).not.toBeNull();
293293
await rememberGitAnswer(input, key!, ask());
294294
expect(await tryAnswerGitCommand(input)).toMatchObject({ exitCode: 0, stdout: "1\t0\n" });
295+
// The key lookup shares the caller's budget: git is about to run either way.
296+
expect(await gitAnswerMemoKey({ ...input, timeoutMs: 0 })).toBeNull();
295297

296298
// A moved ref is a different question.
297299
commit(cwd, "local 2");

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,20 +1272,12 @@ export async function tryAnswerGitCommand(
12721272
if (!isGitFastPathEnabled() || !isGitFastPathEnabled(input.env ?? {})) return null;
12731273
if (hasGitEnvOverride(process.env) || hasGitEnvOverride(input.env)) return null;
12741274
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;
12771275
const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
1278-
let timer: NodeJS.Timeout | undefined;
12791276
try {
1280-
// Reads that take this long mean a stuck disk or share; git gets the question instead.
1281-
const timedOut = new Promise<null>((resolve) => {
1282-
timer = setTimeout(resolve, Math.min(ANSWER_TIMEOUT_MS, input.timeoutMs ?? Infinity), null);
1283-
});
12841277
// Asking for the outer config first proves git runs at all, so a missing git is not papered over.
1285-
const answering = getOuterConfig().then(() => answer(input));
1286-
// When the timer wins, the abandoned attempt still settles; its decline is not an error.
1287-
answering.catch(() => undefined);
1288-
const result = await Promise.race([answering, timedOut]);
1278+
const result = await withinReadBudget(input.timeoutMs, () =>
1279+
getOuterConfig().then(() => answer(input)),
1280+
);
12891281
return result !== null &&
12901282
Math.max(Buffer.byteLength(result.stdout), Buffer.byteLength(result.stderr)) > maxOutputBytes
12911283
? null
@@ -1294,6 +1286,30 @@ export async function tryAnswerGitCommand(
12941286
return error instanceof NotARepository && !gitMessagesMayBeTranslated(input.env)
12951287
? { exitCode: 128, stdout: "", stderr: error.stderr }
12961288
: null;
1289+
}
1290+
}
1291+
1292+
/**
1293+
* Settles with `work`'s result, or with `null` once the caller's budget or
1294+
* ANSWER_TIMEOUT_MS runs out, whichever comes first. A rejection of `work`
1295+
* before then is passed on.
1296+
*/
1297+
async function withinReadBudget<T>(
1298+
timeoutMs: number | null | undefined,
1299+
work: () => Promise<T>,
1300+
): Promise<T | null> {
1301+
// No budget at all: nothing may be answered, however fast the reads turn out.
1302+
if (typeof timeoutMs === "number" && timeoutMs <= 0) return null;
1303+
let timer: NodeJS.Timeout | undefined;
1304+
// Reads that take this long mean a stuck disk or share; git gets the question instead.
1305+
const timedOut = new Promise<null>((resolve) => {
1306+
timer = setTimeout(resolve, Math.min(ANSWER_TIMEOUT_MS, timeoutMs ?? Infinity), null);
1307+
});
1308+
const working = work();
1309+
// When the timer wins, the abandoned attempt still settles; its decline is not an error.
1310+
working.catch(() => undefined);
1311+
try {
1312+
return await Promise.race([working, timedOut]);
12971313
} finally {
12981314
clearTimeout(timer);
12991315
}
@@ -1311,9 +1327,12 @@ export async function gitAnswerMemoKey(input: GitFastPathInput): Promise<string
13111327
const [command, ...rest] = input.args;
13121328
if (command !== "rev-list") return null;
13131329
try {
1314-
const { repo } = await discoverRepository(input.cwd);
1315-
await requireGitAgrees(repo, false);
1316-
return (await revListQuery(repo, rest)).key;
1330+
// Without a key git still runs, so a stuck disk must not hold up the spawn.
1331+
return await withinReadBudget(input.timeoutMs, async () => {
1332+
const { repo } = await discoverRepository(input.cwd);
1333+
await requireGitAgrees(repo, false);
1334+
return (await revListQuery(repo, rest)).key;
1335+
});
13171336
} catch {
13181337
return null;
13191338
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ const makeSuccessfulHandle = (stdout: string) =>
7676
getOutputFd: () => Stream.empty,
7777
});
7878

79+
// Permit tests drive TestClock; the fast path reads real files on the real clock first.
80+
const SPAWN_ONLY_ENV = { T3CODE_GIT_FAST_PATH: "0" };
81+
7982
const makeTmpDir = (
8083
prefix = "git-vcs-driver-test-",
8184
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
@@ -180,6 +183,7 @@ it.effect("bounds Git bursts across drivers without timing out queued commands",
180183
operation: "test.gitBurst",
181184
cwd: "/repo",
182185
args: ["rev-parse", "HEAD"],
186+
env: SPAWN_ONLY_ENV,
183187
...(index < 4 ? {} : { timeoutMs: index < 8 ? 30_000 : 1_000 }),
184188
}),
185189
{ concurrency: "unbounded" },
@@ -231,12 +235,23 @@ it.effect.each([{ timeoutMs: null }, { timeoutMs: 30_001 }])(
231235
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
232236
);
233237
const slow = yield* driver
234-
.execute({ operation: "test.slowGit", cwd: "/repo", args: ["push"], timeoutMs })
238+
.execute({
239+
operation: "test.slowGit",
240+
cwd: "/repo",
241+
args: ["push"],
242+
env: SPAWN_ONLY_ENV,
243+
timeoutMs,
244+
})
235245
.pipe(Effect.forkChild);
236246
yield* Queue.take(starts);
237247
const burst = yield* Effect.all(
238248
Array.from({ length: 8 }, () =>
239-
driver.execute({ operation: "test.fastGit", cwd: "/repo", args: ["status"] }),
249+
driver.execute({
250+
operation: "test.fastGit",
251+
cwd: "/repo",
252+
args: ["status"],
253+
env: SPAWN_ONLY_ENV,
254+
}),
240255
),
241256
{ concurrency: "unbounded" },
242257
).pipe(Effect.forkChild);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
962962
} satisfies GitVcsDriver.ExecuteGitResult;
963963
});
964964

965-
const fastPathInput = { cwd: input.cwd, args: input.args, env: input.env };
965+
const fastPathInput = {
966+
cwd: input.cwd,
967+
args: input.args,
968+
env: input.env,
969+
timeoutMs: input.timeoutMs,
970+
};
966971
const memoKey =
967972
input.stdin === undefined && input.progress === undefined
968973
? yield* Effect.promise(() => GitMetadataFastPath.gitAnswerMemoKey(fastPathInput))

0 commit comments

Comments
 (0)