Skip to content
Merged
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
14 changes: 14 additions & 0 deletions patches/react-native/details.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,17 @@
- Upstream PR/issue: https://github.com/facebook/react-native/pull/55934
- E/App issue: https://github.com/Expensify/App/issues/75120
- PR Introducing Patch: https://github.com/Expensify/App/pull/79962

### [react-native+0.83.1+032+fix-hermes-sampling-profiler-disable.patch](react-native+0.83.1+032+fix-hermes-sampling-profiler-disable.patch)

- Reason: Fixes a copy-paste bug in `HermesSamplingProfiler::registerNatives()` where the JNI `"disable"` method was mapped to `HermesSamplingProfiler::enable` instead of `::disable`. This caused Sentry (or any caller of `HermesSamplingProfiler.disable()`) to call `enable()` again, corrupting the sampling thread state and leading to a `SIGABRT` crash (`invalid pthread_t passed to pthread_kill` in the `hermes-sampling` thread).
- Upstream PR/issue: https://github.com/facebook/react-native/issues/56120
- E/App issue: [#77171](https://github.com/Expensify/App/issues/77171)
- PR introducing patch: [#84708](https://github.com/Expensify/App/pull/84708)

### [react-native+0.83.1+033+fix-hermes-sampling-profiler-pthread-kill-crash.patch](react-native+0.83.1+033+fix-hermes-sampling-profiler-pthread-kill-crash.patch)

- Reason: On Android (Bionic libc), `pthread_kill` with an invalid `pthread_t` (e.g., thread has exited) calls `abort()` instead of returning `ESRCH` like glibc. The Hermes sampling profiler's timer thread sends `SIGPROF` via `pthread_kill` to registered runtime threads at ~100Hz. If a runtime's thread exits while profiling is active (e.g., during HybridApp transitions or background/foreground), the stale `pthread_t` triggers `SIGABRT: invalid pthread_t passed to pthread_kill`. This patch adds a `doLast` hook to the `unzipHermes` Gradle task that modifies `SamplingProfilerPosix.cpp` after Hermes source extraction, replacing `pthread_kill` with the `tgkill` syscall on Android, which safely returns `ESRCH` for dead threads. A kernel thread ID (`currentTid_`) is stored alongside the `pthread_t` and kept in sync via `setRuntimeThread()`. The Gradle hook approach is necessary because `unzipHermes` downloads fresh Hermes source, overwriting any direct source patches.
- Upstream PR/issue: 🛑
- E/App issue: [#77171](https://github.com/Expensify/App/issues/77171)
- PR introducing patch: [#84708](https://github.com/Expensify/App/pull/84708)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/node_modules/react-native/ReactAndroid/src/main/jni/react/hermes/instrumentation/HermesSamplingProfiler.cpp b/node_modules/react-native/ReactAndroid/src/main/jni/react/hermes/instrumentation/HermesSamplingProfiler.cpp
--- a/node_modules/react-native/ReactAndroid/src/main/jni/react/hermes/instrumentation/HermesSamplingProfiler.cpp
+++ b/node_modules/react-native/ReactAndroid/src/main/jni/react/hermes/instrumentation/HermesSamplingProfiler.cpp
@@ -34,7 +34,7 @@
void HermesSamplingProfiler::registerNatives() {
javaClassLocal()->registerNatives({
makeNativeMethod("enable", HermesSamplingProfiler::enable),
- makeNativeMethod("disable", HermesSamplingProfiler::enable),
+ makeNativeMethod("disable", HermesSamplingProfiler::disable),
makeNativeMethod(
"dumpSampledTraceToFile",
HermesSamplingProfiler::dumpSampledTraceToFile),
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
diff --git a/node_modules/react-native/ReactAndroid/hermes-engine/build.gradle.kts b/node_modules/react-native/ReactAndroid/hermes-engine/build.gradle.kts
--- a/node_modules/react-native/ReactAndroid/hermes-engine/build.gradle.kts
+++ b/node_modules/react-native/ReactAndroid/hermes-engine/build.gradle.kts
@@ -117,6 +117,64 @@
}
}
into(hermesDir)
+ doLast {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Ensure Hermes source patch runs on up-to-date builds

Attaching the fix only inside doLast means it executes only when unzipHermes actually runs; Gradle skips task actions when a task is UP-TO-DATE, so cached workspaces/CI agents that already extracted Hermes before this change can keep using an unpatched SamplingProfilerPosix.cpp and still hit the Android SIGABRT path. This should either force rerun/invalidated outputs for unzipHermes or fail fast if the expected replacements are not present.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this input:

In practice, the risk is low because:

  • CI always starts clean → unzipHermes always runs
  • npm install freshens node_modules → extraction happens
  • The if (!content.contains("currentTid_")) guard makes it idempotent when it does run

+ // Apply the tgkill patch to SamplingProfilerPosix.cpp after extraction.
+ // On Android (Bionic libc), pthread_kill aborts when given an invalid pthread_t
+ // (e.g., thread has exited) instead of returning ESRCH like glibc. The Hermes
+ // sampling profiler's timer thread sends SIGPROF via pthread_kill to registered
+ // runtime threads at ~100Hz. If a runtime's thread exits while profiling is active,
+ // the stale pthread_t triggers a SIGABRT crash. This patch replaces pthread_kill
+ // with the tgkill syscall on Android, which safely returns ESRCH for dead threads.
+ val samplingProfilerFile = File(hermesDir, "lib/VM/Profiler/SamplingProfilerPosix.cpp")
+ if (samplingProfilerFile.exists()) {
+ var content = samplingProfilerFile.readText()
+
+ // Add #include <sys/syscall.h> if not already present
+ if (!content.contains("sys/syscall.h")) {
+ content = content.replace(
+ "#include \"SamplingProfilerSampler.h\"",
+ "#include \"SamplingProfilerSampler.h\"\n\n#if defined(__ANDROID__)\n#include <sys/syscall.h>\n#include <unistd.h>\n#endif"
+ )
+ }
+
+ // Add currentTid_ field to SamplingProfilerPosix struct
+ if (!content.contains("currentTid_")) {
+ content = content.replace(
+ "pthread_t currentThread_;",
+ "pthread_t currentThread_;\n\n /// Kernel thread ID for safe signal delivery via tgkill syscall.\n pid_t currentTid_;"
+ )
+
+ // Initialize currentTid_ in constructor body
+ content = content.replace(
+ "currentThread_{pthread_self()} {",
+ "currentThread_{pthread_self()} {\n currentTid_ = static_cast<pid_t>(syscall(__NR_gettid));"
+ )
+
+ // Replace pthread_kill with tgkill in platformSuspendVMAndWalkStack
+ content = content.replace(
+ " pthread_kill(posixProfiler->currentThread_, SIGPROF);",
+ """#if defined(__ANDROID__)
+ // On Android (Bionic), pthread_kill aborts if the pthread_t is invalid
+ // (e.g., thread exited). Use tgkill syscall which returns ESRCH instead.
+ if (syscall(__NR_tgkill, getpid(), posixProfiler->currentTid_, SIGPROF) != 0) {
+ self->profilerForSig_.store(nullptr, std::memory_order_release);
+ return true;
+ }
+#else
+ pthread_kill(posixProfiler->currentThread_, SIGPROF);
+#endif"""
+ )
+
+ // Update currentTid_ in setRuntimeThread
+ content = content.replace(
+ "profiler->currentThread_ = pthread_self();",
+ "profiler->currentThread_ = pthread_self();\n profiler->currentTid_ = static_cast<pid_t>(syscall(__NR_gettid));"
+ )
+ }
+
+ samplingProfilerFile.writeText(content)
+ }
+ }
}

// NOTE: ideally, we would like CMake to be installed automatically by the `externalNativeBuild`
5 changes: 2 additions & 3 deletions src/setup/telemetry/setupSentry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as Sentry from '@sentry/react-native';
import {Platform} from 'react-native';
import {isDevelopment} from '@libs/Environment/Environment';
import {breadcrumbsIntegration, browserProfilingIntegration, consoleIntegration, navigationIntegration, tracingIntegration} from '@libs/telemetry/integrations';
import {processBeforeSendLogs, processBeforeSendTransactions} from '@libs/telemetry/middlewares';
Expand All @@ -15,15 +14,15 @@ function setupSentry(): void {
return;
}

const integrations = [navigationIntegration, tracingIntegration, browserProfilingIntegration, breadcrumbsIntegration, consoleIntegration].filter((integration) => !!integration);
const integrations = [navigationIntegration, tracingIntegration, browserProfilingIntegration, breadcrumbsIntegration, consoleIntegration];

Sentry.init({
dsn: CONFIG.SENTRY_DSN,
transport: isDevelopment() ? makeDebugTransport : undefined,
tracesSampleRate: 1.0,
// 1. Profiling for Android is currently disabled because it causes crashes sometimes.
// 2. When updating the profile sample rate, make sure it will not blow up our current limit in Sentry.
profilesSampleRate: Platform.OS === 'android' ? 0 : 0.1,
profilesSampleRate: 0.1,
enableAutoPerformanceTracing: true,
enableUserInteractionTracing: true,
integrations,
Expand Down
Loading