Repository navigation
Conversation
Replace the PR link placeholder in the Android JS hang tracking entry with #7111.
Semver Impact of This PR⚪ None (no version bump detected) 📋 Changelog PreviewThis is how your changes will appear in the changelog.
🤖 This preview updates automatically when you update the PR. |
|
Review feedback on #7111. sample() cleared its enabled flag before it called disable(), so when disable() threw, the finally block skipped the best-effort disable and the global Hermes sampler could stay on. A JS hang sample does not set isProfiling, so invalidate() would not turn it off later either. The flag is now cleared only after disable() returns. A new test uses a fake profiler whose first disable() throws and checks that the sampler tries to disable it again.
antonis
left a comment
There was a problem hiding this comment.
Left a couple of comments. Let's also update the PR description and add Sentry test links.
Replace the PR link placeholder in the Android JS hang tracking entry with #7111.
84303b8 to
b0582dc
Compare
Review feedback on #7111. sample() cleared its enabled flag before it called disable(), so when disable() threw, the finally block skipped the best-effort disable and the global Hermes sampler could stay on. A JS hang sample does not set isProfiling, so invalidate() would not turn it off later either. The flag is now cleared only after disable() returns. A new test uses a fake profiler whose first disable() throws and checks that the sampler tries to disable it again.
Review feedback on #7111. While a Hermes profile ran, sample() returned null, so the JS hang event had no stack and grouped into its own issue. With profiling on, a hang soon after launch often lands inside the app start profile. Android now does what iOS does. While a profile runs, sample() does not turn the sampler off. It dumps the running profile, keeps the dump, and returns it as a profile trace, so the tracker takes the newest JS stack of the dump. Hermes clears its samples on every dump, so stopProfiling() merges the kept dumps with the final one in time order and moves the stack frame ids of each dump past the ids of the dumps before it. When a dump cannot be merged, the profile keeps the final dump. Starting or discarding a profile drops the kept dumps. On an Android emulator with profilesSampleRate 1.0, a hang inside the profile gave a JS Thread Hang event with the stack down to onPress, and the profile kept the hang samples.
Review feedback on #7111: keep only the first line of the entry.
8.37.0 was released while #7111 was open, so the rebase left the entry in the released section.
📲 Install BuildsAndroid
|
|
Thanks for the profiling fix. I verified it on device: hangs during a profile now keep their stack, and the merged profile is complete. I also tested the reload-crash part, and it doesn't hold on RN 0.88 New Architecture. On Android that's the only architecture for RN ≥ 0.82.
// ReactInstance.kt (RN 0.88.1)
fun destroy() {
reactQueueConfiguration.destroy() // jsQueueThread.quitSynchronous(): quit + join
turboModuleManager.invalidate() // RNSentryModuleImpl.invalidate() runs here
I logged the state at the top of Reload loop: 80 reloads per build, 1.5 s apart, triggered from
The crash already exists on Suggestion: Since the same fix also covers the existing profiling crash, it could go in a small PR that merges first, with this one rebased on top. Wdyt? |
|
@antonis thanks a lot for digging into this, you're right. I checked |
antonis
left a comment
There was a problem hiding this comment.
@antonis thanks a lot for digging into this, you're right. I checked
ReactInstance.destroy()in 0.88.1: the JS queue is quit and joined beforeturboModuleManager.invalidate()soinvalidate()is too late on the New Architecture, and my reload loop didn't cover it (it ran in a debug build where the tracker is off).addBeforeDestroyListenerexists since RN 0.73. I'll do the fix in a separate PR offmainwith your reload loop as the check, and rebase this one on top.
Thank you 🙇 Let's iterate once the separate PR is merged 👍
Replace the PR link placeholder in the Android JS hang tracking entry with #7111.
Review feedback on #7111. sample() cleared its enabled flag before it called disable(), so when disable() threw, the finally block skipped the best-effort disable and the global Hermes sampler could stay on. A JS hang sample does not set isProfiling, so invalidate() would not turn it off later either. The flag is now cleared only after disable() returns. A new test uses a fake profiler whose first disable() throws and checks that the sampler tries to disable it again.
b0582dc to
d05f278
Compare
Review feedback on #7111. While a Hermes profile ran, sample() returned null, so the JS hang event had no stack and grouped into its own issue. With profiling on, a hang soon after launch often lands inside the app start profile. Android now does what iOS does. While a profile runs, sample() does not turn the sampler off. It dumps the running profile, keeps the dump, and returns it as a profile trace, so the tracker takes the newest JS stack of the dump. Hermes clears its samples on every dump, so stopProfiling() merges the kept dumps with the final one in time order and moves the stack frame ids of each dump past the ids of the dumps before it. When a dump cannot be merged, the profile keeps the final dump. Starting or discarding a profile drops the kept dumps. On an Android emulator with profilesSampleRate 1.0, a hang inside the profile gave a JS Thread Hang event with the stack down to onPress, and the profile kept the hang samples.
Review feedback on #7111: keep only the first line of the entry.
8.37.0 was released while #7111 was open, so the rebase left the entry in the released section.
…is destroyed Review feedback on #7111. On the New Architecture, the before destroy listener from #7157 runs invalidate(), which stops the JS hang tracker while the JS thread still runs. A start that came in after that cleanup would leave the tracker, and with it the Hermes sampler, running while the JS thread exits. startJsHangTracking now does nothing after the cleanup, and stops the tracker again when the cleanup ran between the check and the start. With reactHost.reload() every 1.5 s in a release build with profilesSampleRate 1.0, 600 reloads gave no abort on the hermes-sampling thread.
PR risk: mediumFor commit Main concerns: data sent to Sentry, public contract, host performance. This label is a hint for reviewers, not an approval. |
enableJsHangTracking worked only on iOS. On Android, an infinite loop in JS still sent no event, because ANR detection watches only the main thread. RNSentryJSHangTracker uses the same design as iOS. A native thread posts an empty runnable to the JS thread with runOnJSQueueThread every 100 ms, and measures only after the first answer, so bundle loading is not a hang. It pauses in the background and while a debugger is attached. When the runnable does not run within the timeout, it samples the JS thread with the Hermes sampling profiler for 300 ms and captures a handled JS Thread Hang error event, with the oldest JS stack of the sample and the bundle Debug ID for source maps. The capture runs on its own thread, so the tracker thread never waits for disk I/O. The iOS tracker was not ported earlier because a reload often crashed in the Hermes sampler thread (meta/hermes#1853). Hermes fixed the cause on main, where the sampler calls pthread_kill on a JS thread that already exited, but the fix is not in the Hermes release that React Native 0.88 and 0.89 ship. So the SDK keeps the sampler off before the JS thread exits. invalidate() stops the tracker first, and stop() interrupts a running sample and waits until the tracker thread ends. RNSentryHermesSampler now holds the sampler for profiling and for JS hang samples under one lock, so a profile cannot start in the middle of a sample, and stopping a profile turns the sampler off before it creates the trace file. The JS hang flame graph stays iOS only. Robolectric tests cover the tracker with a fake JS thread and sampler, the shared sampler with a fake Hermes profiler, and the module start, stop, and invalidate paths.
Replace the PR link placeholder in the Android JS hang tracking entry with #7111.
Review feedback on #7111. sample() cleared its enabled flag before it called disable(), so when disable() threw, the finally block skipped the best-effort disable and the global Hermes sampler could stay on. A JS hang sample does not set isProfiling, so invalidate() would not turn it off later either. The flag is now cleared only after disable() returns. A new test uses a fake profiler whose first disable() throws and checks that the sampler tries to disable it again.
Review feedback on #7111. While a Hermes profile ran, sample() returned null, so the JS hang event had no stack and grouped into its own issue. With profiling on, a hang soon after launch often lands inside the app start profile. Android now does what iOS does. While a profile runs, sample() does not turn the sampler off. It dumps the running profile, keeps the dump, and returns it as a profile trace, so the tracker takes the newest JS stack of the dump. Hermes clears its samples on every dump, so stopProfiling() merges the kept dumps with the final one in time order and moves the stack frame ids of each dump past the ids of the dumps before it. When a dump cannot be merged, the profile keeps the final dump. Starting or discarding a profile drops the kept dumps. On an Android emulator with profilesSampleRate 1.0, a hang inside the profile gave a JS Thread Hang event with the stack down to onPress, and the profile kept the hang samples.
Review feedback on #7111: keep only the first line of the entry.
8.37.0 was released while #7111 was open, so the rebase left the entry in the released section.
…is destroyed Review feedback on #7111. On the New Architecture, the before destroy listener from #7157 runs invalidate(), which stops the JS hang tracker while the JS thread still runs. A start that came in after that cleanup would leave the tracker, and with it the Hermes sampler, running while the JS thread exits. startJsHangTracking now does nothing after the cleanup, and stops the tracker again when the cleanup ran between the check and the start. With reactHost.reload() every 1.5 s in a release build with profilesSampleRate 1.0, 600 reloads gave no abort on the hermes-sampling thread.
d05f278 to
8f7b837
Compare
The Android JS hang tracking change added 27 tests, many of them for small mapping details or for cases that other tests already cover. The suite now keeps 15: the hang report and its false positive guards, the tracker stop during a sample, the stack selection and frame mapping, the event, the Hermes sampler safety around disable, profiles, and the lock, and the module start, timeout, and invalidate paths. The invalidate test now also checks that the tracker runs before invalidate(), so it cannot pass when the tracker never started.
📢 Type of change
📜 Description
enableJsHangTrackingnow also works on Android. A native thread pings the JS thread withrunOnJSQueueThreadevery 100 ms. When the JS thread does not answer withinjsHangTimeoutInterval, the tracker samples it with the Hermes sampling profiler for 300 ms and captures a handledJS Thread Hangevent with the JS stack and the bundle Debug ID, the same as on iOS. The JS hang flame graph stays iOS only.RNSentryHermesSamplerholds the Hermes sampler for profiling and for JS hang samples under one lock, so a profile cannot start in the middle of a sample.stopProfilingmerges these dumps back into the profile, the same as iOS (RNSentryHermesSampler.mm).invalidate()stops the tracker before anything else. On the New Architecture, the before destroy listener from fix(android): Stop the Hermes sampler before the JS thread exits on reload #7157 runsinvalidate()while the JS thread still runs, so the tracker and its sampler are off before the JS thread exits. The tracker does not start again after that cleanup.💡 Motivation and Context
Closes #6985, follow-up to #6964. The Android tracker was removed from #6964 because a reload could crash in the Hermes sampler thread (meta/hermes#1853, #5441). Hermes fixed the cause in meta/hermes@2751477d3c (the sampler calls
pthread_killon a JS thread that already exited), but the fix is not in the Hermes release that React Native 0.88 and 0.89 ship, so the SDK turns the sampler off before the JS thread exits. On the New Architecture,invalidate()alone runs too late (found by @antonis); #7157 adds the before destroy cleanup.💚 How did you test it?
testDebugUnitTestpasses (189 tests, 15 of them new).yarn build,yarn test,yarn lint, andyarn circularDepCheckpass.profilesSampleRate: 1.0, nested JS hang button:onPress: event. The profile of that run kept the hang samples (1081 JS samples over 17.6 s, valid stack ids).profilesSampleRate: 1.0, JS hang tracking on,reactHost.reload()fromadbevery 1.5 s. 600 reloads, no abort onhermes-sampling(@antonis saw 8 in 63 reloads on the previous version). One run aborted onmqt_v_jswith the sample's ReanimatedinstallTurboModuleassertion during the bundle start, which is not related to the sampler.📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps