Skip to content

Commit fa05b8c

Browse files
shubhamksavitameta-codesync[bot]
authored andcommitted
Add error handling in InspectorPackagerConnection::connect() (#56713)
Summary: Pull Request resolved: #56713 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 Reviewed By: huntie Differential Revision: D100956267 fbshipit-source-id: e6e7ffa99c600ab437069a5344b4c13ad109eca3
1 parent 783f457 commit fa05b8c

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

packages/react-native/ReactAndroid/src/main/jni/react/devsupport/JCxxInspectorPackagerConnectionDelegateImpl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ JCxxInspectorPackagerConnectionDelegateImpl::connectWebSocket(
3030
"connectWebSocket");
3131
auto jWebSocket = method(
3232
self(), url, make_global(JWebSocketDelegate::newObjectCxxArgs(delegate)));
33+
if (!jWebSocket) {
34+
return nullptr;
35+
}
3336
return jWebSocket->wrapInUniquePtr();
3437
}
3538

packages/react-native/ReactCommon/jsinspector-modern/InspectorPackagerConnection.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,12 @@ void InspectorPackagerConnection::Impl::connect() {
341341
webSocket_ = delegate_->connectWebSocket(url_, weak_from_this());
342342
} catch (const std::exception& e) {
343343
LOG(ERROR) << "Failed to create WebSocket connection: " << e.what();
344-
reconnect();
344+
webSocket_.reset();
345345
} catch (...) {
346346
LOG(ERROR) << "Failed to create WebSocket connection: unknown error";
347+
webSocket_.reset();
348+
}
349+
if (!webSocket_ && !closed_) {
347350
reconnect();
348351
}
349352
}

0 commit comments

Comments
 (0)