Public: anyone can read this; the crew can change it.
A cross-repo pull's head_oid row lags its ref while carryApproval runs, so a concurrent refresh reads the old head #12871
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12870-a-cross-repo-pull-s-head-oid-row-lags-it → main 1 commit fork
2 files
changed +50
−6
src/core/following.ts +7 −5
| @@ -476,16 +476,18 @@ export async function refreshPullHead( | ||
| 476 | 476 | : await fetchInto(baseDir, headDir, `refs/heads/${pull.headBranch}`); |
| 477 | 477 | if (tip === undefined) return undefined; |
| 478 | 478 | await writeRef(snapshotDir, `refs/pull/${pull.number}/head`, tip); |
| 479 | // The row says where the head is too, so a reader of the pull — the API, | |
| 480 | // a page — sees the same tip the ref holds. Written with no await after | |
| 481 | // the ref moves: a concurrent refresh sees the ref current and returns | |
| 482 | // nothing, and its caller reads this row (#12870). | |
| 483 | forge.db | |
| 484 | .query("UPDATE pull SET head_oid = ?2 WHERE issue_id = ?1") | |
| 485 | .run(pull.id, tip); | |
| 479 | 486 | // A rebase onto a moved base changes no line: its approval stands. |
| 480 | 487 | if (!apart) { |
| 481 | 488 | const baseNow = await resolveOid(baseDir, `refs/heads/${pull.baseBranch}`); |
| 482 | 489 | if (baseNow !== undefined) await carryApproval(forge.db, baseDir, pull, baseNow, tip); |
| 483 | 490 | } |
| 484 | // The row says where the head is too, so a reader of the pull — the API, | |
| 485 | // a page — sees the same tip the ref holds. | |
| 486 | forge.db | |
| 487 | .query("UPDATE pull SET head_oid = ?2 WHERE issue_id = ?1") | |
| 488 | .run(pull.id, tip); | |
| 489 | 491 | // A kept fork's pull is combined with main on our side (#9012). |
| 490 | 492 | if (!apart && fromKeptFork(forge.db, pull)) { |
| 491 | 493 | const baseTip = await resolveOid(baseDir, `refs/heads/${pull.baseBranch}`); |
test/pull-head-refresh.test.ts +43 −1
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | * an unmoved head spawns no git process, loose or packed, a moved one is |
| 4 | 4 | * fetched, and a vanished branch leaves the last snapshot in place. |
| 5 | 5 | */ |
| 6 | import { afterAll, beforeAll, expect, spyOn, test } from "bun:test"; | |
| 6 | import { afterAll, beforeAll, expect, mock, spyOn, test } from "bun:test"; | |
| 7 | 7 | import { mkdtemp, rm } from "node:fs/promises"; |
| 8 | 8 | import { tmpdir } from "node:os"; |
| 9 | 9 | import { join } from "node:path"; |
| @@ -11,6 +11,7 @@ import { setRole } from "../src/actions/roles.ts"; | ||
| 11 | 11 | import { openPull } from "../src/actions/pulls.ts"; |
| 12 | 12 | import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; |
| 13 | 13 | import { createApp, type App } from "../src/app.ts"; |
| 14 | import * as carryModule from "../src/core/carry_approval.ts"; | |
| 14 | 15 | import { refreshPullHead } from "../src/core/following.ts"; |
| 15 | 16 | import { getPull, type Pull } from "../src/core/pulls.ts"; |
| 16 | 17 | import { getRepo, type Repo } from "../src/core/repos.ts"; |
| @@ -19,6 +20,9 @@ import { runGit } from "../src/git/env.ts"; | ||
| 19 | 20 | import { resolveOid, writeRef } from "../src/git/merge.ts"; |
| 20 | 21 | import { repoDir } from "../src/git/repos.ts"; |
| 21 | 22 | |
| 23 | /** The module as it was, copied before any mock replaces its live bindings. */ | |
| 24 | const realMod = { ...carryModule }; | |
| 25 | ||
| 22 | 26 | let dataDir: string; |
| 23 | 27 | let app: App; |
| 24 | 28 | let parent: Repo; |
| @@ -138,6 +142,44 @@ test("packed refs on both sides answer the same, still without a process", async | ||
| 138 | 142 | expect(await snapshot()).toBe(live); |
| 139 | 143 | }, 30_000); |
| 140 | 144 | |
| 145 | test("the row holds the new head while carryApproval is still awaited (#12870)", async () => { | |
| 146 | const before = (await snapshot())!; | |
| 147 | const next = await commitToBranch(app, ravi, fork, { | |
| 148 | message: "racy", | |
| 149 | branch: "side", | |
| 150 | files: [{ path: "racy.txt", content: "r\n" }], | |
| 151 | }); | |
| 152 | if (!next.ok) throw new Error(JSON.stringify(next.error)); | |
| 153 | const live = (await resolveOid(forkDir(), "refs/heads/side"))!; | |
| 154 | expect(await writeRef(baseDir(), `refs/pull/${number}/head`, before)).toBe(true); | |
| 155 | let reached!: () => void; | |
| 156 | const inCarry = new Promise<void>((r) => (reached = r)); | |
| 157 | let release!: () => void; | |
| 158 | const gate = new Promise<void>((r) => (release = r)); | |
| 159 | mock.module("../src/core/carry_approval.ts", () => ({ | |
| 160 | ...realMod, | |
| 161 | carryApproval: async (...args: Parameters<typeof realMod.carryApproval>) => { | |
| 162 | reached(); | |
| 163 | await gate; | |
| 164 | return realMod.carryApproval(...args); | |
| 165 | }, | |
| 166 | })); | |
| 167 | try { | |
| 168 | const first = refreshPullHead(app, parent, pull()); | |
| 169 | await inCarry; | |
| 170 | // The ref has moved; a second refresh sees nothing to do, and the row | |
| 171 | // its caller reads must already say the same. | |
| 172 | expect(await snapshot()).toBe(live); | |
| 173 | expect(await refreshPullHead(app, parent, pull())).toBeUndefined(); | |
| 174 | expect(pull().headOid).toBe(live); | |
| 175 | release(); | |
| 176 | expect(await first).toBe(live); | |
| 177 | } finally { | |
| 178 | release(); | |
| 179 | mock.module("../src/core/carry_approval.ts", () => ({ ...realMod })); | |
| 180 | } | |
| 181 | }, 30_000); | |
| 182 | ||
| 141 | 183 | test("a vanished branch leaves the last snapshot in place", async () => { |
| 142 | 184 | const kept = await snapshot(); |
| 143 | 185 | const gone = await runGit(["update-ref", "-d", "refs/heads/side"], { cwd: forkDir() }); |
new above
offline