From de7e4a3fbe717f1642c5d78db23c9f9aed381313 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 19:59:48 -0600 Subject: [PATCH] fix(github-backup/drain): drop non-dict or id-less queue events instead of crashing the drain (#6575) --- infra/github-backup/bin/drain_queue.py | 20 ++++++++++ infra/github-backup/tests/test_drain_queue.py | 39 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/infra/github-backup/bin/drain_queue.py b/infra/github-backup/bin/drain_queue.py index 9b79eb11a4..de86a89665 100644 --- a/infra/github-backup/bin/drain_queue.py +++ b/infra/github-backup/bin/drain_queue.py @@ -146,6 +146,8 @@ def event_age_s(ev, now): The Worker stamps ts with `new Date().toISOString()`; accept epoch numbers too so a format change degrades to 'unknown', not a crash. """ + if not isinstance(ev, dict): + return None ts = ev.get("ts") if isinstance(ts, (int, float)): return max(0, int(now - ts)) @@ -173,6 +175,8 @@ def event_repo(ev): webhook-shaped payload too (repository.full_name) so hand-built or future payloads drain identically. """ + if not isinstance(ev, dict): + return None repo = ev.get("repo") if isinstance(repo, str) and repo: return repo @@ -192,6 +196,8 @@ def event_owner(ev): then installation.account.login — the fallback for payloads that carry an installation but no repository owner. """ + if not isinstance(ev, dict): + return None rep = ev.get("repository") if isinstance(rep, dict): owner = rep.get("owner") @@ -246,7 +252,20 @@ def log(line): # by repo; events without a repo carry nothing the drainer can act # on, so they are acked immediately. by_repo, ack, dropped = {}, [], [] + dropped_malformed = 0 for ev in events: + # Event shape itself (#6575): a non-dict element, or a dict + # event whose id is missing or not a non-empty string, can + # never be acked (ack is by id) and must never abort the + # drain — the #3331 poison pill one level up. Drop it loudly, + # count it in the summary, and keep draining. + if not isinstance(ev, dict) or not isinstance(ev.get("id"), str) or not ev.get("id"): + dropped_malformed += 1 + log( + f"{stamp} DRAIN-DROP malformed event={ev!r:.120} " + f"reason=non-dict-or-missing-id" + ) + continue owner = event_owner(ev) repo = event_repo(ev) if owner is None or owner.lower() not in allowed: @@ -317,6 +336,7 @@ def log(line): "failed": failed, "unplanned": unplanned, "dropped_foreign": len(dropped), + "dropped_malformed": dropped_malformed, "coalesced_pending": pending, "acked": len(ack), } diff --git a/infra/github-backup/tests/test_drain_queue.py b/infra/github-backup/tests/test_drain_queue.py index bde114c9d7..9e76d31a79 100644 --- a/infra/github-backup/tests/test_drain_queue.py +++ b/infra/github-backup/tests/test_drain_queue.py @@ -440,6 +440,45 @@ def check(name, cond): check("after-malformed: drop counted in summary", summary.get("dropped_foreign") == 1) +# 16b. event shape itself (#6575): a non-dict element first must not +# abort the drain — it is DRAIN-DROP logged, counted as +# dropped_malformed, never acked (it has no id to ack), and the +# valid planned event behind it is still fetched + acked. +for bad in ("garbage", None, 42): + h = Harness([bad, ev("e17", "owner/repo")], ["owner/repo"], {"owner/repo": "ok"}) + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + rc = h.run() + tag = f"non-dict {type(bad).__name__}" + check(f"{tag}: exit 0", rc == 0) + check(f"{tag}: only valid event acked", h.acked_ids() == ["e17"]) + check(f"{tag}: valid event fetched", h.fetch_calls == ["owner/repo"]) + check(f"{tag}: DRAIN-DROP malformed logged", "DRAIN-DROP malformed" in h.fetch_log()) + summary = json.loads(buf.getvalue().strip().splitlines()[-1]) + check(f"{tag}: counted dropped_malformed", summary.get("dropped_malformed") == 1) + +# 16c. id-less dict events (#6575) in each branch the old code crashed +# on — foreign, no-repo, planned — plus a non-string id: same +# malformed drop, valid event behind each still drains. +idless_cases = [ + ("foreign", {"repo": "stranger/pwned", "event": "push", "ts": iso_age(60)}), + ("no-repo", {"event": "push", "ts": iso_age(60)}), + ("planned", {"repo": "owner/repo", "event": "push", "ts": iso_age(60)}), + ("non-string-id", {"id": 42, "repo": "owner/repo", "event": "push", "ts": iso_age(60)}), +] +for name, bad in idless_cases: + h = Harness([bad, ev("e18", "owner/repo")], ["owner/repo"], {"owner/repo": "ok"}) + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + rc = h.run() + check(f"id-less {name}: exit 0", rc == 0) + check(f"id-less {name}: only valid event acked", h.acked_ids() == ["e18"]) + check(f"id-less {name}: valid event fetched", h.fetch_calls == ["owner/repo"]) + check(f"id-less {name}: DRAIN-DROP malformed logged", "DRAIN-DROP malformed" in h.fetch_log()) + summary = json.loads(buf.getvalue().strip().splitlines()[-1]) + check(f"id-less {name}: counted dropped_malformed", summary.get("dropped_malformed") == 1) + + # 17. a page stream that never completes — every page incomplete with a # fresh, never-repeated cursor (#6572) — must raise at the named # pagination bound instead of looping forever, and a Harness-level