Commit 3188423
fix(android): Stop Hermes profiler on React instance teardown (#6035)
* fix(android): Stop Hermes sampling profiler on React instance teardown (#5441)
HermesSamplingProfiler.enable() starts a global timer thread that fires
pthread_kill(jsThread, SIGPROF) every ~10ms to sample stacks. Before this
change, nothing stopped it when the ReactInstance was torn down (Metro
reload, orderly Activity destroy, CodePush swap), so the sampler could
call pthread_kill on a pthread that was already joined by RN's teardown
sequence, resulting in SIGABRT in libhermes.so at
hermes::vm::sampling_profiler::Sampler::platformSuspendVMAndWalkStack.
Hermes's own Sampler::disable() synchronously joins the timer thread
before returning. Calling it from the module's invalidate() hook closes
the window before the JS thread is destroyed.
- Override invalidate() in both new-arch and old-arch wrappers to call
a cleanup hook on RNSentryModuleImpl.
- Defensive HermesSamplingProfiler.disable() before enable() in
startProfiling() to flush any leaked registration from a prior run.
- Track isProfiling as volatile and snatch the AndroidProfiler reference
locally in invalidate() to avoid racing with a concurrent stopProfiling
on the JS thread.
- Delete the android profiler trace file in invalidate() to avoid
leaking it in cacheDir when the profile is being discarded.
Upstream race: facebook/hermes#1853.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* Fix changelog
* fix(android): Address review: atomic state + startProfiling unwind + stopProfiling TOCTOU
Apply review feedback from Sentry Warden and Cursor Bugbot:
- Replace volatile fields with AtomicBoolean/AtomicReference so PMD's
AvoidUsingVolatile rule passes and to make the cross-thread gating
explicit. startProfiling/stopProfiling run on the JS thread; invalidate
runs on the NativeModules teardown thread.
- startProfiling now sets isProfiling=true immediately after
HermesSamplingProfiler.enable() succeeds, before androidProfiler.start().
The catch block unwinds via getAndSet(false) + disable() so we can't
leave the Hermes sampler running while the flag reads false.
- stopProfiling snapshots androidProfiler.get() into a local, closing the
TOCTOU where a concurrent invalidate() could null the field between
the null-check and endAndCollect().
- invalidate gates on isProfiling.getAndSet(false) and atomically snatches
the profiler reference with androidProfiler.getAndSet(null).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(android): Isolate Hermes disable from Android profiler cleanup in invalidate
Address two new review findings:
- High severity (Cursor Bugbot): the previous invalidate() shared a single
try/catch with endAndCollect(). If endAndCollect threw, the
HermesSamplingProfiler.disable() call on the line below was skipped,
leaving the sampler's timer thread alive — defeating the entire purpose
of invalidate(). Reorder so the crash-critical Hermes disable runs first
in its own try/catch; the Android platform profiler cleanup follows in
an independent try/catch.
- Medium severity (Cursor Bugbot / reviewer): stopProfiling could race
with invalidate on the same AndroidProfiler instance (both calling
endAndCollect concurrently). Gate stopProfiling on
isProfiling.compareAndSet(true, false) and take ownership of the
profiler reference via androidProfiler.getAndSet(null), mirroring
invalidate's pattern.
Also add two small idempotency tests that verify invalidate() is a no-op
on a fresh module without crossing the Hermes JNI boundary.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* Update changelog
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 4b4114f commit 3188423
5 files changed
Lines changed: 145 additions & 8 deletions
File tree
- packages/core
- RNSentryAndroidTester/app/src/test/java/io/sentry/react
- android/src
- main/java/io/sentry/react
- newarch/java/io/sentry/react
- oldarch/java/io/sentry/react
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
9 | 15 | | |
10 | 16 | | |
11 | 17 | | |
| |||
Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
Lines changed: 94 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
| 81 | + | |
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
| |||
114 | 116 | | |
115 | 117 | | |
116 | 118 | | |
117 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
118 | 128 | | |
119 | 129 | | |
120 | 130 | | |
| |||
773 | 783 | | |
774 | 784 | | |
775 | 785 | | |
776 | | - | |
| 786 | + | |
777 | 787 | | |
778 | 788 | | |
779 | 789 | | |
780 | 790 | | |
781 | 791 | | |
782 | | - | |
| 792 | + | |
783 | 793 | | |
784 | 794 | | |
785 | 795 | | |
786 | 796 | | |
787 | | - | |
| 797 | + | |
788 | 798 | | |
789 | 799 | | |
790 | 800 | | |
791 | 801 | | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
792 | 806 | | |
793 | | - | |
794 | | - | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
795 | 813 | | |
796 | 814 | | |
797 | 815 | | |
798 | 816 | | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
799 | 825 | | |
800 | 826 | | |
801 | 827 | | |
| |||
805 | 831 | | |
806 | 832 | | |
807 | 833 | | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
808 | 843 | | |
809 | 844 | | |
810 | 845 | | |
811 | | - | |
812 | | - | |
| 846 | + | |
| 847 | + | |
813 | 848 | | |
814 | 849 | | |
815 | 850 | | |
| |||
852 | 887 | | |
853 | 888 | | |
854 | 889 | | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
855 | 941 | | |
856 | 942 | | |
857 | 943 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
236 | 242 | | |
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
236 | 242 | | |
0 commit comments