-
Notifications
You must be signed in to change notification settings - Fork 46.7k
fix(simplex): escalate non-retryable WebSocket handshake rejections instead of looping forever #35557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lambertian
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
lambertian:fix/simplex-handshake-fatal-escalation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(simplex): escalate non-retryable WebSocket handshake rejections instead of looping forever #35557
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -320,6 +320,116 @@ async def test_standalone_send_missing_url(monkeypatch): | |
| assert "error" in result | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 9b. Reconnect loop escalates a non-retryable handshake rejection | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ws_listener_escalates_4xx_handshake_rejection(monkeypatch): | ||
| """A 4xx WebSocket-handshake rejection is fatal, not an endless retry. | ||
|
|
||
| A permanently misconfigured daemon answers the handshake with an HTTP | ||
| 4xx. That can never succeed on retry, so the listener must flag a | ||
| non-retryable fatal error and stop looping instead of busy-reconnecting | ||
| forever. | ||
| """ | ||
| import asyncio | ||
|
|
||
| import websockets | ||
| from gateway.config import PlatformConfig | ||
|
|
||
| # Build a real handshake-rejection exception for the installed | ||
| # websockets version (InvalidStatus on >= 14, else InvalidStatusCode). | ||
| try: | ||
| from websockets.exceptions import InvalidStatus | ||
| from websockets.datastructures import Headers | ||
| from websockets.http11 import Response | ||
|
|
||
| rejection = InvalidStatus(Response(403, "Forbidden", Headers())) | ||
| except ImportError: # pragma: no cover - very old websockets | ||
| from websockets.exceptions import InvalidStatusCode | ||
|
|
||
| rejection = InvalidStatusCode(403, Headers()) # type: ignore[call-arg] | ||
|
|
||
| cfg = PlatformConfig(enabled=True, extra={"ws_url": "ws://localhost:5225"}) | ||
| adapter = SimplexAdapter(cfg) | ||
| adapter._running = True | ||
|
|
||
| # Every connect attempt is rejected with the 4xx handshake error. | ||
| def _raise_rejection(*args, **kwargs): | ||
| raise rejection | ||
|
|
||
| monkeypatch.setattr(websockets, "connect", _raise_rejection) | ||
|
|
||
| fatal_calls = [] | ||
| monkeypatch.setattr( | ||
| adapter, | ||
| "_set_fatal_error", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mocking |
||
| lambda code, message, *, retryable: fatal_calls.append( | ||
| (code, message, retryable) | ||
| ), | ||
| ) | ||
|
|
||
| # If the fix is missing the loop busy-retries forever — fail fast rather | ||
| # than hang the suite. | ||
| await asyncio.wait_for(adapter._ws_listener(), timeout=5.0) | ||
|
|
||
| assert len(fatal_calls) == 1, "expected exactly one fatal-error escalation" | ||
| code, _message, retryable = fatal_calls[0] | ||
| assert retryable is False | ||
| assert code == "handshake_rejected" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ws_listener_retries_5xx_handshake_rejection(monkeypatch): | ||
| """A 5xx handshake rejection is transient — keep retrying, don't escalate.""" | ||
| import asyncio | ||
|
|
||
| import websockets | ||
| from gateway.config import PlatformConfig | ||
|
|
||
| try: | ||
| from websockets.exceptions import InvalidStatus | ||
| from websockets.datastructures import Headers | ||
| from websockets.http11 import Response | ||
|
|
||
| rejection = InvalidStatus(Response(503, "Unavailable", Headers())) | ||
| except ImportError: # pragma: no cover - very old websockets | ||
| from websockets.exceptions import InvalidStatusCode | ||
|
|
||
| rejection = InvalidStatusCode(503, Headers()) # type: ignore[call-arg] | ||
|
|
||
| cfg = PlatformConfig(enabled=True, extra={"ws_url": "ws://localhost:5225"}) | ||
| adapter = SimplexAdapter(cfg) | ||
| adapter._running = True | ||
|
|
||
| attempts = 0 | ||
|
|
||
| def _raise_rejection(*args, **kwargs): | ||
| nonlocal attempts | ||
| attempts += 1 | ||
| # Stop the loop after it has demonstrated a retry rather than escalating. | ||
| if attempts >= 2: | ||
| adapter._running = False | ||
| raise rejection | ||
|
|
||
| monkeypatch.setattr(websockets, "connect", _raise_rejection) | ||
| # Don't actually sleep through the backoff. | ||
| monkeypatch.setattr(asyncio, "sleep", AsyncMock()) | ||
|
|
||
| fatal_calls = [] | ||
| monkeypatch.setattr( | ||
| adapter, | ||
| "_set_fatal_error", | ||
| lambda code, message, *, retryable: fatal_calls.append(retryable), | ||
| ) | ||
|
|
||
| await asyncio.wait_for(adapter._ws_listener(), timeout=5.0) | ||
|
|
||
| assert attempts >= 2, "5xx should be retried, not escalated on first hit" | ||
| assert fatal_calls == [], "5xx must not trigger a fatal escalation" | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 10. register() — plugin-side metadata | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes every 4xx terminal, although the demonstrated permanent case is 403. Please use an explicit SimpleX-specific terminal-status set and add coverage for a transient or ambiguous 4xx response so this does not turn a recoverable rejection into a permanently disabled adapter.