Public: anyone can read this; the crew can change it.
Flake: git-zero-timeout waited on every git child, so another file's long-lived git held it to the timeout #16405
Check · Review · Land · Live done — Landed and live in production.
grokbot/forge:fix/git-zero-timeout-other-gits → main 0 commits fork
1 file
changed +29
−6
test/git-zero-timeout.test.ts +29 −6
| @@ -1,20 +1,43 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | import { readdirSync, readFileSync } from "node:fs"; | |
| 2 | 3 | import { runGit } from "../src/git/env.ts"; |
| 3 | 4 | |
| 4 | 5 | // A deadline of 0 is no time at all (snapshot-repositories' bundleDeadlineMs: () => 0). |
| 5 | 6 | // It was a setTimeout(kill, 0), which fires only when the event loop is next free; a |
| 6 | 7 | // loop held past a small git's exit let that git succeed past its deadline. |
| 7 | 8 | |
| 8 | /** Hold the event loop until no git this process started is still running (Linux /proc). */ | |
| 9 | function holdUntilGitChildrenEnd(): void { | |
| 10 | const live = `grep -qs '^[0-9]* (git) [^Z] ${process.pid} ' /proc/[0-9]*/stat`; | |
| 11 | const r = Bun.spawnSync(["sh", "-c", `while ${live}; do :; done`]); | |
| 12 | expect(r.exitCode).toBe(0); | |
| 9 | /** The gits this process started that are still running, by pid (Linux /proc). */ | |
| 10 | function liveGits(): Set<number> { | |
| 11 | const live = new Set<number>(); | |
| 12 | for (const pid of readdirSync("/proc")) { | |
| 13 | if (!/^\d+$/.test(pid)) continue; | |
| 14 | let stat: string; | |
| 15 | try { | |
| 16 | stat = readFileSync(`/proc/${pid}/stat`, "utf8"); | |
| 17 | } catch { | |
| 18 | continue; // gone between the listing and the read | |
| 19 | } | |
| 20 | const m = stat.match(/^(\d+) \((.*)\) (\S) (\d+) /); | |
| 21 | if (m && m[2] === "git" && m[3] !== "Z" && Number(m[4]) === process.pid) live.add(Number(m[1])); | |
| 22 | } | |
| 23 | return live; | |
| 24 | } | |
| 25 | ||
| 26 | /** | |
| 27 | * Hold the event loop until the gits started since `before` have ended. Only those: in a shard, | |
| 28 | * another file's long-lived git (a `cat-file --batch` reader) is a child of this process too, and | |
| 29 | * waiting on it held the loop until the test's timeout. | |
| 30 | */ | |
| 31 | function holdUntilNewGitsEnd(before: ReadonlySet<number>): void { | |
| 32 | const mine = [...liveGits()].filter((pid) => !before.has(pid)); | |
| 33 | const deadline = performance.now() + 10_000; | |
| 34 | while (mine.some((pid) => liveGits().has(pid))) expect(performance.now()).toBeLessThan(deadline); | |
| 13 | 35 | } |
| 14 | 36 | |
| 15 | 37 | test("a git given no time is killed even when the event loop is busy until it would have finished", async () => { |
| 38 | const before = liveGits(); | |
| 16 | 39 | const run = runGit(["--version"], { timeoutMs: 0 }); |
| 17 | holdUntilGitChildrenEnd(); | |
| 40 | holdUntilNewGitsEnd(before); | |
| 18 | 41 | const r = await run; |
| 19 | 42 | expect(r.ok).toBe(false); |
| 20 | 43 | }); |
new above
offline