fix(socket): stop infinite reconnection on Invalid namespace error#4271
fix(socket): stop infinite reconnection on Invalid namespace error#4271octo-patch wants to merge 1 commit intosimstudioai:mainfrom
Conversation
When the Socket.IO server responds with an 'Invalid namespace' error, the client was falling into the auto-reconnect path and retrying forever. This caused an endless 'Reconnecting...' UI state with no way to recover short of a hard page reload. 'Invalid namespace' is a permanent error — the server explicitly rejected the namespace and retrying will not succeed until the configuration is corrected. Treat it the same as auth failures: disconnect, clear the socket, set authFailed to stop the re-initialization loop, and log a diagnostic message pointing to NEXT_PUBLIC_SOCKET_URL as the likely misconfiguration. Fixes simstudioai#2704
PR SummaryLow Risk Overview
Reviewed by Cursor Bugbot for commit e26a2fc. Bugbot is set up for automated code reviews on this repo. Configure here. |
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Greptile SummaryThis PR adds an The only notable concern is a semantic one: reusing the Confidence Score: 5/5Safe to merge — the fix is minimal, targeted, and functionally correct. Only finding is P2: reusing No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[connect_error event] --> B{isAuthError?}
B -- Yes --> C[disconnect + clear socket\nsetAuthFailed true\nstop reconnection\nreset initializedRef]
B -- No --> D{isNamespaceError?\nInvalid namespace}
D -- Yes --> E[disconnect + clear socket\nsetAuthFailed true\nstop reconnection\nreset initializedRef\nlog config hint]
D -- No --> F{socketInstance.active?}
F -- Yes --> G[setIsReconnecting true\nauto-reconnect continues]
F -- No --> H[no-op]
C --> I[useEffect guard blocks\nre-initialization]
E --> I
Reviews (1): Last reviewed commit: "fix(socket): stop infinite reconnection ..." | Re-trigger Greptile |
| } else if (isNamespaceError) { | ||
| logger.warn( | ||
| 'Invalid namespace error - stopping reconnection attempts. Check NEXT_PUBLIC_SOCKET_URL configuration.', | ||
| { message: error.message } | ||
| ) | ||
| socketInstance.disconnect() | ||
| setSocket(null) | ||
| setAuthFailed(true) | ||
| setIsReconnecting(false) | ||
| initializedRef.current = false |
There was a problem hiding this comment.
Misleading
authFailed state for namespace errors
authFailed is set to true for a namespace error, but the state variable and several surrounding log messages assume auth is the root cause. Line 327 will log "Socket initialization skipped - auth failed, waiting for retry" even when the actual failure was a bad NEXT_PUBLIC_SOCKET_URL. Likewise, the retryConnection callback logs "Retrying socket connection after auth failure" and its comment block says "Call this when user has re-authenticated". This is only a semantic concern — the guard logic works correctly — but it could mislead developers debugging a namespace misconfiguration.
Consider introducing a separate state variable (e.g. permanentError) or at minimum a permanentFailureReason that both auth and namespace errors can set, so that log messages and the public retryConnection surface accurate context.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit e26a2fc. Configure here.
| ) | ||
| socketInstance.disconnect() | ||
| setSocket(null) | ||
| setAuthFailed(true) |
There was a problem hiding this comment.
Namespace error reuses auth-specific flag and recovery path
Low Severity
The namespace error handler sets authFailed = true, overloading a flag that semantically represents authentication failure. This causes retryConnection to log "Retrying socket connection after auth failure" when the actual issue is a misconfigured NEXT_PUBLIC_SOCKET_URL, and the init useEffect guard to log "Socket initialization skipped - auth failed." Since authFailed is exposed in the context's public API, future consumers could incorrectly show a "re-login" prompt for what is actually a permanent configuration error that no amount of re-authentication will fix.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit e26a2fc. Configure here.


Summary
Problem
When the Socket.IO server responds with an error (e.g. because
NEXT_PUBLIC_SOCKET_URLpoints to the wrong host or the client has cached code that references a removed namespace), theconnect_errorhandler did not recognise this as a permanent error. It fell through to theelse if (socketInstance.active)branch, setisReconnecting = true, and let Socket.IO (configured withreconnectionAttempts: Infinity) retry forever.The result was an endless "Reconnecting..." notification in the UI with no way to recover short of a hard page reload.
Fix
Added an
isNamespaceErrorcheck alongside the existingisAuthErrorcheck. When the error message contains'Invalid namespace', the handler now:authFailed = trueto prevent the initializationuseEffectfrom re-runningNEXT_PUBLIC_SOCKET_URLmisconfigurationThis mirrors the existing auth-failure handling and stops the reconnection loop.
Type of Change
Testing
Manually verified that the
connect_errorhandler correctly branches to the new path when the error message is'Invalid namespace'.Checklist
Fixes #2704