Skip to content

fix(flutter): Avoid JNI callbacks for Android scope sync#3676

Merged
buenaflor merged 2 commits into
mainfrom
fix/android-jni-callback-lifecycle
Apr 30, 2026
Merged

fix(flutter): Avoid JNI callbacks for Android scope sync#3676
buenaflor merged 2 commits into
mainfrom
fix/android-jni-callback-lifecycle

Conversation

@denrase

@denrase denrase commented Apr 28, 2026

Copy link
Copy Markdown
Collaborator

📜 Description

Moves Android beforeSend tagging and context sync updates from Dart-backed JNI callbacks to Kotlin static methods. This avoids long-lived or short-lived Java→Dart proxy callbacks in the affected scope-sync path while preserving existing event origin/environment tagging and context behavior.

Since we cannot bump JNI, which may have a fix in v 1.0.0, we go with this workaround.

Also called a sample app launch/close in a loop, but could not re-produce the issue reported. So this is a best effort by moving the most likley affected parts of the code.

💡 Motivation and Context

Fixes #3658
Fixes #3601

💚 How did you test it?

Added temp logs to sample app and called the methods in question:

Console/logcat output:

I/flutter: SENTRY_JNI_TEMP starting context/beforeSend verification
I/SentryJniTemp: setContext key=jni-temp-startup-context value={source=startup, value=from-dart, timestamp=2026-04-28T15:26:27.710851}
valueType=java.util.HashMap
I/SentryJniTemp: setContext key=jni-temp-removed-context value=remove-me valueType=java.lang.String
I/SentryJniTemp: removeContext key=jni-temp-removed-context
I/flutter: SENTRY_JNI_TEMP invoking native capture
I/SentryJniTemp: beforeSend sdk=sentry.java.android.flutter origin=android environment=java
I/flutter: SENTRY_JNI_TEMP native capture invoked

📝 Checklist

  • I reviewed submitted code
  • 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
  • All tests passing
  • No breaking changes

🔮 Next steps

We also have JNI dart side callbacks for replay, but since the original issue is about scope sync, i focused on those first.

Moves Android beforeSend tagging and context sync updates from Dart-backed JNI callbacks to Kotlin static methods. This avoids long-lived or short-lived Java→Dart proxy callbacks in the affected scope-sync path while preserving existing event origin/environment tagging and context behavior.
@github-actions

Copy link
Copy Markdown
Contributor

🚨 Detected changes in high risk code 🚨

High-risk code has higher potential to break the SDK and may be hard to test. To prevent severe bugs, apply the rollout process for releasing such changes and be extra careful when changing and reviewing these files:

  • packages/flutter/android/src/main/kotlin/io/sentry/flutter/SentryFlutterPlugin.kt

@codecov

codecov Bot commented Apr 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.90%. Comparing base (2269f05) to head (1030efa).
⚠️ Report is 6 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3676      +/-   ##
==========================================
+ Coverage   86.80%   91.90%   +5.09%     
==========================================
  Files         335      105     -230     
  Lines       11934     3729    -8205     
==========================================
- Hits        10359     3427    -6932     
+ Misses       1575      302    -1273     
Flag Coverage Δ
sentry ?
sentry_dio ?
sentry_drift ?
sentry_file ?
sentry_firebase_remote_config 100.00% <ø> (ø)
sentry_flutter 91.51% <ø> (+0.34%) ⬆️
sentry_hive ?
sentry_isar ?
sentry_link ?
sentry_logging ?
sentry_sqflite ?
sentry_supabase 97.27% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sentry

sentry Bot commented Apr 28, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
sentry_flutter_example io.sentry.flutter.sample 9.19.0 (1) Release

⚙️ sentry-flutter Build Distribution Settings

@denrase
denrase marked this pull request as ready for review April 28, 2026 13:45
@denrase
denrase requested a review from buenaflor as a code owner April 28, 2026 13:45
@github-actions

github-actions Bot commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

iOS Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1254.25 ms 1263.96 ms 9.71 ms
Size 5.73 MiB 6.18 MiB 465.26 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
1fff351 1265.77 ms 1256.06 ms -9.70 ms
d2356d0 1257.04 ms 1257.94 ms 0.89 ms
c002f00 1252.47 ms 1258.78 ms 6.31 ms
cf443d2 1255.79 ms 1248.38 ms -7.40 ms
3f47ea3 1263.90 ms 1263.87 ms -0.02 ms
1ce780b 1252.49 ms 1256.17 ms 3.68 ms
0929dbf 1275.89 ms 1282.22 ms 6.33 ms
6b69699 1254.80 ms 1273.31 ms 18.52 ms
a10aff4 1241.67 ms 1255.02 ms 13.35 ms
fd88186 1255.06 ms 1252.76 ms -2.30 ms

App size

Revision Plain With Sentry Diff
1fff351 5.73 MiB 6.17 MiB 455.43 KiB
d2356d0 5.66 MiB 6.09 MiB 448.38 KiB
c002f00 5.65 MiB 6.09 MiB 448.38 KiB
cf443d2 5.53 MiB 6.00 MiB 479.99 KiB
3f47ea3 5.53 MiB 5.96 MiB 444.81 KiB
1ce780b 5.66 MiB 6.10 MiB 451.58 KiB
0929dbf 7.86 MiB 9.54 MiB 1.69 MiB
6b69699 7.86 MiB 9.44 MiB 1.58 MiB
a10aff4 5.53 MiB 6.00 MiB 486.71 KiB
fd88186 5.53 MiB 6.00 MiB 479.94 KiB

