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

Merged opened this

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

Merged

scout/forge:scout/12870-a-cross-repo-pull-s-head-oid-row-lags-it → main 1 commit fork

  • Merged. scout/12870-a-cross-repo-pull-s-head-oid-row-lags-it was merged into main as 8dc19f4.

Review

Sign in to review this change.
2 files changed +50 −6
src/core/following.ts +7 −5
@@ -476,16 +476,18 @@ export async function refreshPullHead(
476476 : await fetchInto(baseDir, headDir, `refs/heads/${pull.headBranch}`);
477477 if (tip === undefined) return undefined;
478478 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);
479486 // A rebase onto a moved base changes no line: its approval stands.
480487 if (!apart) {
481488 const baseNow = await resolveOid(baseDir, `refs/heads/${pull.baseBranch}`);
482489 if (baseNow !== undefined) await carryApproval(forge.db, baseDir, pull, baseNow, tip);
483490 }
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);
489491 // A kept fork's pull is combined with main on our side (#9012).
490492 if (!apart && fromKeptFork(forge.db, pull)) {
491493 const baseTip = await resolveOid(baseDir, `refs/heads/${pull.baseBranch}`);
test/pull-head-refresh.test.ts +43 −1
@@ -3,7 +3,7 @@
33 * an unmoved head spawns no git process, loose or packed, a moved one is
44 * fetched, and a vanished branch leaves the last snapshot in place.
55 */
6 import { afterAll, beforeAll, expect, spyOn, test } from "bun:test";
6 import { afterAll, beforeAll, expect, mock, spyOn, test } from "bun:test";
77 import { mkdtemp, rm } from "node:fs/promises";
88 import { tmpdir } from "node:os";
99 import { join } from "node:path";
@@ -11,6 +11,7 @@ import { setRole } from "../src/actions/roles.ts";
1111 import { openPull } from "../src/actions/pulls.ts";
1212 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
1313 import { createApp, type App } from "../src/app.ts";
14 import * as carryModule from "../src/core/carry_approval.ts";
1415 import { refreshPullHead } from "../src/core/following.ts";
1516 import { getPull, type Pull } from "../src/core/pulls.ts";
1617 import { getRepo, type Repo } from "../src/core/repos.ts";
@@ -19,6 +20,9 @@ import { runGit } from "../src/git/env.ts";
1920 import { resolveOid, writeRef } from "../src/git/merge.ts";
2021 import { repoDir } from "../src/git/repos.ts";
2122
23 /** The module as it was, copied before any mock replaces its live bindings. */
24 const realMod = { ...carryModule };
25
2226 let dataDir: string;
2327 let app: App;
2428 let parent: Repo;
@@ -138,6 +142,44 @@ test("packed refs on both sides answer the same, still without a process", async
138142 expect(await snapshot()).toBe(live);
139143 }, 30_000);
140144
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
141183 test("a vanished branch leaves the last snapshot in place", async () => {
142184 const kept = await snapshot();
143185 const gone = await runGit(["update-ref", "-d", "refs/heads/side"], { cwd: forkDir() });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs