Skip to content

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

Merged
romtsn merged 13 commits intomainfrom
rz/fix/anr-gesture-detector
Apr 16, 2026
Merged

fix(gestures): Replace GestureDetectorCompat with lightweight detector to fix ANR#5138
romtsn merged 13 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 412.58 ms 464.67 ms 52.09 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

Previous results on branch: rz/fix/anr-gesture-detector

Startup times

Revision Plain With Sentry Diff
85a0af4 312.65 ms 356.14 ms 43.49 ms
d7d285a 324.00 ms 357.96 ms 33.96 ms
fdd395a 304.87 ms 340.11 ms 35.24 ms

App size

Revision Plain With Sentry Diff
85a0af4 0 B 0 B 0 B
d7d285a 0 B 0 B 0 B
fdd395a 0 B 0 B 0 B

@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
romtsn and others added 3 commits April 15, 2026 21:08
When a second finger touches the screen (ACTION_POINTER_DOWN), cancel
tap detection by setting isInTapRegion to false. Previously, pinch-to-zoom
gestures where the first finger stayed still would incorrectly trigger
onSingleTapUp, producing false click breadcrumbs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add local btrace-perfetto skill for capturing and comparing Perfetto
traces on Android devices using btrace 3.0.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Workflow skill that automates capturing and comparing Perfetto traces
using btrace 3.0 on Android devices. Includes a Perfetto UI viewer
template with SQL query deep-linking via postMessage API.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

Fix All in Cursor

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

Reviewed by Cursor Bugbot for commit 76e9b72. Configure here.

romtsn and others added 2 commits April 16, 2026 13:06
Use release builds for accurate profiling, add ProGuard keep rules
for btrace and Sentry class names, increase trace duration to 30s,
and play a Ping sound synced to the actual trace start.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…rison

Prefer debug builds for richer tracing instrumentation (Handler,
MessageQueue, Lock slices). Add trace_processor prerequisite for local
querying. Add Step 6 with SQL queries and comparison table generation.
Include sampling rate reference and additional troubleshooting entries.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@romtsn
Copy link
Copy Markdown
Member Author

romtsn commented Apr 16, 2026

Ran some tests with perfetto in the sample app and this is the result (although the diff will have been much more significant in a real production app on a device under pressure). Tested on an HMD Pulse, Android 15, 4GB RAM, T606 CPU
Android Studio 2026-04-16 13 44 10

other calls were too fast for the profiling sample rate to catch them

…ied swipes

The ACTION_POINTER_DOWN handler suppressed taps but not flings. When
the last finger lifts quickly after a pinch-to-zoom, the velocity check
in ACTION_UP could fire onFling, causing SentryGestureListener to
misclassify the gesture as a swipe breadcrumb.

Add ignoreUpEvent flag mirroring GestureDetector's mIgnoreNextUpEvent
to skip the entire UP handler after multi-touch.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@romtsn romtsn merged commit de6a178 into main Apr 16, 2026
67 of 69 checks passed
@romtsn romtsn deleted the rz/fix/anr-gesture-detector branch April 16, 2026 12:35
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