@github-actions

Copy link
Copy Markdown
Contributor

Android Performance metrics 🚀

  Plain With Sentry Diff
Startup time 378.80 ms 380.66 ms 1.86 ms
Size 14.31 MiB 15.56 MiB 1.25 MiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
c97f488 502.43 ms 492.47 ms -9.97 ms
3801d52 397.53 ms 378.87 ms -18.66 ms
0929dbf 462.82 ms 492.76 ms 29.94 ms
426fbfd 368.10 ms 353.23 ms -14.87 ms
0e2b9b0 368.00 ms 356.72 ms -11.28 ms
cf443d2 464.64 ms 479.04 ms 14.40 ms
396cb30 405.70 ms 427.56 ms 21.86 ms
ce5c42b 401.52 ms 394.63 ms -6.90 ms
c8596a6 474.00 ms 492.96 ms 18.96 ms
2d34233 470.54 ms 558.90 ms 88.36 ms

App size

Revision Plain With Sentry Diff
c97f488 14.31 MiB 15.49 MiB 1.19 MiB
3801d52 14.31 MiB 15.49 MiB 1.19 MiB
0929dbf 6.54 MiB 7.70 MiB 1.17 MiB
426fbfd 13.93 MiB 15.06 MiB 1.13 MiB
0e2b9b0 14.30 MiB 15.49 MiB 1.19 MiB
cf443d2 13.93 MiB 15.00 MiB 1.06 MiB
396cb30 13.93 MiB 15.06 MiB 1.13 MiB
ce5c42b 14.31 MiB 15.49 MiB 1.19 MiB
c8596a6 6.54 MiB 7.53 MiB 1015.27 KiB
2d34233 6.54 MiB 7.55 MiB 1.01 MiB

@github-actions

github-actions Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

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).


Features

  • (span-first) Add transaction and app start type span attributes by buenaflor in #3678
  • Prevent cross-organization trace continuation by antonis in #3567
    • By default, the SDK now extracts the organization ID from the DSN (e.g. o123.ingest.sentry.io) and compares it with the sentry-org_id value in incoming baggage headers. When the two differ, the SDK starts a fresh trace instead of continuing the foreign one. This guards against accidentally linking traces across organizations.
    • New option strictTraceContinuation (default false): when enabled, both the SDK's org ID and the incoming baggage org ID must be present and match for a trace to be continued. Traces with a missing org ID on either side are rejected.
    • New option orgId: allows explicitly setting the organization ID for self-hosted and Relay setups where it cannot be extracted from the DSN.
    • Options are also applied to the native Android SDK. On iOS, only the Dart layer enforces strict trace continuation.

Fixes

Dart

  • Make sentryOnError synchronous in runZonedGuarded by theprantadutta in #3697
  • Route SDK diagnostic logs to browser console on web by theprantadutta in #3698

Flutter

  • Avoid JNI callbacks for Android scope sync by denrase in #3676
  • Release Android JNI refs by buenaflor in #3712
  • Release replay JNI refs by buenaflor in #3699
  • Avoid JNI callbacks for Android scope sync by denrase in #3676
  • Send frame delay in seconds by buenaflor in #3677

Enhancements

  • (flutter) Optimize Android scope sync by buenaflor in #3708

Dependencies

Deps

  • chore(deps): update Android SDK to v8.41.0 by github-actions in #3687
  • chore(deps): update Cocoa SDK to v8.58.2 by github-actions in #3664
  • chore(deps): update Native SDK to v0.13.8 by github-actions in #3667

Internal Changes

Deps

  • Bump getsentry/github-workflows from 3.3.0 to 3.4.0 by dependabot in #3705
  • Bump actions/create-github-app-token from 3.1.1 to 3.2.0 by dependabot in #3704
  • Bump getsentry/craft from 2.26.3 to 2.26.5 by dependabot in #3706
  • Bump getsentry/craft/.github/workflows/changelog-preview.yml from 2.26.3 to 2.26.5 by dependabot in #3703
  • Bump getsentry/craft from 2.26.2 to 2.26.3 by dependabot in #3694
  • Bump ruby/setup-ruby from 1.306.0 to 1.307.0 by dependabot in #3692
  • Bump getsentry/craft/.github/workflows/changelog-preview.yml from 2.26.2 to 2.26.3 by dependabot in #3693
  • Bump ruby/setup-ruby from 1.299.0 to 1.306.0 by dependabot in #3673
  • Bump actions/github-script from 8.0.0 to 9.0.0 by dependabot in #3670
  • Bump getsentry/craft/.github/workflows/changelog-preview.yml from 2.25.4 to 2.26.2 by dependabot in #3671
  • Bump getsentry/craft from 2.25.2 to 2.26.2 by dependabot in #3672
  • Bump actions/upload-artifact from 7.0.0 to 7.0.1 by dependabot in #3653

Other

  • (feedback) Rename SentryFeedbackWidget to SentryFeedbackForm by denrase in #3702
  • (flutter) Align CI with stable SwiftPM defaults by buenaflor in #3710
  • Remove collection runtime dependency by buenaflor in #3680
  • Notify linked issues on release by buenaflor in #3685
  • Enforce conventional commit format for PR titles by buenaflor in #3666

Other

  • release: 9.20.0 by buenaflor in 4860eca0

🤖 This preview updates automatically when you update the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants