Public: anyone can read this; the crew can change it.

Two forks of one parent made at once both succeed: the lender's config.lock race #14709

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/14684-two-forks-of-one-parent-made-at-once-bot → main 1 commit fork 1 thread · 1 unresolved

  • Merged. torque/14684-two-forks-of-one-parent-made-at-once-bot was merged into main as 9e5252e.

Review

Sign in to review this change.
2 files changed +69 −4
src/git/repos.ts +28 −2
@@ -119,12 +119,38 @@ export async function keepLentObjects(forkDir: string): Promise<Result<void, Git
119119 const path = line.trim();
120120 if (path === "" || path.startsWith("#")) continue;
121121 const lender = dirname(isAbsolute(path) ? path : resolve(own, path));
122 const set = await runGit(["config", "gc.pruneExpire", "never"], { cwd: lender });
123 if (!set.ok) return set;
122 // Read before writing: two forks of one parent made at once both reach here, and
123 // git's config write takes `config.lock`, so the second writer is refused "could not
124 // lock config file" though the first set what both wanted. A lender already kept is
125 // not written again; a write that lost the lock is tried again shortly.
126 const kept = await keep(lender);
127 if (!kept.ok) return kept;
124128 }
125129 return ok();
126130 }
127131
132 /**
133 * The policy for a lender's config lock: `tries` writes, waiting `backoffMs` times the
134 * attempt number between them (20, 40, 60, 80 ms: a held lock fails after about 200 ms).
135 */
136 export const KEEP_RETRY = { tries: 5, backoffMs: 20 } as const;
137
138 async function keep(lender: string): Promise<Result<void, GitFailure>> {
139 for (let attempt = 1; ; attempt++) {
140 if (await keeps(lender)) return ok();
141 const set = await runGit(["config", "gc.pruneExpire", "never"], { cwd: lender });
142 if (set.ok) return ok();
143 if (attempt === KEEP_RETRY.tries) return (await keeps(lender)) ? ok() : set;
144 await Bun.sleep(KEEP_RETRY.backoffMs * attempt); // real-timer: another writer's lock lasts milliseconds
145 }
146 }
147
148 /** Does this repository already keep its unreachable objects forever? */
149 async function keeps(dir: string): Promise<boolean> {
150 const got = await runGit(["config", "--get", "gc.pruneExpire"], { cwd: dir });
151 return got.ok && got.value === "never";
152 }
153
128154 /** A fork half made is no fork: remove it, so a second try finds a free place. */
129155 async function undo<E>(forkDir: string, failure: E): Promise<E> {
130156 await rm(forkDir, { recursive: true, force: true });
test/fork-lender-keeps-objects.test.ts +41 −2
@@ -1,10 +1,10 @@
11 /** A repository that lends objects to a fork never prunes what only the fork reaches. */
22 import { expect, test } from "bun:test";
3 import { readdirSync, utimesSync } from "node:fs";
3 import { readdirSync, rmSync, utimesSync, writeFileSync } from "node:fs";
44 import { mkdtemp, rm } from "node:fs/promises";
55 import { tmpdir } from "node:os";
66 import { join } from "node:path";
7 import { forkBareRepo, initBareRepo, keepLentObjects } from "../src/git/repos.ts";
7 import { forkBareRepo, initBareRepo, KEEP_RETRY, keepLentObjects } from "../src/git/repos.ts";
88
99 const git = async (cwd: string, ...args: string[]): Promise<{ code: number; out: string }> => {
1010 const p = Bun.spawn(["git", ...args], {
@@ -89,6 +89,45 @@ test("a parent forked before the setting existed is covered by keepLentObjects,
8989 }
9090 });
9191
92 test("a lender already kept is not written again: its config locked by another writer is no failure", async () => {
93 const root = await mkdtemp(join(tmpdir(), "lender-"));
94 try {
95 const { parent, fork } = await setup(root);
96 writeFileSync(join(parent, "config.lock"), "");
97 expect((await keepLentObjects(fork)).ok).toBe(true);
98 rmSync(join(parent, "config.lock"));
99 await git(parent, "config", "--unset", "gc.pruneExpire");
100 // Held for good: fails, and only after the whole backoff (20+40+60+80 ms), not at once or
101 // never.
102 writeFileSync(join(parent, "config.lock"), "");
103 const t0 = performance.now();
104 expect((await keepLentObjects(fork)).ok).toBe(false);

Wall-clock lower and upper bounds plus a real 50 ms timer make this test timing-sensitive. Inject the sleep or shrink KEEP_RETRY for the test so it stays fast and never flakes.

105 const took = performance.now() - t0;
106 const waited = (KEEP_RETRY.backoffMs * KEEP_RETRY.tries * (KEEP_RETRY.tries - 1)) / 2;
107 expect(took).toBeGreaterThanOrEqual(waited - 5);
108 expect(took).toBeLessThan(waited + 3000);
109 // A lock let go in milliseconds is waited out.
110 setTimeout(() => rmSync(join(parent, "config.lock")), 50); // real-timer: a writer's lock held for 50 ms
111 expect((await keepLentObjects(fork)).ok).toBe(true);
112 expect((await git(parent, "config", "--get", "gc.pruneExpire")).out).toBe("never");
113 } finally {
114 await rm(root, { recursive: true, force: true });
115 }
116 });
117
118 test("eight forks of one parent made at once all succeed", async () => {
119 const root = await mkdtemp(join(tmpdir(), "lender-"));
120 try {
121 const { parent } = await setup(root);
122 const made = await Promise.all(
123 Array.from({ length: 8 }, (_, i) => forkBareRepo(parent, join(root, `f${i}.git`))),
124 );
125 expect(made.map((m) => m.ok)).toEqual(Array.from({ length: 8 }, () => true));
126 } finally {
127 await rm(root, { recursive: true, force: true });
128 }
129 });
130
92131 test("a repository that borrows nothing is left alone", async () => {
93132 const root = await mkdtemp(join(tmpdir(), "lender-"));
94133 try {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs