Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
bump: patch
---

### Fixed

- pr-merge.yml's full-run dispatch, in both jobs, now also sends again on Go's `net/http: TLS handshake timeout`, which fails before the request is written; a rate limit still stops the step red with no further send. The re-run step's `rerun-failed-jobs` POST and the full-run wait's send to post-merge-demotion-alert.yml take the same retry: at most three attempts, sent again after a capped full-jitter wait (up to 10 s, then 20 s) only when gh's error shows the request never left. A rate-limited alert send now stops red, sent once. The warning after a dispatch that never started a run now says a scheduled pr-merge run starts one only for a lane merge, and any other merge waits for the next push or pr-require-checks' own schedule.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
bump: none
---

Test-only: two babysit tests quote gh's real rate-limit stderr in place of a hand-written string; no shipped behavior changes.
50 changes: 44 additions & 6 deletions .github/scripts/pr-merge/merge-job.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,10 +1193,17 @@ test("the early check names only a lane-dispatched run awaiting its re-run", ()
// Runs the re-run step against a fake gh that answers `attempts` in turn for
// the run read, failing a read whose entry is "x" (a 502) and rate-limiting
// one whose entry is "limited" (a 403 with the core pool spent until
// 2100-01-01). `post` answers the re-run POST: "ok", "refuse" (a 403 that is
// no rate limit) or "limited" (a 429 with a retry-after). Like gh, with -i it
// 2100-01-01). `post` answers the re-run POST, or each POST in turn when a
// list: "ok", "refuse" (a 403 that is no rate limit), "limited" (a 429 with a
// retry-after) or "unsent" (a TLS handshake timeout, before the request left).
// The "limited" read's stderr is RATE_LIMITED_STDERR. Like gh, with -i it
// prints the status and headers before the body, the guard's shape.
const LIMITED_403 = 'HTTP/2.0 403 Forbidden\\r\\nX-Ratelimit-Remaining: 0\\r\\nX-Ratelimit-Reset: 4102444800\\r\\nX-Ratelimit-Resource: core\\r\\n\\r\\n{"message": "API rate limit exceeded for installation ID 1."}\\n';
// gh's stderr for a call GitHub refused for a rate limit, as run 39906176584
// logged it (pr-merge.yml, dispatch-full-run, "Start the full CI run on main",
// 2026-10-23T05:30:57Z).
const RATE_LIMITED_STDERR =
"gh: API rate limit exceeded for installation. If you reach out to GitHub Support for help, please include the request ID 8000:1DF2D:1EE39F:657D15:6ADAF110 and timestamp 2026-10-23 05:30:57 UTC. For more on scraping GitHub and how it may affect your rights, please review our Terms of Service (https://docs.github.com/en/site-policy/github-terms/github-terms-of-service) (HTTP 403)";
const LIMITED_403 ='HTTP/2.0 403 Forbidden\\r\\nX-Ratelimit-Remaining: 0\\r\\nX-Ratelimit-Reset: 4102444800\\r\\nX-Ratelimit-Resource: core\\r\\n\\r\\n{"message": "API rate limit exceeded for installation ID 1."}\\n';
const runRerun = (attempts, { refuse = false, post = refuse ? "refuse" : "ok", runId = "11" } = {}) => {
const dir = mkdtempSync(join(tmpdir(), "pr-merge-rerun-"));
try {
Expand All @@ -1207,19 +1214,21 @@ const runRerun = (attempts, { refuse = false, post = refuse ? "refuse" : "ok", r
`#!/usr/bin/env bash\ninc=; if [ "$2" = -i ]; then inc=1; set -- "$1" "\${@:3}"; fi\n` +
`echo "$*" >>"${dir}/calls"\n` +
`say() { if [ -n "$inc" ]; then printf "$1"; else printf "$1" | sed '1,/^\\r\\?$/d'; fi; }\n` +
`if [ "$3" = POST ]; then case ${post} in\n` +
`if [ "$3" = POST ]; then p=(${[post].flat().join(" ")}); s=$(($(cat "${dir}/posts" 2>/dev/null || echo 0) + 1)); echo "$s" >"${dir}/posts"\n` +
`case "\${p[$((s - 1))]:-\${p[-1]}}" in\n` +
` ok) say 'HTTP/2.0 204 No Content\\r\\n\\r\\n'; exit 0 ;;\n` +
` unsent) echo 'Post "https://api.github.com/repos/o/r/actions/runs/11/rerun-failed-jobs": net/http: TLS handshake timeout' >&2; exit 1 ;;\n` +
` refuse) say 'HTTP/2.0 403 Forbidden\\r\\n\\r\\n{"message": "Resource not accessible by integration"}\\n'; echo "HTTP 403" >&2; exit 1 ;;\n` +
` limited) say 'HTTP/2.0 429 Too Many Requests\\r\\nRetry-After: 60\\r\\n\\r\\n{"message": "Too many requests"}\\n'; echo "HTTP 429" >&2; exit 1 ;;\n` +
`esac; fi\n` +
`n=$(($(cat "${dir}/reads" 2>/dev/null || echo 0) + 1))\necho "$n" >"${dir}/reads"\n` +
`a=(${attempts.join(" ")})\nv="\${a[$((n - 1))]:-\${a[-1]}}"\n` +
`if [ "$v" = x ]; then say 'HTTP/2.0 502 Bad Gateway\\r\\n\\r\\n{}\\n'; echo "HTTP 502" >&2; exit 1; fi\n` +
`if [ "$v" = limited ]; then say '${LIMITED_403}'; echo "gh: API rate limit exceeded for installation ID 1. (HTTP 403)" >&2; exit 1; fi\n` +
`if [ "$v" = limited ]; then say '${LIMITED_403}'; echo '${RATE_LIMITED_STDERR}' >&2; exit 1; fi\n` +
`if [ "$v" = bare429 ]; then echo "gh: Too Many Requests (HTTP 429)" >&2; exit 1; fi\n` +
`say "HTTP/2.0 200 OK\\r\\n\\r\\n$v\\n"\n`,
);
writeFileSync(join(bin, "sleep"), "#!/usr/bin/env bash\n");
writeFileSync(join(bin, "sleep"), `#!/usr/bin/env bash\necho "$1" >>"${dir}/slept"\n`);
chmodSync(join(bin, "gh"), 0o755);
chmodSync(join(bin, "sleep"), 0o755);
symlinkSync(root, join(dir, ".base"));
Expand All @@ -1244,6 +1253,7 @@ const runRerun = (attempts, { refuse = false, post = refuse ? "refuse" : "ok", r
calls: read("calls").split("\n").filter(Boolean),
output: read("output"),
record: read("rerun.json"),
slept: read("slept").split("\n").filter(Boolean).map(Number),
};
} finally {
rmSync(dir, { recursive: true, force: true });
Expand Down Expand Up @@ -1287,6 +1297,34 @@ test("the re-run step records nothing when the re-run is refused or lands past a
}
});

// The owner outage rule (Q677: at most three attempts, a capped full-jitter
// backoff), as the dispatch: a POST that never left is sent again; a refusal
// GitHub answered is not, since the re-run may have started.
test("a re-run POST that never left is sent again after a jittered wait, at most three times", () => {
const run = runRerun([1, 2], { post: ["unsent", "unsent", "ok"] });
assert.equal(run.status, 0, run.stdout);
assert.deepEqual(run.calls.filter((c) => c.includes("POST")), [POST, POST, POST]);
assert.ok(run.slept[0] >= 1 && run.slept[0] <= 10, `${run.slept[0]}`);
assert.ok(run.slept[1] >= 1 && run.slept[1] <= 20, `${run.slept[1]}`);
assert.match(run.output, /^recorded=true$/m);
const spent = runRerun([1], { post: ["unsent"] });
assert.equal(spent.status, 0, spent.stdout);
assert.equal(spent.calls.filter((c) => c.includes("POST")).length, 3);
assert.equal(spent.record, "");
assert.match(spent.stdout, /::warning::the re-run of run 11 was refused after 3 attempt\(s\)/);
const refused = runRerun([1], { post: ["refuse", "ok"] });
assert.deepEqual(refused.calls.filter((c) => c.includes("POST")), [POST]);
assert.deepEqual(refused.slept, []);
});

test("a rate limit after a re-run POST that never left still stops red, with no further send", () => {
const run = runRerun([1], { post: ["unsent", "limited", "ok"] });
assert.equal(run.status, 1, run.stdout);
assert.deepEqual(run.calls.filter((c) => c.includes("POST")), [POST, POST]);
assert.equal(run.record, "");
assert.match(run.output, /^rate-limited=true$/m);
});

test("the re-run step rides out failed reads after the re-run and records attempt 2", () => {
const run = runRerun([1, "x", "x", "x", 2]);
assert.equal(run.status, 0, run.stdout);
Expand Down
87 changes: 84 additions & 3 deletions .github/scripts/pr-merge/workflow.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,15 @@ const SEND = `api -X POST repos/o/r/actions/workflows/post-merge-demotion-alert.
// With notModified N, the first read finds the run in progress with ETag
// "e1", the next N reads (sent with If-None-Match on it) are answered 304
// with gh's exit 1, and the one after finds it completed. With limited, every
// read is refused with limitedMessage.
function runWait(conclusion, { dispatchFails = false, notModified = 0, limited = false, limitedMessage = "gh: API rate limit exceeded for installation ID 1. (HTTP 403)" } = {}) {
// read is refused with limitedMessage. Each entry of sendErrors fails one send
// in turn with that stderr; the send after them succeeds, unless
// dispatchFails.
// gh's stderr for a POST GitHub refused for a rate limit, as run 39906176584
// logged it (pr-merge.yml, dispatch-full-run, "Start the full CI run on main",
// 2026-10-23T05:30:57Z).
const RATE_LIMITED_STDERR =
"gh: API rate limit exceeded for installation. If you reach out to GitHub Support for help, please include the request ID 8000:1DF2D:1EE39F:657D15:6ADAF110 and timestamp 2026-10-23 05:30:57 UTC. For more on scraping GitHub and how it may affect your rights, please review our Terms of Service (https://docs.github.com/en/site-policy/github-terms/github-terms-of-service) (HTTP 403)";
function runWait(conclusion, { dispatchFails = false, sendErrors = [], notModified = 0, limited = false, limitedMessage = RATE_LIMITED_STDERR } = {}) {
const step = job("dispatch-full-run").split("\n - name: Wait for the full run to finish\n")[1];
const body = /\n {8}run: \|\n((?: {10}.*\n|\s*\n)+)/
.exec(step)[1]
Expand All @@ -76,14 +83,18 @@ function runWait(conclusion, { dispatchFails = false, notModified = 0, limited =
mode: 0o755,
});
writeFileSync(join(dir, "n304"), String(notModified));
sendErrors.forEach((stderr, i) => writeFileSync(join(dir, `send-stderr-${i + 1}`), stderr));
const done = `{"status": "completed", "conclusion": "${conclusion}", "head_sha": "${HEAD}"}`;
const first = notModified > 0 ? `{"status": "in_progress", "conclusion": null, "head_sha": "${HEAD}"}` : done;
writeFileSync(
join(dir, "bin", "gh"),
`#!/usr/bin/env bash
printf '%s\\n' "$*" >>"${dir}/log"
case "$*" in
*-X\\ POST*) exit ${dispatchFails ? 1 : 0} ;;
*-X\\ POST*)
s=$(($(cat "${dir}/sends" 2>/dev/null || echo 0) + 1)); echo "$s" >"${dir}/sends"
if [ -f "${dir}/send-stderr-$s" ]; then cat "${dir}/send-stderr-$s" >&2; exit 1; fi
exit ${dispatchFails ? 1 : 0} ;;
${limited ? `*) echo '${limitedMessage}' >&2; exit 1 ;;` : ""}
*'-H If-None-Match: "e1"')
n=$(cat "${dir}/n304")
Expand Down Expand Up @@ -187,6 +198,38 @@ test("a send that fails fails the job", () => {
assert.match(out, /::error::could not send run 4242 to post-merge-demotion-alert\.yml/);
});

// The owner outage rule (Q677: at most three attempts, a capped full-jitter
// backoff), sent again only when the request never left, as the dispatch.
// The 180 s entries are the wait's own polls; the alert's waits come after.
test("an alert send that never left is sent again after a jittered wait, at most three times", () => {
const timeout = 'Post "https://api.github.com/repos/o/r/actions/workflows/post-merge-demotion-alert.yml/dispatches": net/http: TLS handshake timeout';
const sent = runWait("failure", { sendErrors: [timeout, timeout] });
assert.equal(sent.status, 0, sent.out);
assert.deepEqual(sent.writes, [SEND, SEND, SEND]);
const waits = sent.sleeps.slice(1).map(Number);
assert.equal(waits.length, 2);
assert.ok(waits[0] >= 1 && waits[0] <= 10, `${waits[0]}`);
assert.ok(waits[1] >= 1 && waits[1] <= 20, `${waits[1]}`);
const spent = runWait("failure", { sendErrors: [timeout, timeout, timeout] });
assert.equal(spent.status, 1, spent.out);
assert.equal(spent.writes.length, 3);
assert.match(spent.out, /::error::could not send run 4242 to post-merge-demotion-alert\.yml after 3 attempt\(s\)/);
// A send that may have reached GitHub is not sent again.
const answered = runWait("failure", { sendErrors: ["gh: Server Error (HTTP 502)", ""] });
assert.equal(answered.status, 1);
assert.deepEqual(answered.writes, [SEND]);
});

test("a rate-limited alert send is sent once and stops red", () => {
for (const stderr of [RATE_LIMITED_STDERR, "gh: Too Many Requests (HTTP 429)"]) {
const { status, out, writes, sleeps } = runWait("failure", { sendErrors: [stderr] });
assert.equal(status, 1, out);
assert.deepEqual(writes, [SEND]);
assert.deepEqual(sleeps, ["180"]);
assert.match(out, /::error title=GitHub rate limit::the send of run 4242 to post-merge-demotion-alert\.yml was rate limited/);
}
});

test("the dispatch job's token is actions: write and nothing else", () => {
const block = /\n {4}permissions:\n((?: {6}[a-z-]+: [a-z]+.*\n)+)/.exec(job("dispatch-full-run"))[1];
assert.deepEqual(
Expand Down Expand Up @@ -624,6 +667,44 @@ test("a request that never left is sent again after a jittered wait inside its b
assert.equal(runDispatch([unsent[2], ok(4247)]).posts, 2);
});

// Go's net/http error when the TLS handshake outlasts its timeout, before any
// request byte is written, so a resend cannot start a second run.
const handshakeTimeout = { stderr: `Post "${DISPATCH_URL}": net/http: TLS handshake timeout` };

test("a TLS handshake timeout is sent again in both jobs", () => {
for (const jobId of ["dispatch-full-run", "post-merge-dispatch"]) {
const r = runDispatch([handshakeTimeout, ok(4248)], { jobId, event: "schedule" });
assert.equal(r.status, 0, r.out);
assert.equal(r.posts, 2, jobId);
assert.equal(r.slept.length, 1, jobId);
assert.ok(r.slept[0] >= 1 && r.slept[0] <= 10, `${r.slept[0]}`);
assert.match(r.output, /^run-id=4248$/m, jobId);
}
});

// The resend never reaches past a rate limit: the attempt after the timeout
// is answered with one, and nothing is sent after it.
test("a rate limit after a TLS handshake timeout still stops red, with no further send", () => {
for (const jobId of ["dispatch-full-run", "post-merge-dispatch"]) {
const r = runDispatch([handshakeTimeout, limited({ "X-Ratelimit-Remaining": "0", "X-Ratelimit-Reset": `${NOW + 30}`, "X-Ratelimit-Resource": "core" }), ok(1)], { jobId, event: "schedule" });
assert.equal(r.status, 1, r.out);
assert.equal(r.posts, 2, jobId);
assert.equal(r.slept.length, 1, jobId);
assert.match(r.output, /^rate-limited=true$/m, jobId);
assert.doesNotMatch(r.output, /run-id=/, jobId);
}
});

// Only a lane merge is picked up by the scheduled post-merge check; any other
// merge waits for the next push or pr-require-checks.yml's own schedule.
test("the exhausted-retry warning names who starts the run next", () => {
assert.match(read("pr-require-checks.yml"), /\n {2}schedule:\n {4}- cron: /);
for (const jobId of ["dispatch-full-run", "post-merge-dispatch"]) {
const r = runDispatch([refused()], { jobId, event: "schedule" });
assert.match(r.out, /::warning::no full CI run started .*; a scheduled pr-merge run starts one only for a lane merge, and any other merge waits for the next push or pr-require-checks' own schedule$/m, jobId);
}
});

// A reset or a timeout on an open connection may come after GitHub took the
// request, so the run may exist: stop as on exhaustion.
test("no response once the request may have been sent is not sent again", () => {
Expand Down
Loading
Loading