Guest invite: serialize member invites — parallel direct calls can't slip two guests

The one-per-30-days rule is a READ of the guests ledger, provisioning
takes seconds of external calls, and the ledger row that closes the
window is written LAST — so two PARALLEL direct calls (curl, bypassing
the dashboard JS) could both pass guest_request_guard before either row
exists and slip two guests past the rule. The member invite path now
runs guard+provision under _GUEST_INVITE_LOCK (single-process uvicorn),
making check-and-send atomic: the second caller re-runs the guard after
the first caller's ledger row exists and is refused 429.

tests/test_guest_concurrency.py proves both halves on the fixture DB:
unguarded, two parallel invites both provision (the test has teeth);
with the lock, exactly one provisions, one is refused 429, one ledger
row, one account, one email; sequential semantics unchanged.
This commit is contained in:
inference-bot committed 2026-11-08 20:53:54 +00:00
1 parent 1d102a4754
commit 2954fe609f
2 files changed
+281 -6

No files matched your search

+22 -6
View File
@@ -6057,6 +6057,12 @@ async def broker_cancel_gift_alias(request: Request):
# Member self-serve guest invitations (provision immediately, 2026-11-08).
# ---------------------------------------------------------------------------
# Member guest invites serialize on this lock (see POST /broker/guests):
# the one-per-30-days guard reads the guests ledger, and the row that closes
# the window is written only at the END of provisioning.
_GUEST_INVITE_LOCK = asyncio.Lock()
@app.get("/broker/guests")
@limiter.limit("30/minute")
async def broker_guest_list(request: Request):
@@ -6172,6 +6178,15 @@ async def broker_guest_invite(request: Request):
guests group, $2 non-expiring credit, GUEST_DAYS time box, invitation
email naming the inviter, newsletter subscribe, ledger row) and re-runs
every guard, so a moved-state refusal still refuses.
The whole guard-then-provision sequence runs under _GUEST_INVITE_LOCK:
the 30-day rule is a read of the guests ledger, provisioning takes
seconds of external calls, and the ledger row is written LAST — so two
PARALLEL direct calls could otherwise both pass the guard before either
row exists and slip two guests past the one-per-month rule. The app runs
a single uvicorn process, so this lock makes check-and-send atomic for
every member invite; the second caller re-runs the guard AFTER the first
caller's ledger row exists and is refused.
"""
email = _verify_broker_secret(request)
body = await request.json()
@@ -6180,13 +6195,14 @@ async def broker_guest_invite(request: Request):
if not recipient or "@" not in recipient:
raise HTTPException(400, "A valid guest email is required")
conn = get_db()
refusal = guest_request_guard(conn, email, recipient)
conn.close()
if refusal:
raise HTTPException(429, refusal)
async with _GUEST_INVITE_LOCK:
conn = get_db()
refusal = guest_request_guard(conn, email, recipient)
conn.close()
if refusal:
raise HTTPException(429, refusal)
user_id = await provision_guest_member(recipient, name, email)
user_id = await provision_guest_member(recipient, name, email)
return {
"status": "provisioned", "account_type": "guest", "email": recipient,
"inviter": email, "user_id": user_id, "budget": GUEST_BUDGET,