Skip to content

Add error handling in InspectorPackagerConnection::connect()#56713

Open
shubhamksavita wants to merge 1 commit intofacebook:mainfrom
shubhamksavita:export-D100956267
Open

Add error handling in InspectorPackagerConnection::connect()#56713
shubhamksavita wants to merge 1 commit intofacebook:mainfrom
shubhamksavita:export-D100956267

Conversation

@shubhamksavita
Copy link
Copy Markdown
Contributor

Summary:
Fix a SIGSEGV crash that can occur during WebSocket connection in the React Native JS inspector when JCxxInspectorPackagerConnectionDelegateImpl::connectWebSocket() fails (for example, due to allocation failure when constructing the WebSocket delegate hybrid object, or a JNI exception thrown from the Java side).

Previously InspectorPackagerConnection::Impl::connect() had no error handling around the connectWebSocket call — any exception (std::bad_alloc, JniException, etc.) would propagate unhandled and terminate the process.

Changes

  1. InspectorPackagerConnection.cpp: Wrap connectWebSocket in try-catch. On failure, log the error, reset the WebSocket, and trigger reconnect() (existing 2-second retry mechanism). This is consistent with how other error paths in the same class already handle failures (e.g., didFailWithError and didClose both call reconnect()).
  2. JCxxInspectorPackagerConnectionDelegateImpl.cpp: Add a null check on the JNI return value before calling wrapInUniquePtr() to prevent a null dereference if the Java method returns null.

Changelog:
[General][Fixed] - Handle exceptions thrown during WebSocket connect in InspectorPackagerConnection to avoid native crashes when the JS inspector fails to connect
[Android][Fixed] - Add null check on JNI return value in JCxxInspectorPackagerConnectionDelegateImpl::connectWebSocket to prevent null dereference

Differential Revision: D100956267

Summary:
Fix a SIGSEGV crash that can occur during WebSocket connection in the React Native JS inspector when `JCxxInspectorPackagerConnectionDelegateImpl::connectWebSocket()` fails (for example, due to allocation failure when constructing the WebSocket delegate hybrid object, or a JNI exception thrown from the Java side).

Previously `InspectorPackagerConnection::Impl::connect()` had no error handling around the `connectWebSocket` call — any exception (`std::bad_alloc`, `JniException`, etc.) would propagate unhandled and terminate the process.

### Changes
1. **`InspectorPackagerConnection.cpp`**: Wrap `connectWebSocket` in try-catch. On failure, log the error, reset the WebSocket, and trigger `reconnect()` (existing 2-second retry mechanism). This is consistent with how other error paths in the same class already handle failures (e.g., `didFailWithError` and `didClose` both call `reconnect()`).
2. **`JCxxInspectorPackagerConnectionDelegateImpl.cpp`**: Add a null check on the JNI return value before calling `wrapInUniquePtr()` to prevent a null dereference if the Java method returns null.

Changelog:
[General][Fixed] - Handle exceptions thrown during WebSocket connect in `InspectorPackagerConnection` to avoid native crashes when the JS inspector fails to connect
[Android][Fixed] - Add null check on JNI return value in `JCxxInspectorPackagerConnectionDelegateImpl::connectWebSocket` to prevent null dereference

Differential Revision: D100956267
@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 May 6, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented May 6, 2026

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

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 meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant