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
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14684-two-forks-of-one-parent-made-at-once-bot → main 1 commit fork 1 thread · 1 unresolved
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 | ||
| 119 | 119 | const path = line.trim(); |
| 120 | 120 | if (path === "" || path.startsWith("#")) continue; |
| 121 | 121 | 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; | |
| 124 | 128 | } |
| 125 | 129 | return ok(); |
| 126 | 130 | } |
| 127 | 131 | |
| 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 | ||
| 128 | 154 | /** A fork half made is no fork: remove it, so a second try finds a free place. */ |
| 129 | 155 | async function undo<E>(forkDir: string, failure: E): Promise<E> { |
| 130 | 156 | await rm(forkDir, { recursive: true, force: true }); |
test/fork-lender-keeps-objects.test.ts +41 −2
| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | /** A repository that lends objects to a fork never prunes what only the fork reaches. */ |
| 2 | 2 | import { expect, test } from "bun:test"; |
| 3 | import { readdirSync, utimesSync } from "node:fs"; | |
| 3 | import { readdirSync, rmSync, utimesSync, writeFileSync } from "node:fs"; | |
| 4 | 4 | import { mkdtemp, rm } from "node:fs/promises"; |
| 5 | 5 | import { tmpdir } from "node:os"; |
| 6 | 6 | 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"; | |
| 8 | 8 | |
| 9 | 9 | const git = async (cwd: string, ...args: string[]): Promise<{ code: number; out: string }> => { |
| 10 | 10 | const p = Bun.spawn(["git", ...args], { |
| @@ -89,6 +89,45 @@ test("a parent forked before the setting existed is covered by keepLentObjects, | ||
| 89 | 89 | } |
| 90 | 90 | }); |
| 91 | 91 | |
| 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); | |
| 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 | ||
| 92 | 131 | test("a repository that borrows nothing is left alone", async () => { |
| 93 | 132 | const root = await mkdtemp(join(tmpdir(), "lender-")); |
| 94 | 133 | try { |
new above
offline
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.