Repository navigation
[Sentry] enable android profiling #84708
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
Merged
Julesssss
merged 17 commits into
Expensify:main
from
callstack-internal:eliran/sentry-enable-android-profiling
Mar 19, 2026
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
648bc8d
reenable android profiling
elirangoshen e460caf
lint fixes
elirangoshen c79749e
temporary allow sentry on dev
elirangoshen a713107
adding patches for hermes
elirangoshen e545d88
Add native patches to fix Hermes sampling profiler SIGABRT crash on A…
elirangoshen 4c96561
patch fix
elirangoshen 348c5a2
revert flag
elirangoshen 306217a
Merge branch 'main' into eliran/sentry-enable-android-profiling
elirangoshen bc85e89
patches
elirangoshen 1941cc5
revert ios sample rate
elirangoshen 6fb0a82
prettier fix
elirangoshen 77d2100
revert sample rate to 0.3
elirangoshen cad6b43
profiling comment revert
elirangoshen 564a221
revert to sample rate 1
elirangoshen f81201a
revert sample rate
elirangoshen 5e5e7d7
Merge branch 'main' into eliran/sentry-enable-android-profiling
elirangoshen 47af401
Rename patches from 0.81.4 to 0.83.1 after RN upgrade on main
elirangoshen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
patches/react-native/react-native+0.83.1+032+fix-hermes-sampling-profiler-disable.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), |
68 changes: 68 additions & 0 deletions
68
...eact-native/react-native+0.83.1+033+fix-hermes-sampling-profiler-pthread-kill-crash.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| + // 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` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attaching the fix only inside
doLastmeans it executes only whenunzipHermesactually runs; Gradle skips task actions when a task isUP-TO-DATE, so cached workspaces/CI agents that already extracted Hermes before this change can keep using an unpatchedSamplingProfilerPosix.cppand still hit the AndroidSIGABRTpath. This should either force rerun/invalidated outputs forunzipHermesor fail fast if the expected replacements are not present.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
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:
unzipHermesalways runsnpm installfreshens node_modules → extraction happensif (!content.contains("currentTid_"))guard makes it idempotent when it does run