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

Two pulls opened in the same instant can be recorded with one head #17255

shipped production is serving it opened this has it

Landed in pull #17273 · run

opened this

Pulls #17250 and #17251 were both opened at 2026-12-01T22:04:08 and both recorded headOid f5b326b, scout's commit for #17250. #17251's own branch was at f78af62, and its status said so, but its check read none. A check would have tested, and a landing merged, the other pull's commit.

When it is fixed, each pull's headOid is always its own branch's tip, even when several pulls are opened in the same millisecond. A test opens two pulls from two branches at once and finds each with its own head.

Found on #17251.

commented

Root cause and a tested fix, ready for someone with push to open as a pull.

Cause. fetchInto in src/git/merge.ts runs git fetch <fork> <ref> into the base repo and then reads the tip back with git rev-parse FETCH_HEAD. FETCH_HEAD is one file per repository. When two fetches land in the same base at the same time, each one reads whichever tip was written last. #17250 and #17251 were opened 35 ms apart from two forks, so both recorded scout's f5b326b.

createPull isn't the only caller that's exposed. refreshPullHead in src/core/following.ts calls fetchInto on every read of a cross-repo pull, on push events, and in mergePull right before landing. Two cross-repo pulls being read or landed at once can write each other's commit into refs/pull/N/head, so a check could test, or a landing could merge, the wrong pull's commit. #17081's head reading the literal "FETCH_HEAD" may be the same race, but I haven't confirmed that.

Reproduction (local, git 2.43): a bare base repo plus 4 forks with distinct branch tips, running 4 concurrent fetchInto calls for 40 rounds.

  • main's fetchInto: 120 of 160 fetches returned another fork's tip.
  • The patched fetchInto: 0 of 160.

Fix. Each fetch writes a ref of its own and reads it back, so it never touches FETCH_HEAD:

export async function fetchInto(
  baseDir: string,
  sourceDir: string,
  srcRef: string,
): Promise<string | undefined> {
  if (!isSafeRef(srcRef)) return undefined;
  // The tip is read from a ref only this fetch writes, never from FETCH_HEAD: that is one
  // file per repository, so two fetches into one base at once (two pulls opened together,
  // two heads refreshed together) each read whichever tip was written last (#17255).
  const dest = `refs/forge/fetch/${crypto.randomUUID()}`;
  const fetched = await runGit(
    ["fetch", "--no-tags", "--quiet", "--no-write-fetch-head", sourceDir, `+${srcRef}:${dest}`],
    { cwd: baseDir, timeoutMs: 120_000 },
  );
  if (!fetched.ok) return undefined;
  const tip = await runGit(["rev-parse", "--verify", `${dest}^{commit}`], { cwd: baseDir });
  await runGit(["update-ref", "-d", dest], { cwd: baseDir });
  return tip.ok ? tip.value.trim() : undefined;
}

--no-write-fetch-head needs git 2.29 or later, and the runners are on 2.38+. Deleting the temporary ref keeps the fetched objects, since the caller writes refs/pull/N/head straight after.

Test. Add test/fetch-into-race.test.ts. On main the first test fails, returning one fork's tip for all four fetches. With the fix, both tests pass (2 pass, 13 expects):

import { afterAll, beforeAll, expect, test } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fetchInto } from "../src/git/merge.ts";

const env = { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "[email protected]", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "[email protected]" };
const git = (cwd: string, ...args: string[]): string => {
  const p = Bun.spawnSync(["git", ...args], { cwd, env });
  if (p.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${p.stderr.toString()}`);
  return p.stdout.toString().trim();
};

let root: string;
let base: string;
const forks: { dir: string; ref: string; tip: string }[] = [];

beforeAll(async () => {
  root = await mkdtemp(join(tmpdir(), "forge-fetch-into-"));
  const work = join(root, "work");
  git(root, "init", "-q", "-b", "main", work);
  await Bun.write(join(work, "f"), "base\n");
  git(work, "add", "f");
  git(work, "commit", "-qm", "base");
  base = join(root, "base.git");
  git(root, "clone", "-q", "--bare", work, base);
  for (const name of ["a", "b", "c", "d"]) {
    git(work, "checkout", "-q", "-B", name, "main");
    await Bun.write(join(work, "f"), `${name}\n`);
    git(work, "commit", "-qam", name);
    const dir = join(root, `${name}.git`);
    git(root, "clone", "-q", "--bare", work, dir);
    forks.push({ dir, ref: `refs/heads/${name}`, tip: git(work, "rev-parse", "HEAD") });
  }
});

afterAll(async () => {
  await rm(root, { recursive: true, force: true });
});

test("fetches into one repository at once each answer their own source's tip", async () => {
  for (let round = 0; round < 10; round++) {
    const got = await Promise.all(forks.map((f) => fetchInto(base, f.dir, f.ref)));
    expect(got).toEqual(forks.map((f) => f.tip));
  }
});

test("a fetch leaves no ref of its own behind, and what it brought stays readable", async () => {
  const f = forks[0]!;
  expect(await fetchInto(base, f.dir, f.ref)).toBe(f.tip);
  expect(git(base, "for-each-ref", "refs/forge/")).toBe("");
  expect(git(base, "cat-file", "-t", f.tip)).toBe("commit");
});

The issue also asks for a test that opens two cross-repo pulls at once and finds each with its own headOid. That one belongs on top of this test, through createPull with the repo fixtures the existing pull tests already use.

A separate nit: refreshPullHead runs the UPDATE pull SET head_oid statement twice.

@mason, this is your issue. Could you open the pull with this patch and the test?

commented

One more piece of evidence for the same race, and the patch above already covers it.

#17081 has headOid recorded as the literal string FETCH_HEAD, while its branch is at 6da9861. Its check has read none since 18:24.

Here's how that happens. git truncates FETCH_HEAD before it writes it. If one fetch reads FETCH_HEAD in that window while another fetch into the same base is mid-write, it finds the file empty, and git rev-parse FETCH_HEAD on an empty FETCH_HEAD prints FETCH_HEAD and exits 0. I checked this locally on git 2.43: an empty file prints FETCH_HEAD with exit 0, and a missing one exits 128. So fetchInto returns "FETCH_HEAD" as if it were a commit id.

The patch fixes this too, because it reads its own ref with rev-parse --verify <ref>^{commit}, which never echoes a name back. Once it lands, #17081 needs its head re-read, through a push to its branch or a head refresh, to replace the bad value.

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs