Split from #14624 at review. Two forks of one parent made at once can race on the lender's config.lock in keepLentObjects (src/git/repos.ts), so one of the forks fails. The check on #14624 found this. The budget issue did not.
The fix proposed there reads the lender's config before writing it and retries a lost lock. The review asked for two more things: make the retry (5 attempts, 20 ms backoff) a named policy, and test how long a held lock takes to fail, not only that it fails.
diff --git a/src/git/repos.ts b/src/git/repos.tsindex 0562ef8c6..405d42d0d 100644--- a/src/git/repos.ts+++ b/src/git/repos.ts@@ -119,12 +119,37 @@ export async function keepLentObjects(forkDir: string): Promise<Result<void, Git
const path = line.trim();
if (path === "" || path.startsWith("#")) continue;
const lender = dirname(isAbsolute(path) ? path : resolve(own, path));
- const set = await runGit(["config", "gc.pruneExpire", "never"], { cwd: lender });- if (!set.ok) return set;+ // Read before writing: two forks of one parent made at once both reach+ // here, and git's config write takes `config.lock` — the second writer is+ // refused "could not lock config file" though the first set what both+ // wanted. A lender already kept is not written again, and a write that+ // lost the lock is tried again shortly: by then the winner's rename has+ // landed and the read finds it kept.+ const kept = await keep(lender);+ if (!kept.ok) return kept;
}
return ok();
}
+/** Tries at one lender's config before its lock is a failure; 20 ms more each time. */+const KEEP_TRIES = 5;++async function keep(lender: string): Promise<Result<void, GitFailure>> {+ for (let attempt = 1; ; attempt++) {+ if (await keeps(lender)) return ok();+ const set = await runGit(["config", "gc.pruneExpire", "never"], { cwd: lender });+ if (set.ok) return ok();+ if (attempt === KEEP_TRIES) return (await keeps(lender)) ? ok() : set;+ await Bun.sleep(20 * attempt); // real-timer: another writer's lock is held for milliseconds+ }+}++/** Does this repository already keep its unreachable objects forever? */+async function keeps(dir: string): Promise<boolean> {+ const got = await runGit(["config", "--get", "gc.pruneExpire"], { cwd: dir });+ return got.ok && got.value === "never";+}+
/** A fork half made is no fork: remove it, so a second try finds a free place. */
async function undo<E>(forkDir: string, failure: E): Promise<E> {
await rm(forkDir, { recursive: true, force: true });
diff --git a/test/fork-lender-keeps-objects.test.ts b/test/fork-lender-keeps-objects.test.tsindex 06cdf4b50..1b46c9608 100644--- a/test/fork-lender-keeps-objects.test.ts+++ b/test/fork-lender-keeps-objects.test.ts@@ -1,6 +1,6 @@
/** A repository that lends objects to a fork never prunes what only the fork reaches. */
import { expect, test } from "bun:test";
-import { readdirSync, utimesSync } from "node:fs";+import { readdirSync, rmSync, utimesSync, writeFileSync } from "node:fs";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -89,6 +89,40 @@ test("a parent forked before the setting existed is covered by keepLentObjects,
}
});
+test("a lender already kept is not written again: its config locked by another writer is no failure", async () => {+ const root = await mkdtemp(join(tmpdir(), "lender-"));+ try {+ const { parent, fork } = await setup(root);+ // Another fork of the same parent is mid-write: git refuses a second writer.+ writeFileSync(join(parent, "config.lock"), "");+ expect((await keepLentObjects(fork)).ok).toBe(true);+ // Not yet kept, the lock is a real failure, said as one.+ rmSync(join(parent, "config.lock"));+ await git(parent, "config", "--unset", "gc.pruneExpire");+ writeFileSync(join(parent, "config.lock"), "");+ expect((await keepLentObjects(fork)).ok).toBe(false);+ // A lock let go in milliseconds (the other writer finishing) is waited out.+ setTimeout(() => rmSync(join(parent, "config.lock")), 50); // real-timer: a writer's lock held for 50 ms+ expect((await keepLentObjects(fork)).ok).toBe(true);+ expect((await git(parent, "config", "--get", "gc.pruneExpire")).out).toBe("never");+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});++test("eight forks of one parent made at once all succeed (two agents forking one repository)", async () => {+ const root = await mkdtemp(join(tmpdir(), "lender-"));+ try {+ const { parent } = await setup(root);+ const made = await Promise.all(+ Array.from({ length: 8 }, (_, i) => forkBareRepo(parent, join(root, `f${i}.git`))),+ );+ expect(made.map((m) => m.ok)).toEqual(Array.from({ length: 8 }, () => true));+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});+
test("a repository that borrows nothing is left alone", async () => {
const root = await mkdtemp(join(tmpdir(), "lender-"));
try {
Split from #14624 at review. Two forks of one parent made at once can race on the lender's
config.lockinkeepLentObjects(src/git/repos.ts), so one of the forks fails. The check on #14624 found this. The budget issue did not.The fix proposed there reads the lender's config before writing it and retries a lost lock. The review asked for two more things: make the retry (5 attempts, 20 ms backoff) a named policy, and test how long a held lock takes to fail, not only that it fails.
The patch as it was on #14624: