Skip to content

Add exponential backoff to InspectorPackagerConnection reconnect and exception handling to connect (#56650)#56650

Closed
shubhamksavita wants to merge 1 commit into
react:mainfrom
shubhamksavita:export-D100956687
Closed

Add exponential backoff to InspectorPackagerConnection reconnect and exception handling to connect (#56650)#56650
shubhamksavita wants to merge 1 commit into
react:mainfrom
shubhamksavita:export-D100956687

Conversation

@shubhamksavita

@shubhamksavita shubhamksavita commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Summary:

Summary

Fix occasional SIGSEGV in operator new during InspectorPackagerConnection::connectWebSocket().

Root Cause

The InspectorPackagerConnection reconnection loop retries every 2 seconds with no backoff or limit. When no Metro dev server is reachable, this results in hundreds of failed WebSocket connection attempts over the app lifetime. Each attempt allocates and deallocates C++ hybrid objects, JNI references, and OkHttp WebSocket instances. This accumulated churn leads to heap fragmentation and eventual heap metadata corruption, manifesting as SIGSEGV in operator new when the allocator follows corrupted internal pointers.

Additionally, the connect() method had no exception handling around the connectWebSocket() call, meaning any JNI/Java exception would propagate uncaught.

Changes

  1. Exponential backoff in reconnect(): Start at 2s, double each retry, cap at 120s. This reduces reconnection attempts from ~500 in 17 minutes to ~15, drastically reducing resource churn.
  2. Try-catch in connect(): Catch exceptions from connectWebSocket() (e.g., fbjni/JNI exceptions) and gracefully trigger a reconnect instead of crashing.
  3. Reset backoff on success: When didOpen() fires (successful connection), reset the delay back to 2s and clear suppressConnectionErrors_.

Changelog:

[General][Fixed] - Add exponential backoff and exception handling to InspectorPackagerConnection reconnect loop to prevent heap fragmentation crashes

Reviewed By: cipolleschi

Differential Revision: D100956687

@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 Apr 29, 2026
@meta-codesync

meta-codesync Bot commented Apr 29, 2026

Copy link
Copy Markdown

@shubhamksavita has exported this pull request. If you are a Meta employee, you can view the originating Diff in D100956687.

…exception handling to connect (react#56650)

Summary:

## Summary

Fix occasional SIGSEGV in `operator new` during `InspectorPackagerConnection::connectWebSocket()`.

### Root Cause

The `InspectorPackagerConnection` reconnection loop retries every 2 seconds with no backoff or limit. When no Metro dev server is reachable, this results in hundreds of failed WebSocket connection attempts over the app lifetime. Each attempt allocates and deallocates C++ hybrid objects, JNI references, and OkHttp WebSocket instances. This accumulated churn leads to heap fragmentation and eventual heap metadata corruption, manifesting as SIGSEGV in `operator new` when the allocator follows corrupted internal pointers.

Additionally, the `connect()` method had no exception handling around the `connectWebSocket()` call, meaning any JNI/Java exception would propagate uncaught.

### Changes

1. **Exponential backoff in `reconnect()`**: Start at 2s, double each retry, cap at 120s. This reduces reconnection attempts from ~500 in 17 minutes to ~15, drastically reducing resource churn.
2. **Try-catch in `connect()`**: Catch exceptions from `connectWebSocket()` (e.g., fbjni/JNI exceptions) and gracefully trigger a reconnect instead of crashing.
3. **Reset backoff on success**: When `didOpen()` fires (successful connection), reset the delay back to 2s and clear `suppressConnectionErrors_`.

## Changelog:
[General][Fixed] - Add exponential backoff and exception handling to InspectorPackagerConnection reconnect loop to prevent heap fragmentation crashes

Reviewed By: cipolleschi

Differential Revision: D100956687
@meta-codesync meta-codesync Bot changed the title Add exponential backoff to InspectorPackagerConnection reconnect and exception handling to connect Add exponential backoff to InspectorPackagerConnection reconnect and exception handling to connect (#56650) Apr 29, 2026
@meta-codesync meta-codesync Bot closed this in 32d3eaf Apr 30, 2026
@facebook-github-tools facebook-github-tools Bot added the Merged This PR has been merged. label Apr 30, 2026
@meta-codesync

meta-codesync Bot commented Apr 30, 2026

Copy link
Copy Markdown

This pull request has been merged in 32d3eaf.

@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @shubhamksavita in 32d3eaf

When will my fix make it into a release? | How to file a pick request?

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. fb-exported Merged This PR has been merged. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants