From 419b1f4322c525459f31c78603b02a2caadabef4 Mon Sep 17 00:00:00 2001 From: inference-bot Date: Mon, 23 Nov 2026 14:26:03 +0000 Subject: [PATCH] matrix sync: skip a member's remaining rooms after a transport error An unreachable remote homeserver hangs every invite POST to that member until the client timeout (8 rooms x 30s), which outlived the 5-minute service-sync tick and starved every member queued behind it. Raise a member-scoped _TransportError instead of returning False, skip that member's remaining rooms for this pass, and report transport_skipped in the result. Invites stay idempotent, so the next pass retries. Test: a timed-out member is attempted once, not once per room. --- app/main.py | 37 ++++++++++++++++++++++++++++------- tests/test_service_invites.py | 11 +++++++++-- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/app/main.py b/app/main.py index 7e0bfbc..207c514 100644 --- a/app/main.py +++ b/app/main.py @@ -2285,6 +2285,13 @@ async def sync_matrix_space() -> dict: super().__init__(f"retry_after_ms={wait_ms}") self.wait_ms = wait_ms + class _TransportError(Exception): + """The invite POST itself never got an answer (timeout/reset).""" + + def __init__(self, kind): + super().__init__(kind) + self.kind = kind + async def invite(mxid: str, room: str) -> bool: try: r = await client.post( @@ -2293,14 +2300,16 @@ async def sync_matrix_space() -> dict: ) except httpx.HTTPError as exc: # A transport failure (read timeout, connection reset) belongs to - # ONE member/room, not to the sync. Synapse federates some invites - # and can exceed the read timeout. Swallow it, log it, let the next - # pass retry: raising here aborted the whole pass and silently - # skipped every member after this one, which is how a new member - # sat uninvited for a day. + # ONE member, not to the sync. Synapse federates some invites and + # can exceed the read timeout. Raise a member-scoped error so the + # caller can skip this member's REMAINING rooms: a remote homeserver + # that cannot be reached hangs every room until the client timeout + # (8 x 30s), which outlived the scheduled tick and starved every + # member queued behind it. Invites are idempotent; the next pass + # retries. logger.warning("Matrix invite %s -> %s transport error: %s", mxid, room, type(exc).__name__) - return False + raise _TransportError(type(exc).__name__) from exc if r.status_code == 200: return True if r.status_code == 429: @@ -2319,6 +2328,7 @@ async def sync_matrix_space() -> dict: rooms = member_invite_rooms() invited_count = 0 rate_limited = False + transport_skipped = 0 async with httpx.AsyncClient(timeout=30) as client: # Snapshot EVERY room's membership once, up front: who is joined and who @@ -2379,6 +2389,18 @@ async def sync_matrix_space() -> dict: sent_any = True if sent_any: invited_count += 1 + except _TransportError as exc: + # One member's federation timeout must not cost the pass their + # remaining rooms. An unreachable remote homeserver hangs EVERY + # room's invite until the client timeout, so retrying room by + # room burns the whole tick and the members queued behind this + # one never get invited. Skip this member and move on; invites + # are idempotent and the next pass retries. + transport_skipped += 1 + logger.info( + "Matrix sync: %s transport error (%s); skipping their remaining " + "rooms this pass", mxid, exc.kind) + continue except _RateLimited as exc: rate_limited = True logger.info( @@ -2466,7 +2488,8 @@ async def sync_matrix_space() -> dict: local_mxid, room, type(exc).__name__) if retired: logger.info("Matrix sync: retired %s local account(s)/invite(s)", retired) - return {"invited": invited_count, "rate_limited": rate_limited} + return {"invited": invited_count, "rate_limited": rate_limited, + "transport_skipped": transport_skipped} async def matrix_member_mxids(client: httpx.AsyncClient): diff --git a/tests/test_service_invites.py b/tests/test_service_invites.py index 6aa266f..559f6de 100644 --- a/tests/test_service_invites.py +++ b/tests/test_service_invites.py @@ -74,6 +74,7 @@ UNREADABLE = set() # rooms whose joined_members 500s invites_sent = [] # (room, mxid) INVITE_429_FOR = set() # mxids whose invite comes back 429 INVITE_TIMEOUT_FOR = set() # mxids whose invite POST raises httpx.ReadTimeout +TIMEOUT_ATTEMPTS = [] # (room, mxid) for every POST that timed out POSTS = [] # Loomio POST payloads ROSTER = {} # Loomio roster: email -> membership id @@ -129,6 +130,7 @@ class FakeClient: if "/invite" in url: mxid = (json or {}).get("user_id") if mxid in INVITE_TIMEOUT_FOR: + TIMEOUT_ATTEMPTS.append((url, mxid)) raise httpx.ReadTimeout("timed out") if mxid in INVITE_429_FOR: return FakeResp(429, {"retry_after_ms": 300000}, @@ -165,6 +167,7 @@ def reset(): global invites_sent invites_sent = [] POSTS.clear() + TIMEOUT_ATTEMPTS.clear() async def main(): @@ -209,8 +212,8 @@ async def main(): not any(mx == "@amember:inference.coop" for _, mx in invites_sent)) check("per-room: two members can be pending on different rooms", not any(mx == "@cmember:inference.coop" for _, mx in invites_sent)) - check("return shape: the result carries invited + rate_limited", - set(r) == {"invited", "rate_limited"} and r["rate_limited"] is False) + check("return shape: the result carries invited + rate_limited + transport_skipped", + set(r) == {"invited", "rate_limited", "transport_skipped"} and r["rate_limited"] is False) # --- 2. IDEMPOTENCE: a second pass sends nothing ---------------------- reset() @@ -325,6 +328,10 @@ async def main(): {room for room, _ in invites_sent} == set(ROOMS) and all(mx == "@umember:inference.coop" for _, mx in invites_sent)) check("timeout: the timed-out member is not counted as invited", r["invited"] == 1) + check("timeout: a timed-out member is attempted ONCE, not once per room", + len(TIMEOUT_ATTEMPTS) == 1) + check("timeout: the pass reports the member whose rooms it skipped", + r.get("transport_skipped") == 1) # The next pass retries the timed-out member (invites are idempotent). INVITE_TIMEOUT_FOR = set()