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
8 changes: 8 additions & 0 deletions .changes/pr-pipeline/fix-pr-merge-dispatch-slack-6a1f3c92.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
bump: patch
---

### Fixed

- pr-merge.yml's `post-merge-dispatch` job caps its dispatch waits at 180 s instead of 300 s. Its whole run took 97-127 s on recent scheduled runs, and the re-run step after the dispatch sleeps up to 170 s while it reads back attempt 2. The lower cap leaves about 6.5 of the job's 15 minutes free, so a timeout no longer lands after the re-run POST and leaves attempt 2 unrecorded.
- Both dispatch steps send the dispatch again after no response only when gh's error shows the connection failed before the request was sent (a `dial tcp` error or a failed TLS handshake). A reset or timeout on an open connection may follow GitHub creating the run, so the step now warns and stops, as it does on exhaustion.
55 changes: 49 additions & 6 deletions .github/scripts/pr-merge/workflow.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ test("pr-require-checks.yml never cancels a dispatched or scheduled run", () =>
});

// The dispatch POST's retry, run under bash against a stub gh that answers
// each POST in turn from `answers` ({status, headers, body}, or null for no
// response at all), a stub sleep that logs its seconds and a stub date. The
// each POST in turn from `answers` ({status, headers, body}, or {stderr} for
// no response at all, with gh's error text), a stub sleep that logs its seconds and a stub date. The
// expected waits come from the owner outage rule (at most three attempts;
// retry-after, else x-ratelimit-reset for a primary limit, else at least
// 60 s for a secondary one; otherwise a capped full-jitter backoff) and
Expand Down Expand Up @@ -179,20 +179,22 @@ function runDispatch(answers, { jobId = "dispatch-full-run", cap, event = "push"
mkdirSync(join(dir, "bin"));
answers.forEach((a, i) => {
const text =
a === null
a.status === undefined
? ""
: `HTTP/2.0 ${a.status}\n${Object.entries(a.headers)
.map(([k, v]) => `${k}: ${v}\r\n`)
.join("")}\r\n${a.body}`;
writeFileSync(join(dir, `answer-${i + 1}`), text);
writeFileSync(join(dir, `code-${i + 1}`), a !== null && a.status.startsWith("200") ? "0" : "1");
writeFileSync(join(dir, `stderr-${i + 1}`), a.stderr ?? "");
writeFileSync(join(dir, `code-${i + 1}`), a.status?.startsWith("200") ? "0" : "1");
});
writeFileSync(
join(dir, "bin", "gh"),
`#!/usr/bin/env bash
n=$(($(cat "${dir}/calls" 2>/dev/null || echo 0) + 1)); echo "$n" >"${dir}/calls"
printf '%s\\n' "$*" >>"${dir}/log"
cat "${dir}/answer-$n" 2>/dev/null
cat "${dir}/stderr-$n" >&2 2>/dev/null
exit "$(cat "${dir}/code-$n" 2>/dev/null || echo 1)"
`,
{ mode: 0o755 },
Expand Down Expand Up @@ -307,14 +309,55 @@ test("a refusal that is no rate limit, and a 5xx, are sent once", () => {
}
});

test("no response at all is sent again after a jittered wait inside its bound", () => {
const r = runDispatch([null, null, ok(4246)]);
// gh's errors for a request that never left (Go net/http wording, as gh
// prints it): a DNS lookup, a refused connection, a failed TLS handshake.
const unsent = [
'Post "https://api.github.com/repos/o/r/actions/workflows/pr-require-checks.yml/dispatches": dial tcp: lookup api.github.com on 127.0.0.53:53: no such host',
'Post "https://api.github.com/repos/o/r/actions/workflows/pr-require-checks.yml/dispatches": dial tcp 140.82.112.6:443: connect: connection refused',
'Post "https://api.github.com/repos/o/r/actions/workflows/pr-require-checks.yml/dispatches": remote error: tls: handshake failure',
].map((stderr) => ({ stderr }));

test("a request that never left is sent again after a jittered wait inside its bound", () => {
const r = runDispatch([unsent[0], unsent[1], ok(4246)]);
assert.equal(r.status, 0, r.out);
assert.equal(r.posts, 3);
assert.equal(r.slept.length, 2);
assert.ok(r.slept[0] >= 1 && r.slept[0] <= 10, `${r.slept[0]}`);
assert.ok(r.slept[1] >= 1 && r.slept[1] <= 20, `${r.slept[1]}`);
assert.match(r.output, /^run-id=4246$/m);
assert.equal(runDispatch([unsent[2], ok(4247)]).posts, 2);
});

// 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", () => {
for (const stderr of [
'Post "https://api.github.com/repos/o/r/actions/workflows/pr-require-checks.yml/dispatches": read tcp 10.1.0.4:51234->140.82.112.6:443: read: connection reset by peer',
'Post "https://api.github.com/repos/o/r/actions/workflows/pr-require-checks.yml/dispatches": context deadline exceeded (Client.Timeout exceeded while awaiting headers)',
"",
]) {
const r = runDispatch([{ stderr }, ok(1)]);
assert.equal(r.status, 0, r.out);
assert.equal(r.posts, 1, stderr);
assert.deepEqual(r.slept, [], stderr);
assert.match(r.out, /::warning::no full CI run started .*the request may have been sent; not sent again/, stderr);
assert.doesNotMatch(r.output, /run-id=/);
}
});

// post-merge-dispatch's 15 minutes hold its measured steps, the re-run
// step's read-back after this one, and at most 180 s of waits here (the yaml
// comment holds the numbers): a 181 s wait is not taken, two 60 s ones are.
test("post-merge-dispatch takes at most 180 s of waits", () => {
const opts = { jobId: "post-merge-dispatch", event: "schedule" };
const over = runDispatch([limited({ "Retry-After": "181" }), ok(1)], opts);
assert.equal(over.posts, 1);
assert.deepEqual(over.slept, []);
assert.match(over.out, /a 181s wait does not fit the 180s this job allows/);
const secondary = limited({ "Retry-After": "60" });
const within = runDispatch([secondary, secondary, ok(4248)], opts);
assert.deepEqual(within.slept, [60, 60]);
assert.match(within.output, /^run-id=4248$/m);
});

test("a dispatch that returns no run id is not sent again and records nothing", () => {
Expand Down
44 changes: 32 additions & 12 deletions .github/workflows/pr-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,19 @@ jobs:
echo "lane-rerun=$rerun" >>"$GITHUB_OUTPUT"

# The same body as dispatch-full-run's step of this name; see there.
# WAIT_CAP: 15 minutes, less about 4 for the steps above, 3 for the
# re-run step's read-back and 1 for the uploads, leaves 7; 300 s of it.
# WAIT_CAP: of the 15 minutes, the whole job took 97-127 s on the last
# ten scheduled runs read on 2026-10-23 (the post-merge check 81-111 s
# of it). Add 180 s here, the re-run step's read-back sleeps (up to
# 170 s) and about 20 s of reads and uploads: about 8.5 minutes, which
# leaves 6.5 for slow reads. A timeout after the re-run POST would leave
# its attempt 2 unrecorded, so the cap gives way, not the timeout.
- name: Start the full CI run on main
id: dispatch
if: steps.post-merge.outputs.needs-dispatch == 'true'
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
WAIT_CAP: 300
WAIT_CAP: 180
run: |
set -euo pipefail
if [ "$GITHUB_EVENT_NAME" = push ]; then
Expand All @@ -215,7 +219,8 @@ jobs:
for try in 1 2 3; do
code=0
out=$(gh api -i -X POST "repos/$REPO/actions/workflows/pr-require-checks.yml/dispatches" \
-f ref=main -F return_run_details=true) || code=$?
-f ref=main -F return_run_details=true 2>"$RUNNER_TEMP/dispatch-stderr") || code=$?
cat "$RUNNER_TEMP/dispatch-stderr" >&2
hdr=$(sed '/^\r\?$/q' <<<"$out")
body=$(sed '1,/^\r\?$/d' <<<"$out")
status=$(awk 'NR == 1 { print $2 }' <<<"$hdr")
Expand All @@ -241,8 +246,13 @@ jobs:
wait=60
fi
elif [ -z "$status" ]; then
why="no response (gh exit $code)"
wait=$((RANDOM % (10 * 2 ** (try - 1)) + 1))
error=$(tail -n 1 "$RUNNER_TEMP/dispatch-stderr")
if grep -qE 'dial tcp|tls: (handshake|first record)' "$RUNNER_TEMP/dispatch-stderr"; then
why="the connection failed before the request was sent (gh exit $code: $error)"
wait=$((RANDOM % (10 * 2 ** (try - 1)) + 1))
else
why="no response (gh exit $code: ${error:-no error text}), and the request may have been sent; not sent again"
fi
else
why="HTTP $status, not sent again"
fi
Expand Down Expand Up @@ -1231,9 +1241,13 @@ jobs:
# At most three POSTs. A rate-limited 403 or 429 waits as GitHub
# documents: retry-after seconds, else until one second past
# x-ratelimit-reset while remaining is 0, else 60 s (a secondary
# limit); no response at all waits a full-jitter backoff (up to 10 s,
# then 20 s). Any other answer, a 5xx included, is not sent again: the
# run may exist, and a second run would have no record. The waits sum to
# limit). No response is sent again, after a full-jitter backoff (up to
# 10 s, then 20 s), only when gh's error shows the connection failed
# before the request went out: a `dial tcp` error (a DNS lookup, a
# refused or unreachable connection, a connect timeout) or a failed TLS
# handshake. Any other answer, a 5xx, a timeout or a reset on an open
# connection included, is not sent again: the run may exist, and a
# second run would have no record. The waits sum to
# at most WAIT_CAP seconds; one that does not fit is not taken. A
# dispatch that still fails sets no run id and ends green with a
# warning: nothing is recorded or waited on, and the next push, or the
Expand All @@ -1260,7 +1274,8 @@ jobs:
for try in 1 2 3; do
code=0
out=$(gh api -i -X POST "repos/$REPO/actions/workflows/pr-require-checks.yml/dispatches" \
-f ref=main -F return_run_details=true) || code=$?
-f ref=main -F return_run_details=true 2>"$RUNNER_TEMP/dispatch-stderr") || code=$?
cat "$RUNNER_TEMP/dispatch-stderr" >&2
hdr=$(sed '/^\r\?$/q' <<<"$out")
body=$(sed '1,/^\r\?$/d' <<<"$out")
status=$(awk 'NR == 1 { print $2 }' <<<"$hdr")
Expand All @@ -1286,8 +1301,13 @@ jobs:
wait=60
fi
elif [ -z "$status" ]; then
why="no response (gh exit $code)"
wait=$((RANDOM % (10 * 2 ** (try - 1)) + 1))
error=$(tail -n 1 "$RUNNER_TEMP/dispatch-stderr")
if grep -qE 'dial tcp|tls: (handshake|first record)' "$RUNNER_TEMP/dispatch-stderr"; then
why="the connection failed before the request was sent (gh exit $code: $error)"
wait=$((RANDOM % (10 * 2 ** (try - 1)) + 1))
else
why="no response (gh exit $code: ${error:-no error text}), and the request may have been sent; not sent again"
fi
else
why="HTTP $status, not sent again"
fi
Expand Down
Loading