Skip to content

fix(gestures): Replace GestureDetectorCompat with lightweight detector to fix ANR#5138

Open
romtsn wants to merge 7 commits intomainfrom
rz/fix/anr-gesture-detector
Open

fix(gestures): Replace GestureDetectorCompat with lightweight detector to fix ANR#5138
romtsn wants to merge 7 commits intomainfrom
rz/fix/anr-gesture-detector

Conversation

@romtsn
Copy link
Copy Markdown
Member

@romtsn romtsn commented Mar 2, 2026

Summary

  • Replace GestureDetectorCompat with a custom lightweight SentryGestureDetector in SentryWindowCallback to fix ANR SDK-CRASHES-JAVA-596 (175K+ occurrences, 574 users)
  • GestureDetectorCompat internally uses Handler.sendMessage/removeMessages which acquires a synchronized lock on the main thread's MessageQueue, plus recordGestureClassification() triggers IPC/binder calls — both cause contention under load
  • The new detector only implements click, scroll, and fling detection (the only gestures SentryGestureListener uses), eliminating all Handler scheduling, MessageQueue lock contention, and IPC overhead

Test plan

  • Added SentryGestureDetectorTest with 7 test cases: tap, no-tap, scroll with deltas, fling, slow release, cancel cleanup, sequential gesture reset
  • Existing SentryWindowCallbackTest (4 tests) passes
  • Existing SentryGestureListenerTest suite passes
  • ./gradlew :sentry-android-core:testDebugUnitTest passes
  • Manual testing on Android device

🤖 Generated with Claude Code

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 2, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

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


Bug Fixes 🐛

  • (gestures) Replace GestureDetectorCompat with lightweight detector to fix ANR by romtsn in #5138

Internal Changes 🔧

Deps

  • Update Gradle to v9.4.1 by github-actions in #5063
  • Bump actions/github-script from 8.0.0 to 9.0.0 by dependabot in #5285
  • Bump actions/create-github-app-token from 3.0.0 to 3.1.1 by dependabot in #5287
  • Bump actions/upload-artifact from 7.0.0 to 7.0.1 by dependabot in #5286
  • Update Native SDK to v0.13.6 by github-actions in #5277

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 2, 2026

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 312.65 ms 356.14 ms 43.49 ms
Size 0 B 0 B 0 B

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
d15471f 310.66 ms 368.19 ms 57.53 ms
9054d65 330.94 ms 403.24 ms 72.30 ms
b77456b 393.26 ms 441.10 ms 47.84 ms
96449e8 361.30 ms 423.39 ms 62.09 ms
ee747ae 357.79 ms 421.84 ms 64.05 ms
a416a65 295.53 ms 373.74 ms 78.21 ms
ab8a72d 316.24 ms 356.38 ms 40.14 ms
1564554 323.06 ms 336.68 ms 13.62 ms
b3d8889 371.69 ms 432.96 ms 61.26 ms
d15471f 315.20 ms 370.22 ms 55.02 ms

App size

Revision Plain With Sentry Diff
d15471f 1.58 MiB 2.13 MiB 559.54 KiB
9054d65 1.58 MiB 2.29 MiB 723.38 KiB
b77456b 1.58 MiB 2.12 MiB 548.11 KiB
96449e8 1.58 MiB 2.11 MiB 539.35 KiB
ee747ae 1.58 MiB 2.10 MiB 530.95 KiB
a416a65 1.58 MiB 2.12 MiB 555.26 KiB
ab8a72d 1.58 MiB 2.12 MiB 551.55 KiB
1564554 1.58 MiB 2.20 MiB 635.33 KiB
b3d8889 1.58 MiB 2.10 MiB 535.06 KiB
d15471f 1.58 MiB 2.13 MiB 559.54 KiB

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

@sentry
Copy link
Copy Markdown

sentry bot commented Mar 11, 2026

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.38.0 (1) release

⚙️ sentry-android Build Distribution Settings

romtsn and others added 7 commits April 15, 2026 12:23
…stureDetector to fix ANR

GestureDetectorCompat internally uses Handler.sendMessage/removeMessages which
acquires a synchronized lock on the main thread MessageQueue, plus
recordGestureClassification triggers IPC calls. This caused ANRs under load
(SDK-CRASHES-JAVA-596, 175K+ occurrences).

Replace with a minimal custom detector that only detects click, scroll, and
fling without any Handler scheduling, MessageQueue contention, or IPC overhead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…velocity data

Matches GestureDetector behavior: if consecutive ACTION_DOWN events
arrive without an intervening ACTION_UP/ACTION_CANCEL, stale motion
data could bleed into fling detection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…check

UserInteractionIntegration gated itself on GestureDetectorCompat being
available via classloader check, but SentryGestureDetector only uses
Android SDK classes. Remove the check so the integration works without
androidx.core. Also remove the stale proguard -keep rule.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Keep VelocityTracker alive across gesture cycles instead of
obtain/recycle churn on every gesture. Add release() method called
from SentryWindowCallback.stopTracking() to prevent native resource
leaks when activity is destroyed mid-gesture. Also fix broken Javadoc
@link to removed dependency, change onTouchEvent return to void, and
remove redundant null check.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@romtsn romtsn force-pushed the rz/fix/anr-gesture-detector branch from 1a7624e to e9db3cb Compare April 15, 2026 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants