webhook: stop a transient OC read failure from costing a member credits

On 2026-11-13 an `order.processed` delivery for a $50 credit pack returned 500.
fetch_members() -> fetch_all_member_nodes() used a bare httpx.AsyncClient(), so OC
was given httpx's DEFAULT 5-second timeout; the read timed out, httpx.ReadTimeout
escaped the handler as an unhandled ASGI exception, and the purchase was never
credited. The member paid and got nothing, and the log held a stack trace rather
than anything naming the member or the order.

Three changes, each aimed at a different failure:

* fetch_all_member_nodes: an EXPLICIT 30s timeout, and one retry per page on a
  transport error (`_oc_graphql_post`). Still RAISES when it cannot get the full
  list — the sweep's fail-safes depend on that.
* opencollective_webhook: the work moves to _process_opencollective_webhook and a
  thin wrapper answers 503 (retryable) with an ERROR line naming the exception if
  anything unexpected escapes. A 401 stays a 401; that status is the signal that
  the webhook URL and WEBHOOK_TOKEN disagree, and swallowing it would hide a
  misconfigured webhook.
* tests/test_webhook_resilience.py proves it: 401 preserved, 503 (not 500) on a
  simulated ReadTimeout, credits granted exactly once per OC order, an identical
  re-delivery answering already_credited with no money moved, and the retry/raise
  behaviour of the transport helper.
This commit is contained in:
inference-admin committed 2026-11-13 20:04:09 +00:00
1 parent 5705d02792
commit f45e4ef305
2 files changed
+262 -5

No files matched your search

+193
View File
@@ -0,0 +1,193 @@
#!/usr/bin/env python3
"""Regression test: an Open Collective webhook must never fail as a bare 500.
WHY THIS EXISTS
On 2026-11-13 a member's $50 credit-pack purchase arrived as an
`order.processed` delivery. The handler called fetch_members() to turn the OC
slug into an email, `fetch_all_member_nodes` used a bare httpx.AsyncClient()
(httpx's DEFAULT 5-second timeout), OC's GraphQL answered slower than that,
and `httpx.ReadTimeout` escaped the handler as an unhandled ASGI exception:
* the delivery returned 500, so nothing retried it usefully,
* no credits were granted — the member paid and got nothing for 40 minutes,
* the log carried a stack trace instead of anything naming the member or
the order, so the purchase could not be reconciled from the log alone.
The guards proven here, on a FIXTURE db with the network stubbed:
AUTH
1. A wrong URL token is still a 401 — the new error net must not swallow
the one status that means "the webhook URL and WEBHOOK_TOKEN disagree".
NO BARE 500
2. A transport failure while resolving the purchase answers 503 (retryable)
and the handler does not raise out of the ASGI app.
3. The same is true for a failure raised anywhere else in the handler.
MONEY
4. A resolvable purchase grants credits ONCE, with the per-order reference
`oc:<slug>:<idV2>`.
5. Re-delivering the identical payload returns `already_credited` and moves
no money — this is what makes OC's retries safe.
TRANSPORT
6. `_oc_graphql_post` retries once on a transport error and returns the
response; if both attempts fail it RAISES (callers keep their
fail-safes — a partial member list must never look complete).
Runnable: python3 tests/test_webhook_resilience.py (from the repo root)
"""
import asyncio
import os
import sys
import tempfile
import httpx
os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "fixture.db")
os.environ["WEBHOOK_TOKEN"] = "test-webhook-token"
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import app.main as m # noqa: E402
# The app module shares this httpx object, so stubbing it later would replace
# httpx.AsyncClient process-wide — keep a handle on the real class.
REAL_ASYNC_CLIENT = httpx.AsyncClient
m.DB_PATH = os.environ["DB_PATH"]
TOKEN = "test-webhook-token"
MEMBER_SLUG = "nightrose"
MEMBER_EMAIL = "nightrose@example.org"
ORDER_ID = "or_testOrderId123"
PAYLOAD = {
"type": "order.processed",
"data": {
"fromCollective": {"slug": MEMBER_SLUG, "name": "NightRose"},
"order": {
"idV2": ORDER_ID,
"description": "Financial contribution to Inference Cooperative (Credit pack)",
"totalAmount": 5000,
},
"amount": 5000,
"firstPayment": False,
},
}
results = []
def check(name, ok):
results.append((name, ok))
# --- stubs ----------------------------------------------------------------- #
async def fetch_members_ok():
return [{"email": MEMBER_EMAIL, "name": "NightRose", "slug": MEMBER_SLUG,
"role": "BACKER"}]
async def fetch_members_timeout():
raise httpx.ReadTimeout("simulated OC read timeout")
async def order_amount_ok(_order_id_v2=None):
return 5000
async def budget_noop(_email):
return 0.0
async def main():
m.get_db().close() # create + migrate the fixture schema
conn = m.get_db()
conn.execute("INSERT OR REPLACE INTO members (email, slug, active, credit_balance) "
"VALUES (?, ?, 1, 0)", (MEMBER_EMAIL, MEMBER_SLUG))
conn.commit()
conn.close()
transport = httpx.ASGITransport(app=m.app)
url = f"/webhook/opencollective/{TOKEN}"
m.sync_team_budget_from_balance = budget_noop # no LiteLLM calls in a test
m.get_order_amount = order_amount_ok
async with REAL_ASYNC_CLIENT(transport=transport, base_url="http://test") as c:
# --- 1. AUTH: a wrong token is still a 401. ---
r = await c.post("/webhook/opencollective/wrong-token", json=PAYLOAD)
check(f"auth: a wrong webhook token is 401 (got {r.status_code})", r.status_code == 401)
# --- 2. NO BARE 500 when the OC lookup times out. ---
m.fetch_members = fetch_members_timeout
r = await c.post(url, json=PAYLOAD)
body = r.json() if r.headers.get("content-type", "").startswith("application/json") else {}
check(f"timeout: the delivery answers 503, not 500 (got {r.status_code})",
r.status_code == 503)
check(f"timeout: the response asks for a retry (got {body})",
body.get("retry") is True)
check("timeout: an unexpected failure never raises out of the app "
"(httpx.ReadTimeout was caught by the handler net)",
r.status_code != 500)
# --- 3. MONEY: a resolvable purchase credits exactly once. ---
m.fetch_members = fetch_members_ok
r = await c.post(url, json=PAYLOAD)
body = r.json()
check(f"grant: a resolvable credit pack grants credits (got {body})",
r.status_code == 200 and body.get("status") == "credits_added"
and body.get("credits_added") == 50.0)
check("grant: the balance reflects the purchase",
m.get_credit_balance(MEMBER_EMAIL) == 50.0)
ref = m.credit_pack_reference(MEMBER_SLUG, {"idV2": ORDER_ID})
check("grant: the ledger row is keyed on the OC order (idempotence key)",
ref == f"oc:{MEMBER_SLUG}:{ORDER_ID}" and m.credit_reference_exists(ref))
# --- 4. Re-delivery moves no money. ---
r = await c.post(url, json=PAYLOAD)
body = r.json()
check(f"redelivery: the identical payload answers already_credited (got {body})",
r.status_code == 200 and body.get("status") == "already_credited"
and body.get("credits_added") == 0.0)
check("redelivery: the balance is unchanged",
m.get_credit_balance(MEMBER_EMAIL) == 50.0)
# --- 5. TRANSPORT: retry-once, then raise. ---
calls = []
class FlakyClient:
def __init__(self, fail_times):
self.fail_times = fail_times
async def post(self, *a, **k):
calls.append(1)
if len(calls) <= self.fail_times:
raise httpx.ReadTimeout("simulated timeout")
return "RESPONSE"
sentinel = await m._oc_graphql_post(FlakyClient(1), {"query": "x"})
check(f"transport: one transient failure is retried and succeeds (got {sentinel!r})",
sentinel == "RESPONSE" and len(calls) == 2)
calls.clear()
err = None
try:
await m._oc_graphql_post(FlakyClient(2), {"query": "x"})
except Exception as e: # noqa: BLE001
err = e
check("transport: a persistent failure still RAISES to the caller",
isinstance(err, httpx.ReadTimeout))
# --- 6. The client used for the member walk has a real timeout. ---
src = open(os.path.join(os.path.dirname(os.path.dirname(
os.path.abspath(__file__))), "app", "main.py"), encoding="utf-8").read()
check("timeout: fetch_all_member_nodes does NOT use httpx's 5s default",
"async with httpx.AsyncClient(timeout=30.0) as client:" in src)
asyncio.run(main())
fails = sum(not ok for _, ok in results)
for name, ok in results:
print(f" [{'PASS' if ok else 'FAIL'}] {name}")
print(f"\n{len(results) - fails}/{len(results)} passed")
sys.exit(1 if fails else 0)