Skip to content

Proposal changes for RCTTurboModule.mm crash handling logic #50076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule {

id<RCTBridgeModule> instance_;
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker_;

/**
* Sets custom exception handler to be called when NSException is caught through TurboModule method invocation.
* This method allows app owner to handle the exception on their side.
*/
static void setCustomNSExceptionHandler(
std::function<void(jsi::Runtime&, NSException*, const std::string&)> handler);

protected:
void setMethodArgConversionSelector(NSString *methodName, size_t argIndex, NSString *fnName);
Expand Down Expand Up @@ -166,6 +173,7 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule {

using PromiseInvocationBlock = void (^)(RCTPromiseResolveBlock resolveWrapper, RCTPromiseRejectBlock rejectWrapper);
jsi::Value createPromise(jsi::Runtime &runtime, std::string methodName, PromiseInvocationBlock invoke);
static std::function<void(jsi::Runtime&, NSException*, const std::string&)> nsExceptionHandler;
};

} // namespace facebook::react
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ id convertJSIValueToObjCObject(
/**
* Creates JSError with current JS runtime and NSException stack trace.
*/
static jsi::JSError convertNSExceptionToJSError(jsi::Runtime &runtime, NSException *exception)
static jsi::JSError convertNSExceptionToJSError(jsi::Runtime &runtime, NSException *exception, const std::string& message)
{
std::string reason = [exception.reason UTF8String];

Expand All @@ -223,6 +223,8 @@ id convertJSIValueToObjCObject(
cause.setProperty(runtime, "stackSymbols", convertNSArrayToJSIArray(runtime, exception.callStackSymbols));
cause.setProperty(
runtime, "stackReturnAddresses", convertNSArrayToJSIArray(runtime, exception.callStackReturnAddresses));
cause.setProperty(
runtime, "invokedFunction", message);

jsi::Value error = createJSRuntimeError(runtime, "Exception in HostFunction: " + reason);
error.asObject(runtime).setProperty(runtime, "cause", std::move(cause));
Expand Down Expand Up @@ -327,6 +329,20 @@ id convertJSIValueToObjCObject(
}));
}

std::function<void(jsi::Runtime&, NSException*, const std::string&)> ObjCTurboModule::nsExceptionHandler =
[](jsi::Runtime& runtime, NSException* ex, const std::string& message) {
// throwing custom std::exception causes untrecable crash reporting issue such as __pthread_kill
// and random crashes in hermes engine at this line.
// Approach can be to create JSError and invoke global.ErrorUtils.reportException on JS.
// Then rethrow caught NSException instead of throwing JSError.
throw convertNSExceptionToJSError(runtime, ex, message);
};

void ObjCTurboModule::setCustomNSExceptionHandler(
std::function<void(jsi::Runtime&, NSException*, const std::string&)> handler) {
ObjCTurboModule::nsExceptionHandler = handler;
}

/**
* Perform method invocation on a specific queue as configured by the module class.
* This serves as a backward-compatible support for RCTBridgeModule's methodQueue API.
Expand Down Expand Up @@ -364,7 +380,9 @@ id convertJSIValueToObjCObject(
@try {
[inv invokeWithTarget:strongModule];
} @catch (NSException *exception) {
throw convertNSExceptionToJSError(runtime, exception);
std::stringstream message;
message << moduleName << "::" << methodNameStr << " failed.";
nsExceptionHandler(runtime, exception, message.str());
} @finally {
[retainedObjectsForInvocation removeAllObjects];
}
Expand Down Expand Up @@ -427,7 +445,9 @@ TraceSection s(
@try {
[inv invokeWithTarget:strongModule];
} @catch (NSException *exception) {
throw convertNSExceptionToJSError(runtime, exception);
std::stringstream message;
message << "Failed TurboModule call: " << moduleName << " :: " << methodNameStr;
nsExceptionHandler(runtime, exception, message.str());
} @finally {
[retainedObjectsForInvocation removeAllObjects];
}
Expand Down
Loading