Skip to content

The agent that grows with you

fix(mattermost): stop misclassifying transient errors as auth failures

Stephen Chind184d68f3790
diff --git a/plugins/platforms/mattermost/adapter.py b/plugins/platforms/mattermost/adapter.py
index c1239b37def..bc6288871b2 100644
--- a/plugins/platforms/mattermost/adapter.py
+++ b/plugins/platforms/mattermost/adapter.py
@@ -751,13 +751,9 @@ class MattermostAdapter(BasePlatformAdapter):
# Detect permanent auth/permission failures that will never
# succeed on retry — stop reconnecting instead of looping forever.
import aiohttp
- err_str = str(exc).lower()
if isinstance(exc, aiohttp.WSServerHandshakeError) and exc.status in {401, 403}:
logger.error("Mattermost WS auth failed (HTTP %d) — stopping reconnect", exc.status)
return
- if "401" in err_str or "403" in err_str or "unauthorized" in err_str:
- logger.error("Mattermost WS permanent error: %s — stopping reconnect", exc)
- return
logger.warning("Mattermost WS error: %s — reconnecting in %.0fs", exc, delay)
if self._closing:
diff --git a/tests/gateway/test_ws_auth_retry.py b/tests/gateway/test_ws_auth_retry.py
index 9df545717a5..9f8b3ce04b1 100644
--- a/tests/gateway/test_ws_auth_retry.py
+++ b/tests/gateway/test_ws_auth_retry.py
@@ -14,6 +14,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
# Mattermost: _ws_loop auth-aware retry
# ---------------------------------------------------------------------------
+
class TestMattermostWSAuthRetry:
"""gateway/platforms/mattermost.py — _ws_loop()"""
@@ -30,6 +31,7 @@ class TestMattermostWSAuthRetry:
)
from plugins.platforms.mattermost.adapter import MattermostAdapter
+
adapter = MattermostAdapter.__new__(MattermostAdapter)
adapter._closing = False
@@ -47,17 +49,72 @@ class TestMattermostWSAuthRetry:
# Should have attempted once and stopped, not retried
assert call_count == 1
+ def test_transient_401_substring_does_not_stop_reconnect(self):
+ """A transient exception whose stringified message merely contains
+ "401" (e.g. a proxy error body with digits) must not be mistaken
+ for a genuine auth rejection. The loop should log a warning and
+ retry, not return."""
+ from plugins.platforms.mattermost.adapter import MattermostAdapter
+
+ adapter = MattermostAdapter.__new__(MattermostAdapter)
+ adapter._closing = False
+
+ call_count = 0
+
+ async def fake_connect():
+ nonlocal call_count
+ call_count += 1
+ if call_count >= 2:
+ # Stop the loop once we've proven a retry happened.
+ adapter._closing = True
+ raise RuntimeError(
+ "proxy returned HTTP/1.1 401 in body but connection reset"
+ )
+
+ adapter._ws_connect_and_listen = fake_connect
+
+ with patch("asyncio.sleep", new=AsyncMock()):
+ asyncio.run(adapter._ws_loop())
+
+ # The substring fallback is gone, so this must retry past the
+ # first attempt instead of returning immediately.
+ assert call_count == 2
+
+ def test_closing_flag_prevents_further_connect_attempts(self):
+ """Existing self._closing early-return behavior is unaffected by
+ the substring-fallback removal: once _closing is set, the loop
+ must not attempt to connect at all."""
+ from plugins.platforms.mattermost.adapter import MattermostAdapter
+
+ adapter = MattermostAdapter.__new__(MattermostAdapter)
+ adapter._closing = True
+
+ call_count = 0
+
+ async def fake_connect():
+ nonlocal call_count
+ call_count += 1
+ raise RuntimeError("should never be called")
+
+ adapter._ws_connect_and_listen = fake_connect
+
+ 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:
@@ -67,6 +124,7 @@ class TestMatrixSyncAuthRetry:
nio_mock.SyncError = SyncError
from plugins.platforms.matrix.adapter import MatrixAdapter
+
adapter = MatrixAdapter.__new__(MatrixAdapter)
adapter._closing = False
@@ -86,6 +144,7 @@ class TestMatrixSyncAuthRetry:
async def run():
import sys
+
sys.modules["nio"] = nio_mock
try:
await adapter._sync_loop()
@@ -94,5 +153,3 @@ class TestMatrixSyncAuthRetry:
asyncio.run(run())
assert sync_count == 1
-
-