Skip to content

Commit e589c74

Browse files
Gourav224claude
andcommitted
fix(server): a branch checkout that outlives its request clears the ref caches when it ends
The request's own cache invalidation runs when the request ends, which for a cancelled switch is before git has finished rewriting the tree. A read in that window could cache the old branch for the cache lifetime. Clear the caches from the forked checkout as well, once git exits. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 5f952f3 commit e589c74

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,11 @@ const makeSlowCheckout = Effect.gen(function* () {
270270
const gate = yield* Deferred.make<void>();
271271
const started = yield* Deferred.make<void>();
272272
const released = yield* Deferred.make<void>();
273+
const spawned: Array<string> = [];
273274
const spawner = ChildProcessSpawner.make((command) =>
274275
Effect.acquireRelease(
275276
Effect.gen(function* () {
277+
if (ChildProcess.isStandardCommand(command)) spawned.push(command.args.join(" "));
276278
if (!ChildProcess.isStandardCommand(command) || command.args[0] !== "checkout") {
277279
return makeSuccessfulHandle("feature");
278280
}
@@ -291,7 +293,7 @@ const makeSlowCheckout = Effect.gen(function* () {
291293
const driver = yield* makeGitVcsDriverCore().pipe(
292294
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
293295
);
294-
return { driver, started, released, finish: Deferred.succeed(gate, undefined) };
296+
return { driver, started, released, spawned, finish: Deferred.succeed(gate, undefined) };
295297
});
296298

297299
// A checkout rewriting thousands of files can outlast any fixed limit, and git cannot roll back
@@ -314,7 +316,7 @@ it.effect("lets a slow branch checkout finish instead of killing it partway", ()
314316

315317
it.effect("keeps a branch checkout running when the request that started it is interrupted", () =>
316318
Effect.gen(function* () {
317-
const { driver, started, released, finish } = yield* makeSlowCheckout;
319+
const { driver, started, released, spawned, finish } = yield* makeSlowCheckout;
318320

319321
const switching = yield* driver
320322
.switchRef({ cwd: "/repo", refName: "feature" })
@@ -323,8 +325,14 @@ it.effect("keeps a branch checkout running when the request that started it is i
323325
yield* Fiber.interrupt(switching);
324326
assert.isFalse(yield* Deferred.isDone(released), "git was killed with its caller");
325327

328+
// The caller's own cache invalidation ran when it was interrupted, before git finished
329+
// rewriting the tree, so a read since could have cached the old branch.
330+
yield* TestClock.adjust("0 seconds");
331+
const spawnedBeforeFinish = spawned.length;
326332
yield* finish;
327333
yield* Deferred.await(released);
334+
yield* TestClock.adjust("0 seconds");
335+
assert.isAbove(spawned.length, spawnedBeforeFinish, "caches were not cleared after git exited");
328336
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
329337
);
330338

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4361,15 +4361,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
43614361
// updating the index and HEAD, which leaves the old branch checked out over the new
43624362
// branch's files. So nothing may end it early: no timeout, no output cap (the output
43634363
// is never read), and a cancelled request stops waiting without killing git.
4364-
const checkout = yield* executeGitWithStableDiagnostics(
4365-
"GitVcsDriver.switchRef.checkout",
4364+
// The caller's own invalidation can run before git exits, so the forked checkout clears
4365+
// the ref caches itself once it ends.
4366+
const checkout = yield* withListRefsInvalidation(
43664367
input.cwd,
4367-
[...checkoutArgs, "--"],
4368-
{
4369-
timeoutMs: null,
4370-
appendTruncationMarker: true,
4371-
fallbackErrorDetail: "git checkout failed",
4372-
},
4368+
executeGitWithStableDiagnostics(
4369+
"GitVcsDriver.switchRef.checkout",
4370+
input.cwd,
4371+
[...checkoutArgs, "--"],
4372+
{
4373+
timeoutMs: null,
4374+
appendTruncationMarker: true,
4375+
fallbackErrorDetail: "git checkout failed",
4376+
},
4377+
),
43734378
).pipe(Effect.forkIn(serviceScope));
43744379
yield* Fiber.join(checkout);
43754380

0 commit comments

Comments
 (0)