Add exponential backoff to InspectorPackagerConnection reconnect and exception handling to connect (#56650)#56650
Closed
shubhamksavita wants to merge 1 commit into
Closed
Conversation
|
@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
b740f53 to
8eca26f
Compare
|
This pull request has been merged in 32d3eaf. |
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? |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
Summary
Fix occasional SIGSEGV in
operator newduringInspectorPackagerConnection::connectWebSocket().Root Cause
The
InspectorPackagerConnectionreconnection 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 inoperator newwhen the allocator follows corrupted internal pointers.Additionally, the
connect()method had no exception handling around theconnectWebSocket()call, meaning any JNI/Java exception would propagate uncaught.Changes
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.connect(): Catch exceptions fromconnectWebSocket()(e.g., fbjni/JNI exceptions) and gracefully trigger a reconnect instead of crashing.didOpen()fires (successful connection), reset the delay back to 2s and clearsuppressConnectionErrors_.Changelog:
[General][Fixed] - Add exponential backoff and exception handling to InspectorPackagerConnection reconnect loop to prevent heap fragmentation crashes
Reviewed By: cipolleschi
Differential Revision: D100956687