Repository navigation
Report Hermes fatal errors on the New Architecture - #59729
Conversation
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
javache
left a comment
There was a problem hiding this comment.
Let's move this to the cross-platform HermesInstance::createJSRuntime path, and log using react_native_log_fatal
Moves the handler from the Android-only JHermesInstance to the cross-platform HermesInstance, and logs through react_native_log_fatal. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3b4bd0e to
ce3d9c9
Compare
|
TL;DR: This PR can merge as is. It doesn't make anything worse: Hermes fatal errors now report their reason through React Native's logger. On Android, though, that reason doesn't yet reliably reach logcat or the crash report. Testing turned up a problem with glog, which the logger uses on Android, and it affects more than Hermes. I've described it below with a proposed fix in #59861. What this PR doesThe Hermes fatal handler is installed once from I tested it with the reproducer from the issue on 0.88.1 (arm64 API 37 emulator). It triggers a Hermes fatal error by exhausting a second runtime with an 8 MB heap, so the reason to look for is Problem: glog is set up too late on the New Architectureglog reaches logcat only after its logcat sink is installed, in the In the reproducer, 3 of 3 runs of the stock template aborted with no log line from glog. With a transform on the root view, 3 of 3 runs logged the reason: Proposed fix: call Implemented in #59861. With it and this PR, 3 of 3 runs of the stock template log the reason as I also considered moving it from Guidance
|
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D130916024. |
## Summary: Part of #59728. Context in #59729 (comment) (problem 1). glog's logcat sink is installed in the `JNI_OnLoad` of `reactnativejni`. Since #50437, the New Architecture loads that library lazily: on the first transform prop or network inspector use, and on `main` also on the first touch event. Until then, glog writes only to stderr, so warnings and fatal errors logged during startup never reach logcat. Since Android 7.0, `liblog` sets the tombstone's abort message from fatal log writes, so those fatal errors also crash with a bare `SIGABRT`. This calls `gloginit::initialize()` from `rninstance`'s `JNI_OnLoad`. `ReactInstance` loads `rninstance` before the runtime, Fabric and TurboModules are created. `gloginit::initialize()` runs once per process, so the call in `reactnativejni` stays for the legacy architecture. ## Changelog: [ANDROID] [FIXED] - Send glog output during startup to logcat on the New Architecture ## Test Plan: Tested with [rn-hermes-fatal-handler-repro](https://github.com/kraenhansen/rn-hermes-fatal-handler-repro), which triggers a Hermes fatal error with the reason `Max heap size was exceeded`. I cherry-picked this commit onto 0.88.1, built `./gradlew :packages:react-native:ReactAndroid:assembleRelease`, swapped the arm64 libraries into the reproducer's stock-template APK, and ran it 3 times per build on an arm64 API 37 emulator. All runs were consistent. | Build | Crash | glog warnings in logcat | Fatal reason in logcat | Abort message in tombstone | | --- | --- | --- | --- | --- | | 0.88.1 | `SIGSEGV` | No | No | No | | 0.88.1 + this PR | `SIGSEGV` | Yes | No | No | | 0.88.1 + #59729 | `SIGABRT` | No | No | No | | 0.88.1 + #59729 + this PR | `SIGABRT` | Yes | Yes | Yes | With this PR and #59729: ```text W ReactNativeJNI: Codegen component descriptors were not configured from JNI_OnLoad W ReactNativeJNI: ReactInstance: evaluateJavaScript() with JS bundle F ReactNativeJNI: OOM: error_code(value = 1, category = vm_allocate_category, message = Max heap size was exceeded) F DEBUG : Abort message: 'OOM: error_code(value = 1, category = vm_allocate_category, message = Max heap size was exceeded)' ``` Nothing was logged twice, including with a transform on the root view, where `reactnativejni` also loads. I didn't run this on API levels below 37. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Differential Revision: D130915975 Pull Request resolved: #59861
Summary:
Part of #59728. The rest of it depends on the glog follow-up described in #59729 (comment).
hermesFatalHandler, which reports the reason for a Hermes fatal error, is only installed byHermesExecutorHolderon the legacy path. On the New Architecture nothing sets a handler, so Hermes writes the reason to stderr, which doesn't reach logcat, and crashes with its deliberate null write. The result is aSIGSEGVwith no reason.This installs a handler from
HermesInstance::createJSRuntime, once per process, on both platforms. It logs the reason throughreact_native_log_fatal, which aborts. It's kept separate from the handler inreactexecutor/OnLoad.cppso that the legacy code can be removed on its own.On Android,
react_native_log_fataluses glog, which only reaches logcat oncereactnativejnihas loaded. That's described, with a proposed follow-up, in #59729 (comment).Why the handler doesn't call
android_set_abort_messageOnce glog's logcat sink is installed, the tombstone's
Abort message:already carries the reason. The sink infirst-party/fbgloginit/glog_init.cppmapsGLOG_FATALtoANDROID_LOG_FATALand writes it with__android_log_print. Since Android 7.0 (API 24, React Native'sminSdk),liblog's__android_log_buf_writecallsandroid_set_abort_messagefor everyANDROID_LOG_FATALwrite (source). Neither this handler norglog_init.cppneeds to call it. On the API 37 emulator, this revision'slibreactnative.soandlibhermestooling.sodon't referenceandroid_set_abort_message, and the tombstone still shows the reason. A missing abort message means the sink wasn't installed, which is the follow-up in the comment linked above, not a missing call.Changelog:
[GENERAL] [FIXED] - Report the reason for Hermes fatal errors through the React Native logger on the New Architecture
Test Plan:
Used the reproducer from the issue, https://github.com/kraenhansen/rn-hermes-fatal-handler-repro, on an arm64 API 37 emulator. It's a stock 0.88.1 release app that exhausts a small-heap runtime 8 seconds after launch. The change was cherry-picked onto
v0.88.1,ReactAndroidwas built from source, and the resultinglibhermestooling.sowas swapped into the APK.Before, with stock 0.88.1:
After, with a
transformon the root view so thatreactnativejniloads, in 3 of 3 runs:With the stock template, 3 of 3 runs ended in
SIGABRTfrom glog, with noReactNativeJNIline and noAbort message:line. Nothing was run on iOS.Note that fatal errors now end in
SIGABRTrather thanSIGSEGV, which may change how they group in crash reporters. An app that sets its own Hermes fatal handler before the runtime is created would be replaced, because Hermes has a single handler and no getter.🤖 Generated with Claude Code