Skip to content

Commit a05d179

Browse files
Riley Dulinkelset
authored andcommitted
Add a fatal error handler for Hermes
Summary: Hermes has a way to set up a callback that is invoked when a fatal error such as Out of Memory occurs. It is a static API that should be called at most once, so it uses `std::call_once` to achieve that. The fatal error handler is simple, it just uses glog to log an error message to logcat, then aborts (using `__android_log_assert`). The reason is typically very helpful for understanding why `hermes_fatal` was called. Changelog: [Android][Internal] - Print a logcat message when Hermes has a fatal error Reviewed By: mhorowitz Differential Revision: D25792805 fbshipit-source-id: 45de70d71e9bd8eaa880526d8835b4e32aab8fe3
1 parent a6a4d33 commit a05d179

File tree

1 file changed

+15
-0
lines changed
  • ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor

1 file changed

+15
-0
lines changed

ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/OnLoad.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
#include <../instrumentation/HermesMemoryDumper.h>
99
#include <HermesExecutorFactory.h>
10+
#include <android/log.h>
1011
#include <fbjni/fbjni.h>
12+
#include <glog/logging.h>
1113
#include <hermes/Public/GCConfig.h>
1214
#include <hermes/Public/RuntimeConfig.h>
1315
#include <jni.h>
@@ -21,6 +23,13 @@
2123
namespace facebook {
2224
namespace react {
2325

26+
static void hermesFatalHandler(const std::string &reason) {
27+
LOG(ERROR) << "Hermes Fatal: " << reason << "\n";
28+
__android_log_assert(nullptr, "Hermes", "%s", reason.c_str());
29+
}
30+
31+
static std::once_flag flag;
32+
2433
static ::hermes::vm::RuntimeConfig makeRuntimeConfig(jlong heapSizeMB) {
2534
namespace vm = ::hermes::vm;
2635
auto gcConfigBuilder =
@@ -62,6 +71,9 @@ class HermesExecutorHolder
6271
jni::alias_ref<jclass>) {
6372
JReactMarker::setLogPerfMarkerIfNeeded();
6473

74+
std::call_once(flag, []() {
75+
facebook::hermes::HermesRuntime::setFatalHandler(hermesFatalHandler);
76+
});
6577
return makeCxxInstance(
6678
std::make_unique<HermesExecutorFactory>(installBindings));
6779
}
@@ -71,6 +83,9 @@ class HermesExecutorHolder
7183
jlong heapSizeMB) {
7284
JReactMarker::setLogPerfMarkerIfNeeded();
7385
auto runtimeConfig = makeRuntimeConfig(heapSizeMB);
86+
std::call_once(flag, []() {
87+
facebook::hermes::HermesRuntime::setFatalHandler(hermesFatalHandler);
88+
});
7489
return makeCxxInstance(std::make_unique<HermesExecutorFactory>(
7590
installBindings, JSIExecutor::defaultTimeoutInvoker, runtimeConfig));
7691
}

0 commit comments

Comments
 (0)