Skip to content

Commit 1593375

Browse files
authored
Initialize glog from rninstance on Android
## 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
1 parent cea863e commit 1593375

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ target_link_libraries(
3131
jsi
3232
jsitooling
3333
fbjni
34+
glog_init
3435
bridgeless
3536
)

‎packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/OnLoad.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
#include <fb/glog_init.h>
89
#include <fbjni/fbjni.h>
910
#include <react/jni/JReactMarker.h>
1011

1112
#include "JJSTimerExecutor.h"
1213
#include "JReactHostInspectorTarget.h"
1314
#include "JReactInstance.h"
1415

16+
#ifndef WITH_GLOGINIT
17+
#define WITH_GLOGINIT 1
18+
#endif
19+
1520
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* /*unused*/) {
1621
return facebook::jni::initialize(vm, [] {
22+
#if WITH_GLOGINIT
23+
facebook::gloginit::initialize();
24+
#endif
1725
facebook::react::JReactMarker::setLogPerfMarkerIfNeeded();
1826
facebook::react::JReactInstance::registerNatives();
1927
facebook::react::JJSTimerExecutor::registerNatives();

0 commit comments

Comments
 (0)