Skip to content

fix(websocket): prevent reconnect timer multiplication and port exhaustion in RCTReconnectingWebSocket (#58988) - #59055

Open
samirakash248-max wants to merge 1 commit into
react:mainfrom
samirakash248-max:samirakash248-max-patch-1
Open

samirakash248-max wants to merge 1 commit into
react:mainfrom
samirakash248-max:samirakash248-max-patch-1

Conversation

@samirakash248-max

Copy link
Copy Markdown

Summary:

Fixes #58988

In development mode on iOS, RCTReconnectingWebSocket manages the WebSocket connection between the app and the Metro packager (/message?role=ios).

When the target port answers with an HTTP error (e.g. 404/500 from a non-Metro service) instead of refusing the TCP connection (ECONNREFUSED), reconnect timers previously multiplied exponentially ($2^N$):

  1. Inside reconnect, start was scheduled via dispatch_after.
  2. When the block executed, start called stop, releasing _socket.
  3. The check if (!socket) evaluated to true (because socket was captured as a __weak pointer to _socket), scheduling a second reconnect timer.
  4. Meanwhile, the newly opened socket receives an HTTP status code >= 400, which SocketRocket delivers via webSocket:didFailWithError:.
  5. didFailWithError: re-armed for every error code other than ECONNREFUSED, scheduling another timer.

Within minutes, ~15,000 TCP sockets entered TIME_WAIT, completely exhausting the host's ephemeral ports and breaking all local network connections on macOS with can't assign requested address.

Solution:

  • Removed phantom re-arm: Dropped if (!socket) { [weakSelf reconnect]; }.
  • Added _reconnectPending guard: Guarantees at most one reconnect timer is queued at any time.
  • Exponential Backoff: Implemented exponential backoff starting at 2.0s and doubling on consecutive failures up to 120.0s (capped), matching InspectorPackagerConnection.cpp. Resets to 2.0s upon successful connection (webSocketDidOpen:).
  • Uniform error handling: Removed the hacky [error code] != ECONNREFUSED exception; all failures are handled cleanly with backoff.
  • Tokenized Invalidation (_reconnectToken): Invalidates pending timers if stop or manual start occurs.
  • Stale Callback Protection: Ignores delegate callbacks if webSocket != _socket.

Changelog:

[IOS] [FIXED] - Prevent reconnect timer multiplication and host ephemeral port exhaustion in RCTReconnectingWebSocket

Test Plan:

  1. State Machine & Invariant Simulation:
    Simulated rapid reconnection failure cycles under HTTP 404 responses.

    • Old behavior: Timer count multiplied: 1 -> 2 -> 4 -> 8 -> 16 -> 32 -> 64 timers in 12s.
    • New behavior: Timer count remained strictly 1, backing off: 2s -> 4s -> 8s -> 16s -> 32s -> 64s -> 120s -> 120s.
    • Reconnect delay properly resets to 2.0s upon webSocketDidOpen:.
    • reconnect() is rejected when _stopped is YES.
  2. iOS Dev Client Verification:

    • Run iOS app in debug mode against Metro.
    • Stop Metro.
    • Start an HTTP server returning 404 on the Metro port (e.g. python3 -m http.server 8081).
    • Monitor connection attempts via netstat:
      netstat -an | grep ':8081 .*TIME_WAIT'
    • Verify connection attempts back off exponentially up to 120s and host ephemeral ports are not exhausted.

@meta-cla

meta-cla Bot commented Oct 14, 2026

Copy link
Copy Markdown

Hi @samirakash248-max!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Oct 14, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 14, 2026
@meta-cla

meta-cla Bot commented Oct 14, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 14, 2026

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

1 participant