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
20 changes: 20 additions & 0 deletions infra/github-backup/bin/drain_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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),
}
Expand Down
39 changes: 39 additions & 0 deletions infra/github-backup/tests/test_drain_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading