Skip to content

The agent that grows with you

refactor(matrix): classify sync auth errors on errcode/http_status only

kshitijk4poord5430fb0c1fa
diff --git a/plugins/platforms/matrix/adapter.py b/plugins/platforms/matrix/adapter.py
index ccd07ffdcc8..30cdc1822af 100644
--- a/plugins/platforms/matrix/adapter.py
+++ b/plugins/platforms/matrix/adapter.py
@@ -201,76 +201,28 @@ _MATRIX_PERMANENT_ERRCODES = frozenset({
"m_missing_token",
"m_forbidden",
})
-# Leading HTTP status on error strings formatted as ``"<status>: <body>"``.
-_MATRIX_LEADING_STATUS_RE = re.compile(r"^\s*(\d{3})\b")
-# Transport-level failures are always transient. Checked before any status or
-# text inspection so no amount of unlucky digits or keywords in a message can
-# ever promote a network timeout or dropped connection to "permanent".
-_MATRIX_TRANSIENT_EXC_TYPES: tuple[type[BaseException], ...] = (
- asyncio.TimeoutError,
- TimeoutError,
- ConnectionError, # covers ConnectionResetError, ConnectionRefusedError, ...
- OSError,
-)
-def _is_permanent_matrix_auth_error(exc: object) -> bool:
+def _is_permanent_matrix_auth_error(exc: BaseException) -> bool:
"""Return True only for genuine auth failures that must stop the sync loop.
A transient homeserver outage surfaces as a 5xx whose body may be an HTML
error page (Umbrel's app-proxy returns one). Naive substring checks like
``"403" in str(exc)`` false-positive on digits embedded in that HTML (an SVG
- coordinate such as ``40.4302`` contains ``403``), which previously stopped
- the sync loop permanently on a passing blip. Classify on the real HTTP
- status / errcode instead, and retry everything that is not 401/403.
+ coordinate such as ``40.4302`` contains ``403``) or in the ``since`` token
+ echoed by a timeout message, which stopped the sync loop permanently on a
+ passing blip. mautrix raises ``MatrixRequestError`` with ``errcode`` and
+ ``http_status`` for every non-2xx, so classify on those alone; anything
+ without a structured auth signal (timeouts, dropped connections, 5xx) is
+ retried. Deliberately not ``.status``/``.status_code``/``.code``: those
+ belong to unrelated exception shapes (aiohttp responses, OS errno) and can
+ misclassify on a coincidental integer.
"""
- # Transport failures are never permanent, regardless of what their
- # message text says. A ConnectionError raised while retrying a call whose
- # URL happens to contain "403" must still be retried.
- if isinstance(exc, _MATRIX_TRANSIENT_EXC_TYPES):
- return False
-
- # Prefer structured attributes when the exception carries them.
errcode = getattr(exc, "errcode", None)
- if (
- isinstance(errcode, str)
- and errcode.strip().lower() in _MATRIX_PERMANENT_ERRCODES
- ):
+ if isinstance(errcode, str) and errcode.strip().lower() in _MATRIX_PERMANENT_ERRCODES:
return True
-
- # mautrix's MatrixRequestError exposes ``.http_status``. Deliberately not
- # ``.status``/``.status_code``/``.code``: those belong to unrelated
- # exception shapes (aiohttp responses, OS errno, generic process exit
- # codes) and a first-int-wins probe across all of them can misclassify on
- # a coincidental integer that has nothing to do with the HTTP response.
status = getattr(exc, "http_status", None)
- if isinstance(status, int):
- return status in (401, 403)
-
- # Fall back to parsing a leading status code off the string form
- # (e.g. ``"502: <!DOCTYPE html>...``). A known status is authoritative:
- # only 401/403 are permanent; 5xx/429/etc. must retry regardless of body.
- text = str(exc)
- m = _MATRIX_LEADING_STATUS_RE.match(text)
- if m:
- return int(m.group(1)) in (401, 403)
-
- # No structured status available at all: fall back to a bounded,
- # whole-word text scan. This is a deliberate divergence from a
- # structured-only classifier. Some transports (a bare Exception wrapping a
- # homeserver error) never expose http_status/errcode, so without this
- # fallback we'd silently treat every unstructured auth failure as
- # retryable forever.
- # Bounded to the first 200 characters with \b word boundaries so a large
- # HTML error body (which can run to kilobytes) has no way to smuggle a
- # false positive the way the old unbounded substring check did.
- head = text[:200].lower()
- return bool(
- re.search(
- r"\b(m_unknown_token|m_missing_token|m_forbidden|unauthorized|forbidden)\b",
- head,
- )
- )
+ return isinstance(status, int) and status in (401, 403)
class _MatrixHtmlSanitizer(HTMLParser):
@@ -1838,18 +1790,8 @@ class MatrixAdapter(BasePlatformAdapter):
while not self._closing:
try:
# 45s outer cap guards TCP-level hangs the 30s long-poll timeout cannot catch.
+ # mautrix raises on every non-2xx, so a non-dict here is never an error object.
sync_data = await asyncio.wait_for(client.sync(since=next_batch, timeout=30000), timeout=45.0)
-
- # Route result objects through the same classifier as raised errors.
- _sync_msg = getattr(sync_data, "message", None)
- if isinstance(_sync_msg, str):
- structured = isinstance(getattr(sync_data, "errcode", None), str) or isinstance(getattr(sync_data, "http_status", None), int)
- permanent = _is_permanent_matrix_auth_error(sync_data) if structured else _is_permanent_matrix_auth_error(_sync_msg)
- if permanent:
- logger.error("Matrix: permanent auth error from sync: %s, stopping", _sync_msg)
- return
-
-
if isinstance(sync_data, dict):
next_batch = await self._absorb_sync(client, sync_data) or next_batch
await asyncio.sleep(0) # let fresh invite joins start before the next sync
diff --git a/tests/gateway/test_matrix.py b/tests/gateway/test_matrix.py
index 7933e89ed06..aba2eca206c 100644
--- a/tests/gateway/test_matrix.py
+++ b/tests/gateway/test_matrix.py
@@ -1340,208 +1340,67 @@ class TestMatrixSyncLoop:
assert captured[0].text == "hello"
assert captured[0].source.chat_type == "dm"
- @pytest.mark.asyncio
- async def test_sync_loop_retries_transient_error_instead_of_stopping(self):
- """A timeout whose message text incidentally contains "401" must be retried, not fatal.
-
- This is the actual production regression: a plain connection timeout
- wraps the sync pagination token in its message, and that token is an
- arbitrary digit string that happens to contain "401". The old naive
- classifier did `"401" in str(exc).lower()` and stopped the sync loop
- permanently on what was really a transient timeout with no auth
- failure at all.
-
- I confirmed this fixture actually discriminates the two classifiers
- (see _verify_fixture.py, run manually against both): the OLD
- substring check classifies it "permanent" (bug reproduces) and the
- NEW layered classifier classifies it "transient" (fix works), since
- the digits sit inside a pagination token, not a structured
- http_status/errcode and not a whole-word match in the bounded
- prefix scan. A fixture that both classifiers agree on (like the
- old 502/SVG body used before this rework) proves nothing, since
- agreement means nothing was actually being tested.
-
- This test drives `_sync_loop` itself (not just the classifier
- function) so a regression in how the loop wires the classifier in
- would also be caught here.
- """
- adapter = _make_adapter()
- adapter._closing = False
-
- transient_timeout_with_incidental_401 = (
- "Connection timeout to host https://matrix.example.org/_matrix/"
- "client/v3/sync?timeout=30000&since=s72802_401975_486_12943_11759"
- "_12_1514_279_0_1_2_1_1"
- )
-
- calls = {"n": 0}
-
- async def _sync_side_effect(**kwargs):
- calls["n"] += 1
- if calls["n"] == 1:
- raise Exception(transient_timeout_with_incidental_401)
- # Second call: stop the loop cleanly after proving the retry
- # happened, without needing a real sync payload.
- adapter._closing = True
- return {"next_batch": "s1"}
-
- mock_sync_store = MagicMock()
- mock_sync_store.get_next_batch = AsyncMock(return_value=None)
- mock_sync_store.put_next_batch = AsyncMock()
-
- fake_client = MagicMock()
- fake_client.sync = AsyncMock(side_effect=_sync_side_effect)
- fake_client.sync_store = mock_sync_store
- adapter._client = fake_client
-
- with patch("asyncio.sleep", new=AsyncMock()) as mock_sleep:
- await adapter._sync_loop()
-
- # Retried rather than returning after the first (transient) error.
- assert calls["n"] == 2
- # The 5s backoff sleep on the retry path (not the 0s dispatch-yield).
- assert 5 in [call.args[0] for call in mock_sleep.await_args_list]
-
- @pytest.mark.asyncio
- async def test_sync_loop_stops_on_real_permanent_auth_error(self):
- """A genuine 401/403 auth failure must stop the loop, not retry forever.
-
- Note this fixture is not a discriminating RED/GREEN test between the
- old and new classifier. The message text contains the word
- "forbidden", so the old naive substring check also stops the loop
- here, just for the wrong reason (a keyword match instead of a
- structured errcode). I kept this test because it proves the loop
- still stops on a real auth failure after the classifier rewrite, a
- straightforward regression check, not proof that the fix changed
- behavior on this specific input.
- """
- adapter = _make_adapter()
- adapter._closing = False
-
- auth_exc = Exception("M_FORBIDDEN: Invalid access token")
- auth_exc.errcode = "M_FORBIDDEN"
-
- mock_sync_store = MagicMock()
- mock_sync_store.get_next_batch = AsyncMock(return_value=None)
- mock_sync_store.put_next_batch = AsyncMock()
-
- fake_client = MagicMock()
- fake_client.sync = AsyncMock(side_effect=auth_exc)
- fake_client.sync_store = mock_sync_store
- adapter._client = fake_client
-
- with patch("asyncio.sleep", new=AsyncMock()) as mock_sleep:
- await adapter._sync_loop()
-
- # No retry backoff: the loop returned on the first, permanent error.
- assert fake_client.sync.await_count == 1
- assert 5 not in [call.args[0] for call in mock_sleep.await_args_list]
-
- # ------------------------------------------------------------------
- # Result-object (non-exception) auth failures.
- #
- # The pinned mautrix 0.21.0 never returns an error object from sync():
- # HTTPAPI._send raises make_request_error() for any non-2xx and otherwise
- # returns parsed JSON, so a real auth failure arrives as an exception and
- # is covered by the two tests above. The result-object branch in
- # _sync_loop is inherited from the earlier matrix-nio client (whose
- # SyncError objects were genuine) and is kept as defense in depth. These
- # tests pin it to the same classifier the exception path uses so a future
- # client swap cannot silently reintroduce the infinite-resync bug.
- # ------------------------------------------------------------------
-
@staticmethod
- def _result_obj(**attrs):
- """Build an error *result object*, not a MagicMock.
-
- A MagicMock would auto-create ``errcode``/``http_status`` attributes
- and make the structured-vs-text branch untestable, so these fixtures
- expose exactly the attributes a real client object would.
- """
- return type("SyncErrorResult", (), attrs)()
-
- async def _run_loop_with_sync_result(self, result_obj):
- """Drive _sync_loop with sync() returning result_obj, then a clean dict."""
+ def _sync_error(message, **attrs):
+ """Shape of mautrix's MatrixRequestError: message text + structured attrs."""
+ exc = Exception(message)
+ for k, v in attrs.items():
+ setattr(exc, k, v)
+ return exc
+
+ async def _run_sync_loop_with_first_error(self, exc):
+ """Drive _sync_loop: sync() raises exc once, then returns a clean dict and closes."""
adapter = _make_adapter()
adapter._closing = False
-
calls = {"n": 0}
async def _sync_side_effect(**kwargs):
calls["n"] += 1
if calls["n"] == 1:
- return result_obj
- # If the loop treated the object as transient it comes back here;
- # stop cleanly so the test can assert the retry happened.
+ raise exc
adapter._closing = True
return {"next_batch": "s1"}
- mock_sync_store = MagicMock()
- mock_sync_store.get_next_batch = AsyncMock(return_value=None)
- mock_sync_store.put_next_batch = AsyncMock()
-
fake_client = MagicMock()
fake_client.sync = AsyncMock(side_effect=_sync_side_effect)
- fake_client.sync_store = mock_sync_store
+ fake_client.sync_store = MagicMock()
+ fake_client.sync_store.get_next_batch = AsyncMock(return_value=None)
+ fake_client.sync_store.put_next_batch = AsyncMock()
adapter._client = fake_client
-
- with patch("asyncio.sleep", new=AsyncMock()):
+ with patch("asyncio.sleep", new=AsyncMock()) as mock_sleep:
await adapter._sync_loop()
-
- return fake_client.sync.await_count
-
- @pytest.mark.asyncio
- async def test_sync_loop_stops_on_result_object_with_permanent_errcode(self):
- """An M_MISSING_TOKEN result object must stop the loop.
-
- This is the discriminating RED/GREEN case for routing the branch
- through _is_permanent_matrix_auth_error. The old code tested only
- ``"m_unknown_token" in msg or "unknown_token" in msg``, and this
- message ("Missing access token") contains neither, so the old branch
- fell through and resynced forever against a credential that can
- never succeed. Reading the structured errcode catches it.
- """
- obj = self._result_obj(
- message="Missing access token", errcode="M_MISSING_TOKEN"
- )
- assert await self._run_loop_with_sync_result(obj) == 1
-
- @pytest.mark.asyncio
- async def test_sync_loop_stops_on_result_object_with_401_http_status(self):
- """A result object exposing only http_status=401 must stop the loop.
-
- Also discriminating: the message text carries no auth keyword at all,
- so only the structured status read reaches the right verdict.
- """
- obj = self._result_obj(message="Sync request failed", http_status=401)
- assert await self._run_loop_with_sync_result(obj) == 1
+ return fake_client.sync.await_count, [c.args[0] for c in mock_sleep.await_args_list]
@pytest.mark.asyncio
- async def test_sync_loop_stops_on_unstructured_result_object_via_message(self):
- """With no errcode and no http_status, the message text is the only signal.
-
- str(obj) on a result object is an opaque repr like
- ``<SyncErrorResult object at 0x...>``, so the classifier must be
- handed ``.message`` explicitly or this auth failure is missed.
- """
- obj = self._result_obj(message="M_FORBIDDEN: access token rejected")
- assert await self._run_loop_with_sync_result(obj) == 1
+ @pytest.mark.parametrize(
+ "exc",
+ [
+ # Umbrel app-proxy 502: the SVG coordinate 40.4302 embeds "403".
+ _sync_error.__func__(
+ '502: <!DOCTYPE html><svg><path d="M17.4517 40.4302C12.7214 40.4302"/></svg>',
+ http_status=502,
+ ),
+ # Plain timeout echoing the pagination token, which embeds "401".
+ asyncio.TimeoutError(
+ "Connection timeout to host https://matrix.example.org/_matrix/"
+ "client/v3/sync?timeout=30000&since=s72802_401975_486_12943_11759"
+ ),
+ ],
+ ids=["502-html-body-with-403-digits", "timeout-since-token-with-401-digits"],
+ )
+ async def test_sync_loop_retries_transient_error_whose_text_embeds_auth_digits(self, exc):
+ """Both production repros: the old substring check stopped the loop forever on these."""
+ sync_calls, sleeps = await self._run_sync_loop_with_first_error(exc)
+ assert sync_calls == 2
+ assert 5 in sleeps # the retry backoff, not the 0s dispatch-yield
@pytest.mark.asyncio
- async def test_sync_loop_retries_transient_result_object_despite_auth_keyword(self):
- """A structured 502 must be retried even though its body says "Forbidden".
-
- This pins the precedence rule. A homeserver behind a reverse proxy can
- return a 502 HTML error page containing the word "Forbidden"; if the
- message-text scan were allowed to override the structured status, a
- passing outage would permanently kill the sync loop. That is the same
- false-positive class the classifier rework exists to prevent.
- """
- obj = self._result_obj(
- message="<html><body><h1>502 Bad Gateway</h1>Forbidden</body></html>",
- http_status=502,
- )
- assert await self._run_loop_with_sync_result(obj) == 2
+ async def test_sync_loop_stops_on_structured_auth_error(self):
+ """A 401 with errcode M_UNKNOWN_TOKEN is permanent: no retry, loop returns."""
+ exc = self._sync_error("Invalid access token", errcode="M_UNKNOWN_TOKEN", http_status=401)
+ sync_calls, sleeps = await self._run_sync_loop_with_first_error(exc)
+ assert sync_calls == 1
+ assert 5 not in sleeps
@pytest.mark.asyncio
async def test_connect_receives_dm_from_initial_sync_dispatch(self):
@@ -3545,92 +3404,3 @@ class TestCryptoPickleKeyMigration:
# start still sees a legacy-key account and retries the migration.
store.put_account.assert_not_awaited()
assert "retried on the next start" in caplog.text
-
-
-class TestMatrixPermanentAuthClassifier:
- """Only genuine 401/403 auth failures may stop the sync loop.
-
- A transient homeserver outage surfaces as a 5xx whose body may be an HTML
- error page. The Umbrel app-proxy returns one whose embedded SVG contains the
- coordinate ``40.4302``, which contains the substring ``403``. The old
- ``"403" in str(exc)`` check false-positived on that and permanently halted
- Matrix sync on a passing blip. The classifier must retry any status that is
- not 401/403.
- """
-
- # Trimmed excerpt of the actual Umbrel 502 body that caused the outage;
- # the SVG path coordinate 40.4302 contains the substring "403".
- _REAL_502 = (
- '502: <!DOCTYPE html><svg><path d="M17.4517 40.4302C12.7214 40.4302'
- ' 9.82339 41.8182 7.98048 44.0001"/></svg>'
- )
-
- def _fn(self):
- from plugins.platforms.matrix.adapter import _is_permanent_matrix_auth_error
-
- return _is_permanent_matrix_auth_error
-
- def test_transient_502_html_body_is_retried(self):
- # The exact failure mode: a 502 whose HTML body embeds "403".
- assert self._fn()(Exception(self._REAL_502)) is False
-
- @pytest.mark.parametrize("status", [500, 502, 503, 504, 429])
- def test_server_errors_are_retried(self, status):
- assert self._fn()(Exception(f"{status}: upstream unavailable")) is False
-
- def test_connection_errors_are_retried(self):
- fn = self._fn()
- assert fn(Exception("[Errno 104] Connection reset by peer")) is False
- assert fn(Exception("Server disconnected")) is False
-
- @pytest.mark.parametrize("status", [401, 403])
- def test_real_auth_status_stops_sync(self, status):
- assert self._fn()(Exception(f"{status}: nope")) is True
-
- def test_http_status_attribute_beats_body_digits(self):
- # A structured 502 whose message text also contains "403" must retry.
- exc = Exception("body 40.4302")
- exc.http_status = 502
- assert self._fn()(exc) is False
-
- def test_errcode_unknown_token_stops_sync(self):
- exc = Exception("sync failed")
- exc.errcode = "M_UNKNOWN_TOKEN"
- assert self._fn()(exc) is True
-
- def test_bare_auth_keyword_without_status_stops_sync(self):
- assert self._fn()(Exception("Unauthorized")) is True
-
- def test_misleading_code_attribute_is_not_misclassified(self):
- # A non-Matrix exception with a bare `.code` that happens to be 401
- # must NOT be treated as an auth failure. `.code` on an arbitrary
- # exception can be an errno or an exit code; only `.http_status`
- # is trustworthy for HTTP status classification.
- exc = Exception("subprocess failed")
- exc.code = 401
- assert self._fn()(exc) is False
-
- def test_status_attribute_is_ignored_in_favor_of_http_status(self):
- # aiohttp response objects and similar carry `.status`, which is a
- # different namespace than mautrix's `.http_status`. A bare `.status`
- # must not drive the classification.
- exc = Exception("some http client error")
- exc.status = 403
- assert self._fn()(exc) is False
-
- @pytest.mark.parametrize(
- "exc_factory",
- [
- lambda: asyncio.TimeoutError("401 in the pagination token url"),
- lambda: TimeoutError("403 in the pagination token url"),
- lambda: ConnectionError("... forbidden ..."),
- lambda: ConnectionResetError("Connection reset, status 401"),
- lambda: OSError("network unreachable, code 403"),
- ],
- )
- def test_transient_exception_types_never_classify_as_permanent(self, exc_factory):
- # Transport-level exception types are short-circuited to "transient"
- # before any text/status inspection runs, so a coincidental "401"
- # or the keyword "forbidden" inside their message can never flip
- # them to permanent.
- assert self._fn()(exc_factory()) is False
diff --git a/tests/gateway/test_ws_auth_retry.py b/tests/gateway/test_ws_auth_retry.py
index f07bba85630..a00f4fb659c 100644
--- a/tests/gateway/test_ws_auth_retry.py
+++ b/tests/gateway/test_ws_auth_retry.py
@@ -122,55 +122,3 @@ class TestMattermostWSAuthRetry:
asyncio.run(adapter._ws_loop())
assert call_count == 0
-
-
-# ---------------------------------------------------------------------------
-# Matrix: _sync_loop auth-aware retry
-# ---------------------------------------------------------------------------
-
-
-class TestMatrixSyncAuthRetry:
- """gateway/platforms/matrix.py — _sync_loop()"""
-
- def test_unknown_token_sync_error_stops_loop(self):
- """A SyncError with M_UNKNOWN_TOKEN should stop syncing."""
- import types
-
- nio_mock = types.ModuleType("nio")
-
- class SyncError:
- def __init__(self, message):
- self.message = message
-
- nio_mock.SyncError = SyncError
-
- from plugins.platforms.matrix.adapter import MatrixAdapter
-
- adapter = MatrixAdapter.__new__(MatrixAdapter)
- adapter._closing = False
-
- sync_count = 0
-
- async def fake_sync(timeout=30000, since=None):
- nonlocal sync_count
- sync_count += 1
- return SyncError("M_UNKNOWN_TOKEN: Invalid access token")
-
- adapter._client = MagicMock()
- adapter._client.sync = fake_sync
- adapter._client.sync_store = MagicMock()
- adapter._client.sync_store.get_next_batch = AsyncMock(return_value=None)
- adapter._pending_megolm = []
- adapter._joined_rooms = set()
-
- async def run():
- import sys
-
- sys.modules["nio"] = nio_mock
- try:
- await adapter._sync_loop()
- finally:
- del sys.modules["nio"]
-
- asyncio.run(run())
- assert sync_count == 1