fix potential segfault: dont use Companion in JNI calls and improve JNI ref releasing#3354
Merged
fix potential segfault: dont use Companion in JNI calls and improve JNI ref releasing#3354
Companion in JNI calls and improve JNI ref releasing#3354Conversation
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:
|
Contributor
Android Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| ad121c0 | 353.85 ms | 358.80 ms | 4.95 ms |
| 2cf9161 | 454.12 ms | 512.67 ms | 58.55 ms |
| c1e775e | 482.36 ms | 608.98 ms | 126.62 ms |
| 7b21e8b | 467.74 ms | 466.24 ms | -1.50 ms |
| 9b99523 | 456.91 ms | 490.55 ms | 33.64 ms |
| cc4e375 | 426.15 ms | 482.34 ms | 56.19 ms |
| 6bcdc99 | 440.23 ms | 435.77 ms | -4.46 ms |
| a69a51f | 437.18 ms | 450.60 ms | 13.42 ms |
| 6b69699 | 456.06 ms | 557.44 ms | 101.38 ms |
| 819c1e7 | 449.80 ms | 442.98 ms | -6.82 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| ad121c0 | 13.93 MiB | 15.06 MiB | 1.13 MiB |
| 2cf9161 | 6.54 MiB | 7.70 MiB | 1.16 MiB |
| c1e775e | 6.54 MiB | 7.70 MiB | 1.17 MiB |
| 7b21e8b | 13.93 MiB | 15.00 MiB | 1.06 MiB |
| 9b99523 | 6.54 MiB | 7.69 MiB | 1.15 MiB |
| cc4e375 | 6.54 MiB | 7.69 MiB | 1.15 MiB |
| 6bcdc99 | 13.93 MiB | 15.00 MiB | 1.06 MiB |
| a69a51f | 13.93 MiB | 15.06 MiB | 1.13 MiB |
| 6b69699 | 6.54 MiB | 7.70 MiB | 1.17 MiB |
| 819c1e7 | 13.93 MiB | 15.00 MiB | 1.06 MiB |
Previous results on branch: fix/segfault
Startup times
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| f2c85c0 | 449.14 ms | 461.86 ms | 12.72 ms |
| 69cd270 | 373.60 ms | 374.26 ms | 0.66 ms |
| a584335 | 360.74 ms | 365.60 ms | 4.86 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| f2c85c0 | 13.93 MiB | 15.18 MiB | 1.25 MiB |
| 69cd270 | 13.93 MiB | 15.18 MiB | 1.25 MiB |
| a584335 | 13.93 MiB | 15.18 MiB | 1.25 MiB |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3354 +/- ##
==========================================
+ Coverage 88.47% 89.96% +1.48%
==========================================
Files 291 95 -196
Lines 9917 3198 -6719
==========================================
- Hits 8774 2877 -5897
+ Misses 1143 321 -822 ☔ View full report in Codecov by Sentry. |
Contributor
iOS Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 6ad8fc4 | 1263.70 ms | 1266.06 ms | 2.36 ms |
| c8596a6 | 1234.11 ms | 1241.19 ms | 7.08 ms |
| c0dde50 | 1268.90 ms | 1275.61 ms | 6.71 ms |
| 2cf9161 | 1248.33 ms | 1266.55 ms | 18.22 ms |
| c1e775e | 1263.08 ms | 1275.32 ms | 12.24 ms |
| bfabaf2 | 1251.72 ms | 1253.38 ms | 1.67 ms |
| 7b21e8b | 1256.79 ms | 1267.12 ms | 10.33 ms |
| d0aa4b6 | 1268.23 ms | 1268.39 ms | 0.15 ms |
| 54acf91 | 1257.65 ms | 1277.96 ms | 20.31 ms |
| 192b44c | 1269.08 ms | 1275.52 ms | 6.44 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 6ad8fc4 | 5.53 MiB | 6.01 MiB | 487.65 KiB |
| c8596a6 | 7.86 MiB | 9.44 MiB | 1.58 MiB |
| c0dde50 | 5.53 MiB | 6.01 MiB | 488.14 KiB |
| 2cf9161 | 7.86 MiB | 9.44 MiB | 1.58 MiB |
| c1e775e | 20.70 MiB | 22.46 MiB | 1.75 MiB |
| bfabaf2 | 5.53 MiB | 6.01 MiB | 487.95 KiB |
| 7b21e8b | 5.53 MiB | 6.00 MiB | 479.96 KiB |
| d0aa4b6 | 5.53 MiB | 6.02 MiB | 502.04 KiB |
| 54acf91 | 20.70 MiB | 22.46 MiB | 1.75 MiB |
| 192b44c | 5.53 MiB | 5.96 MiB | 444.33 KiB |
Companion in JNI callsCompanion in JNI calls and fix JNI ref releasing
Companion in JNI calls and fix JNI ref releasingCompanion in JNI calls and improve JNI ref releasing
denrase
approved these changes
Nov 19, 2025
packages/flutter/lib/src/native/java/sentry_native_java_init.dart
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📜 Description
Switch JNI/Dart calls from
native.SentryFlutterPlugin.Companion.xyz()tonative.SentryFlutterPlugin.xyz()No object lifetime hazards: Static calls use only a jclass and
CallStatic…Method. Companion calls use a Companion object andCall…Method, which can crash if the object ref is stale, not global, or used across threads.globalEnv_CallStaticObjectMethodvsglobalEnv_CallObjectMethodFixes #3353
Properly set up releasing of JNI Refs
Each
toJString()(and othertoJxyz()) creates a JNI local reference.releasedBy(arena)defers deletion until the arena ends, so building big maps/lists holds hundreds/thousands of locals at once. That can overflow the per-thread local reference table and crash aroundglobalEnv_NewString/AddGlobalRef.Fixes #3348
💡 Motivation and Context
Potentially fixes #3348 and #3353
💚 How did you test it?
Unfortunately I could not repro the crashes referenced in the issue.
But the existing integration tests should still work with this change
📝 Checklist
sendDefaultPiiis enabled🔮 Next steps