Skip to content

fix(android): Stop the Hermes sampler before the JS thread exits on reload - #7157

Merged
alwx merged 2 commits into
mainfrom
alwx/bugfix/5441-before-destroy
Dec 1, 2026
Merged

alwx merged 2 commits into
mainfrom
alwx/bugfix/5441-before-destroy

Conversation

@alwx

@alwx alwx commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

📢 Type of change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring

📜 Description

On bridgeless Android apps (React Native 0.73+), the module now runs its Hermes sampler cleanup from ReactHost.addBeforeDestroyListener, which runs on reload and destroy while the JS thread still runs. invalidate() stays as the cleanup on the bridge and unregisters the listener. A profile cannot start after the cleanup.

The SDK compiles against the app's React Native version, so the listener reaches the React host through reflection. R8 removed addBeforeDestroyListener from minified release builds, so the module now ships consumer R8 rules (android/proguard-rules.pro) that keep these members.

💡 Motivation and Context

Follow-up to #5441, found by @antonis in #7111. The #5441 fix disables the sampler in invalidate(), but bridgeless ReactInstance.destroy() quits and joins the JS thread before it invalidates the native modules (ReactInstance.kt#L419-L422), so a reload during a profile can still abort with invalid pthread_t passed to pthread_kill on hermes-sampling (meta/hermes#1853). #7111 will be rebased on top of this.

💚 How did you test it?

  • Robolectric tests for the listener: registers only bridgeless apps with a React host, runs the action on destroy, unregisters (also from inside the listener), and never lets the action throw into React Native. testDebugUnitTest passes (174 tests).

  • yarn build, yarn test, yarn lint, and yarn circularDepCheck pass.

  • Reload loop on an Android 16 emulator: release arm64 build of the sample, profilesSampleRate: 1.0, reactHost.reload() from adb every 1.5 s through a temporary receiver.

    Build Reloads SIGABRT on hermes-sampling
    main 230 2
    this PR 300 0

📝 Checklist

  • I added tests to verify changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • All tests passing.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.
  • No breaking changes.

alwx added 2 commits December 1, 2026 11:37
…eload

A reload during a Hermes profile could abort the app with `invalid pthread_t passed to pthread_kill` on the hermes-sampling thread (#5441, meta/hermes#1853). The fix from #5441 disables the sampler in invalidate(), but bridgeless ReactInstance.destroy() quits and joins the JS thread before it invalidates the native modules, so on the New Architecture invalidate() runs too late.

ReactHost.addBeforeDestroyListener, from React Native 0.73, runs on reload and on destroy while the JS thread still runs. In bridgeless apps the module now registers a listener there that runs the invalidate() cleanup. invalidate() stays as the cleanup on the bridge, where it runs before the JS thread exits, and unregisters the listener, so the long-lived React host does not keep a destroyed module. A profile cannot start after the cleanup, also when invalidate() runs between the check and the start.

The SDK compiles against the app's React Native version, so the listener reaches the React host through reflection. R8 removed addBeforeDestroyListener from minified release builds, because React Native does not call it, so consumer R8 rules keep these members.

On an Android emulator with a release build, profilesSampleRate 1.0, and reactHost.reload() every 1.5 s, main aborted 2 times in 230 reloads, and this change aborted 0 times in 300 reloads.
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

⚪ None (no version bump detected)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


  • fix(android): Stop the Hermes sampler before the JS thread exits on reload by alwx in #7157
  • chore(deps): bump cross-spawn from 7.0.6 to 7.1.2 by antonis and claude with dependabot in #7156
  • fix(core): Keep Hermes InternalBytecode.js frames out of app by antonis and claude in #7138
  • ci: Add the PR classifiers workflow by christophaigner with sentry-junior in #7142
  • chore(deps): bump getsentry/github-workflows/danger from bce11fe6eaa86dbb9d360ab45a96e659828877f0 to edc87d6f133ac6204e22dee5123167a5eff3ed79 by dependabot in #7137
  • chore(deps): bump ruby/setup-ruby from 1.342.0 to 1.345.0 by dependabot in #7136
  • chore(deps): bump getsentry/craft from 3.13.0 to 3.14.1 by dependabot in #7135
  • chore(deps): bump getsentry/craft/.github/workflows/changelog-preview.yml from 3.13.0 to 3.14.1 by dependabot in #7134
  • chore(deps): bump the codeql-action group with 3 updates by dependabot in #7133
  • chore(deps): update Sentry Android Gradle Plugin to v6.28.0 by github-actions in #7129
  • feat(tracing): Report long tasks as ui.long-task spans in transactions by alwx in #7110
  • chore(deps): update Maestro to v2.13.0 by github-actions in #7126
  • chore(tooling): Make the verify-pr gate stop at the first no by antonis and claude with sentry-junior in #7125
  • chore(tooling): Add review checks and a gate-only mode to verify-pr by antonis and claude in #7121
  • chore(tooling): Fold the open-PR run findings into verify-pr by antonis and claude in #7115

🤖 This preview updates automatically when you update the PR.

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit a1d4747.

Main concerns: host performance, public contract, reach.

Rules that block low also fired:

  • R5: dependencies or bundled SDK versions changed: packages/core/android/build.gradle
  • R6: build or release configuration changed: packages/core/android/build.gradle

This label is a hint for reviewers, not an approval.

@alwx
alwx marked this pull request as ready for review December 1, 2026 10:49
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Dec 1, 2026
alwx added a commit that referenced this pull request Dec 1, 2026
…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.
@antonis antonis added the ready-to-merge Triggers the full CI test suite label Dec 1, 2026
@sentry

sentry Bot commented Dec 1, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
Sentry RN io.sentry.reactnative.sample 8.37.0 (117) Release

⚙️ sentry-react-native Build Distribution Settings

@antonis antonis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM once the CI is 🟢 🚀
Thanks for the quick turnaround.

@alwx
alwx merged commit 93cf63b into main Dec 1, 2026
116 of 122 checks passed
@alwx
alwx deleted the alwx/bugfix/5441-before-destroy branch December 1, 2026 12:47
alwx added a commit that referenced this pull request Dec 1, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Triggers the full CI test suite risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants