Skip to content

Commit 9b3b737

Browse files
committed
fix(server): recover pulls from unrelated ref locks
- Retry failed pulls against the configured upstream when an unrelated ref lock blocks fetch. - Keep status fetches running across poll deadlines and client cancellation.
1 parent 7ce7fe3 commit 9b3b737

2 files changed

Lines changed: 345 additions & 29 deletions

File tree

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

Lines changed: 236 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,13 @@ it.effect("coalesces concurrent ref pages into one repository snapshot", () =>
492492
).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
493493
);
494494

495-
describe("pull output beyond the output cap", () => {
495+
describe("pullCurrentBranch", () => {
496496
// A fast-forward that prints over 1 MB takes thousands of files, so the real pull's output is
497497
// padded past the cap instead.
498-
const withPaddedPull = (stream: "stdout" | "stderr") =>
498+
const withPaddedPull = (
499+
stream: "stdout" | "stderr",
500+
onPull?: (args: ReadonlyArray<string>) => void,
501+
) =>
499502
Effect.gen(function* () {
500503
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
501504
const padding = new Uint8Array(1_000_001).fill(0x20);
@@ -506,6 +509,7 @@ describe("pull output beyond the output cap", () => {
506509
ChildProcess.isStandardCommand(command) &&
507510
command.args.includes("pull") &&
508511
command.args.includes("--ff-only");
512+
if (isPull) onPull?.(command.args);
509513
return isPull
510514
? ChildProcessSpawner.makeHandle({
511515
...handle,
@@ -534,9 +538,235 @@ describe("pull output beyond the output cap", () => {
534538
yield* git(updater, ["add", "remote.txt"]);
535539
yield* git(updater, ["commit", "-m", "remote commit"]);
536540
yield* git(updater, ["push", "origin", initialBranch]);
537-
return { cwd, remoteHead: yield* git(updater, ["rev-parse", "HEAD"]) };
541+
return {
542+
cwd,
543+
remote,
544+
updater,
545+
initialBranch,
546+
remoteHead: yield* git(updater, ["rev-parse", "HEAD"]),
547+
};
538548
});
539549

550+
it.effect("still fetches other branches on the first pull", () =>
551+
Effect.gen(function* () {
552+
const driver = yield* GitVcsDriver.GitVcsDriver;
553+
const { cwd, updater, remoteHead } = yield* cloneBehindRemote;
554+
yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);
555+
556+
const result = yield* driver.pullCurrentBranch(cwd);
557+
558+
assert.equal(result.status, "pulled");
559+
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);
560+
assert.equal(yield* git(cwd, ["rev-parse", "refs/remotes/origin/other"]), remoteHead);
561+
}).pipe(Effect.provide(layerTest)),
562+
);
563+
564+
it.effect.each([false, true])(
565+
"pulls past an unrelated lock with custom fetch mapping=%s",
566+
(customMapping) =>
567+
Effect.gen(function* () {
568+
const driver = yield* GitVcsDriver.GitVcsDriver;
569+
const fs = yield* FileSystem.FileSystem;
570+
const { cwd, updater, initialBranch, remoteHead } = yield* cloneBehindRemote;
571+
yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);
572+
const remoteName = customMapping ? "team/upstream" : "origin";
573+
const refPrefix = customMapping
574+
? `refs/remotes/${remoteName}/mapped`
575+
: "refs/remotes/origin";
576+
if (customMapping) {
577+
yield* git(cwd, ["remote", "rename", "origin", remoteName]);
578+
yield* git(cwd, ["config", `remote.${remoteName}.fetch`, `+refs/heads/*:${refPrefix}/*`]);
579+
yield* git(cwd, ["update-ref", `${refPrefix}/${initialBranch}`, "HEAD"]);
580+
}
581+
const lock = `.git/${refPrefix}/other.lock`;
582+
yield* writeTextFile(cwd, lock, "");
583+
584+
const result = yield* driver.pullCurrentBranch(cwd);
585+
586+
assert.equal(result.status, "pulled");
587+
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);
588+
assert.equal(yield* git(cwd, ["rev-parse", `${refPrefix}/${initialBranch}`]), remoteHead);
589+
assert.isTrue(yield* fs.exists(`${cwd}/${lock}`));
590+
assert.equal(yield* fs.readFileString(`${cwd}/${lock}`), "");
591+
}).pipe(Effect.provide(layerTest)),
592+
);
593+
594+
it.effect("reports an upstream lock after the fallback without removing it", () =>
595+
Effect.gen(function* () {
596+
const driver = yield* GitVcsDriver.GitVcsDriver;
597+
const fs = yield* FileSystem.FileSystem;
598+
const { cwd, initialBranch } = yield* cloneBehindRemote;
599+
const before = yield* git(cwd, ["rev-parse", "HEAD"]);
600+
const lock = `.git/refs/remotes/origin/${initialBranch}.lock`;
601+
yield* writeTextFile(cwd, lock, "");
602+
603+
const error = yield* Effect.flip(driver.pullCurrentBranch(cwd));
604+
605+
assert.include(error.detail, "could not update a local reference");
606+
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), before);
607+
assert.isTrue(yield* fs.exists(`${cwd}/${lock}`));
608+
}).pipe(Effect.provide(layerTest)),
609+
);
610+
611+
it.effect.each(["branch", "HEAD"] as const)(
612+
"does not retry a failed fetch if another operation moved %s",
613+
(moved) =>
614+
Effect.gen(function* () {
615+
const fixtureDriver = yield* GitVcsDriver.GitVcsDriver;
616+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
617+
const { cwd, updater } = yield* cloneBehindRemote;
618+
yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);
619+
yield* writeTextFile(cwd, ".git/refs/remotes/origin/other.lock", "");
620+
let pulls = 0;
621+
const spawner = ChildProcessSpawner.make((command) =>
622+
Effect.gen(function* () {
623+
const handle = yield* delegate.spawn(command);
624+
if (!ChildProcess.isStandardCommand(command) || !command.args.includes("pull")) {
625+
return handle;
626+
}
627+
pulls += 1;
628+
return ChildProcessSpawner.makeHandle({
629+
...handle,
630+
exitCode: handle.exitCode.pipe(
631+
Effect.tap(() =>
632+
git(
633+
cwd,
634+
moved === "branch"
635+
? ["checkout", "-b", "moved"]
636+
: ["commit", "--allow-empty", "-m", "concurrent commit"],
637+
).pipe(
638+
Effect.provideService(GitVcsDriver.GitVcsDriver, fixtureDriver),
639+
Effect.orDie,
640+
),
641+
),
642+
),
643+
});
644+
}),
645+
);
646+
const driver = yield* makeGitVcsDriverCore().pipe(
647+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
648+
);
649+
650+
const error = yield* Effect.flip(driver.pullCurrentBranch(cwd));
651+
652+
assert.include(error.detail, "could not update a local reference");
653+
assert.equal(pulls, 1);
654+
}).pipe(Effect.scoped, Effect.provide(layerServerConfig.pipe(Layer.provideMerge(layerTest)))),
655+
);
656+
657+
it.effect.each(["deadline", "subscriber cancellation"] as const)(
658+
"lets a background fetch finish after %s without leaving its lock behind",
659+
(interruption) =>
660+
Effect.gen(function* () {
661+
const fs = yield* FileSystem.FileSystem;
662+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
663+
const { cwd } = yield* cloneBehindRemote;
664+
const lockPath = `${cwd}/.git/refs/remotes/origin/in-flight.lock`;
665+
const started = yield* Deferred.make<void>();
666+
const release = yield* Deferred.make<void>();
667+
const finalized = yield* Deferred.make<void>();
668+
const spawner = ChildProcessSpawner.make((command) =>
669+
Effect.gen(function* () {
670+
if (!ChildProcess.isStandardCommand(command) || !command.args.includes("fetch")) {
671+
return yield* delegate.spawn(command);
672+
}
673+
// Model Git's gap between creating a lock and registering signal cleanup.
674+
yield* fs.writeFileString(lockPath, "");
675+
yield* Effect.addFinalizer(() => Deferred.succeed(finalized, undefined));
676+
yield* Deferred.succeed(started, undefined);
677+
return ChildProcessSpawner.makeHandle({
678+
...makeSuccessfulHandle(""),
679+
exitCode: Deferred.await(release).pipe(
680+
Effect.andThen(fs.remove(lockPath)),
681+
Effect.as(ChildProcessSpawner.ExitCode(0)),
682+
),
683+
});
684+
}),
685+
);
686+
const driver = yield* makeGitVcsDriverCore().pipe(
687+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
688+
);
689+
const status = yield* driver.statusDetailsRemote(cwd).pipe(Effect.forkScoped);
690+
yield* Deferred.await(started);
691+
692+
if (interruption === "deadline") {
693+
yield* TestClock.adjust("5 seconds");
694+
yield* Fiber.join(status);
695+
} else {
696+
yield* Fiber.interrupt(status);
697+
}
698+
699+
assert.isFalse(yield* Deferred.isDone(finalized));
700+
assert.isTrue(yield* fs.exists(lockPath));
701+
702+
yield* Deferred.succeed(release, undefined);
703+
yield* Deferred.await(finalized);
704+
assert.isFalse(yield* fs.exists(lockPath));
705+
}).pipe(Effect.scoped, Effect.provide(layerServerConfig.pipe(Layer.provideMerge(layerTest)))),
706+
);
707+
708+
it.effect("reuses a background fetch across status wait deadlines", () =>
709+
Effect.gen(function* () {
710+
const fs = yield* FileSystem.FileSystem;
711+
const cwd = yield* makeTmpDir();
712+
yield* fs.makeDirectory(`${cwd}/.git`);
713+
const started = yield* Deferred.make<void>();
714+
const release = yield* Deferred.make<void>();
715+
const finalized = yield* Deferred.make<void>();
716+
const attempts = yield* Ref.make(0);
717+
const spawner = ChildProcessSpawner.make((command) =>
718+
Effect.gen(function* () {
719+
if (!ChildProcess.isStandardCommand(command)) return makeNonRepositoryHandle();
720+
if (command.args.includes("fetch")) {
721+
yield* Ref.update(attempts, (count) => count + 1);
722+
yield* Deferred.succeed(started, undefined);
723+
yield* Effect.addFinalizer(() => Deferred.succeed(finalized, undefined));
724+
return ChildProcessSpawner.makeHandle({
725+
...makeSuccessfulHandle(""),
726+
exitCode: Deferred.await(release).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
727+
});
728+
}
729+
const args = command.args;
730+
return makeSuccessfulHandle(
731+
args.includes("--git-common-dir")
732+
? `${cwd}/.git\n`
733+
: args.includes("--show-toplevel")
734+
? `${cwd}\n`
735+
: args.includes("@{upstream}")
736+
? "origin/main\n"
737+
: args[0] === "remote"
738+
? "origin\n"
739+
: args[0] === "rev-list"
740+
? "0\t0\n"
741+
: args.includes("refs/remotes/origin/HEAD")
742+
? "refs/remotes/origin/main\n"
743+
: "main\n",
744+
);
745+
}),
746+
);
747+
const driver = yield* makeGitVcsDriverCore().pipe(
748+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
749+
);
750+
const first = yield* driver.statusDetailsRemote(cwd).pipe(Effect.forkScoped);
751+
yield* Deferred.await(started);
752+
yield* TestClock.adjust("5 seconds");
753+
yield* Fiber.join(first);
754+
755+
yield* TestClock.adjust("15 seconds");
756+
const second = yield* driver.statusDetailsRemote(cwd).pipe(Effect.forkScoped);
757+
yield* TestClock.adjust("5 seconds");
758+
yield* Fiber.join(second);
759+
760+
assert.equal(yield* Ref.get(attempts), 1);
761+
assert.isFalse(yield* Deferred.isDone(finalized));
762+
yield* Deferred.succeed(release, undefined);
763+
yield* Deferred.await(finalized);
764+
}).pipe(
765+
Effect.scoped,
766+
Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer))),
767+
),
768+
);
769+
540770
it.effect.each(["stdout", "stderr"] as const)(
541771
"reports a fast-forward whose %s passes the cap as pulled",
542772
(stream) =>
@@ -558,7 +788,8 @@ describe("pull output beyond the output cap", () => {
558788
it.effect("still fails a pull that Git rejects, whatever its output size", () =>
559789
Effect.scoped(
560790
Effect.gen(function* () {
561-
const driver = yield* withPaddedPull("stderr");
791+
const pulls: ReadonlyArray<string>[] = [];
792+
const driver = yield* withPaddedPull("stderr", (args) => pulls.push(args));
562793
yield* Effect.gen(function* () {
563794
const { cwd } = yield* cloneBehindRemote;
564795
yield* writeTextFile(cwd, "local.txt", "local\n");
@@ -570,6 +801,7 @@ describe("pull output beyond the output cap", () => {
570801
assert.equal(error.operation, "GitVcsDriver.pullCurrentBranch.pull");
571802
assert.equal(error.exitCode, 128);
572803
assert.notInclude(error.detail, "exceeded");
804+
assert.lengthOf(pulls, 1);
573805
}).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver));
574806
}),
575807
).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),

0 commit comments

Comments
 (0)