Skip to content

fix(socket): stop infinite reconnection on Invalid namespace error#4271

Open
octo-patch wants to merge 1 commit intosimstudioai:mainfrom
octo-patch:fix/socket-invalid-namespace-reconnection
Open

fix(socket): stop infinite reconnection on Invalid namespace error#4271
octo-patch wants to merge 1 commit intosimstudioai:mainfrom
octo-patch:fix/socket-invalid-namespace-reconnection

Conversation

@octo-patch
Copy link
Copy Markdown

Summary

  • Detect Socket.IO errors as permanent failures, not transient ones
  • Stop infinite reconnection loop when the server rejects the namespace connection
  • Log a diagnostic hint pointing to as the likely cause

Problem

When the Socket.IO server responds with an error (e.g. because NEXT_PUBLIC_SOCKET_URL points to the wrong host or the client has cached code that references a removed namespace), the connect_error handler did not recognise this as a permanent error. It fell through to the else if (socketInstance.active) branch, set isReconnecting = true, and let Socket.IO (configured with reconnectionAttempts: 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 isNamespaceError check alongside the existing isAuthError check. When the error message contains 'Invalid namespace', the handler now:

  1. Disconnects the socket
  2. Clears socket state
  3. Sets authFailed = true to prevent the initialization useEffect from re-running
  4. Logs a warning pointing to NEXT_PUBLIC_SOCKET_URL misconfiguration

This mirrors the existing auth-failure handling and stops the reconnection loop.

Type of Change

  • Bug fix

Testing

Manually verified that the connect_error handler correctly branches to the new path when the error message is 'Invalid namespace'.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Fixes #2704

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
@cursor
Copy link
Copy Markdown

cursor Bot commented Apr 23, 2026

PR Summary

Low Risk
Low risk, small change to Socket.IO connect_error handling to treat Invalid namespace as a permanent failure and stop auto-reconnect. Main risk is mistakenly classifying other errors as permanent, which would prevent reconnection until a manual retry.

Overview
Prevents infinite Socket.IO reconnection attempts when the server rejects the namespace (e.g., misconfigured NEXT_PUBLIC_SOCKET_URL).

connect_error now detects Invalid namespace as a permanent failure, disconnects/clears socket state, marks authFailed to block re-init, and logs a targeted warning instead of leaving the client stuck in a perpetual "Reconnecting…" state.

Reviewed by Cursor Bugbot for commit e26a2fc. Bugbot is set up for automated code reviews on this repo. Configure here.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 23, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 23, 2026 4:38am

Request Review

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 23, 2026

Greptile Summary

This PR adds an isNamespaceError guard in the connect_error handler that treats Socket.IO "Invalid namespace" responses as permanent failures, mirroring the existing isAuthError path. The fix correctly disconnects the socket, clears state, and sets authFailed = true to prevent the initialization useEffect from re-running indefinitely.

The only notable concern is a semantic one: reusing the authFailed flag causes surrounding log messages (e.g. \"Socket initialization skipped - auth failed, waiting for retry\") and the retryConnection callback to misattribute a namespace configuration error as an auth failure, which could confuse future debugging.

Confidence Score: 5/5

Safe to merge — the fix is minimal, targeted, and functionally correct.

Only finding is P2: reusing authFailed for a namespace error produces misleading logs but does not affect runtime correctness. The reconnection loop is stopped as intended, and no new regressions are introduced.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/app/workspace/providers/socket-provider.tsx Adds isNamespaceError check in connect_error handler that mirrors the existing isAuthError path; correctly disconnects, clears state, and prevents reinit — only minor issue is reuse of authFailed state for a semantically distinct error type

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
Loading

Reviews (1): Last reviewed commit: "fix(socket): stop infinite reconnection ..." | Re-trigger Greptile

Comment on lines +424 to +433
} 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e26a2fc. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]

1 participant