Skip to content

Conversation

@buenaflor
Copy link
Contributor

@buenaflor buenaflor commented Nov 20, 2025

📜 Description

  • After a Flutter hot restart, the old Dart isolate is torn down and its Dart port becomes invalid.
  • Android still holds Java/Kotlin callback objects (the recorder’s callbacks) and executes them later on the main thread.
  • Those callbacks are Java proxies created via jni that forward into Dart through the now-dead port.
    When the proxy invokes into JNI, libflutter.so dereferences invalid isolate state and crashes (SIGSEGV), often showing PortProxyBuilder.invoke in the stack.

Simple solution

  • Wrap every recorder callback with a small guard that checks a generation number.
  • Bump that generation when the Flutter engine detaches and before wiring a new recorder.
  • Each wrapper captures the generation at creation; if it doesn’t match the current one, it returns immediately and never crosses JNI.
  • Close the previous ReplayIntegration during restart so it stops posting work.

💡 Motivation and Context

Fixes crashing on hot-restart on Android

💚 How did you test it?

Manual test

  • Check it did not crash after hot restarting
  • Check replays are still sent after hot restarting

📝 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

@github-actions
Copy link
Contributor

github-actions bot commented Nov 20, 2025

🚨 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

@github-actions
Copy link
Contributor

github-actions bot commented Nov 20, 2025

Android Performance metrics 🚀

  Plain With Sentry Diff
Startup time 367.51 ms 360.53 ms -6.98 ms
Size 13.93 MiB 15.18 MiB 1.25 MiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
d3fb366 391.49 ms 385.85 ms -5.64 ms
a10aff4 488.19 ms 515.02 ms 26.83 ms
6bcdc99 440.23 ms 435.77 ms -4.46 ms
cdf371b 367.64 ms 377.02 ms 9.38 ms
93b7728 475.28 ms 489.13 ms 13.86 ms
2d34233 470.54 ms 558.90 ms 88.36 ms
7cfee3b 498.78 ms 516.98 ms 18.20 ms
0fb45d0 482.79 ms 554.02 ms 71.23 ms
0fb3800 465.64 ms 536.77 ms 71.13 ms
bfabaf2 408.04 ms 444.38 ms 36.34 ms

App size

Revision Plain With Sentry Diff
d3fb366 13.93 MiB 15.06 MiB 1.13 MiB
a10aff4 13.93 MiB 15.06 MiB 1.13 MiB
6bcdc99 13.93 MiB 15.00 MiB 1.06 MiB
cdf371b 13.93 MiB 15.18 MiB 1.25 MiB
93b7728 6.54 MiB 7.69 MiB 1.15 MiB
2d34233 6.54 MiB 7.55 MiB 1.01 MiB
7cfee3b 6.54 MiB 7.70 MiB 1.17 MiB
0fb45d0 6.54 MiB 7.70 MiB 1.17 MiB
0fb3800 6.54 MiB 7.69 MiB 1.15 MiB
bfabaf2 13.93 MiB 15.06 MiB 1.13 MiB

Previous results on branch: fix/stale-callbacks-crash-on-restart

Startup times

Revision Plain With Sentry Diff
5c9c1da 364.65 ms 359.74 ms -4.91 ms
df23ec7 423.21 ms 435.02 ms 11.81 ms
1858cb4 479.06 ms 479.72 ms 0.66 ms

App size

Revision Plain With Sentry Diff
5c9c1da 13.93 MiB 15.18 MiB 1.25 MiB
df23ec7 13.93 MiB 15.18 MiB 1.25 MiB
1858cb4 13.93 MiB 15.18 MiB 1.25 MiB

@codecov
Copy link

codecov bot commented Nov 20, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.33%. Comparing base (cdf371b) to head (331bcc2).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3358      +/-   ##
==========================================
+ Coverage   88.47%   90.33%   +1.86%     
==========================================
  Files         291       95     -196     
  Lines        9917     3198    -6719     
==========================================
- Hits         8774     2889    -5885     
+ Misses       1143      309     -834     

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

@github-actions
Copy link
Contributor

github-actions bot commented Nov 20, 2025

iOS Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1251.94 ms 1243.50 ms -8.44 ms
Size 5.53 MiB 6.02 MiB 501.22 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
3615e19 1225.02 ms 1234.57 ms 9.55 ms
393f8ec 1256.45 ms 1259.60 ms 3.16 ms
0929dbf 1275.89 ms 1282.22 ms 6.33 ms
e45c0e1 1269.08 ms 1278.83 ms 9.75 ms
2b5e090 1257.04 ms 1258.07 ms 1.02 ms
8541716 1270.18 ms 1271.80 ms 1.62 ms
9b99523 1256.06 ms 1270.33 ms 14.27 ms
396cb30 1265.06 ms 1265.73 ms 0.67 ms
c8596a6 1234.11 ms 1241.19 ms 7.08 ms
cdf371b 1246.24 ms 1251.10 ms 4.86 ms

App size

Revision Plain With Sentry Diff
3615e19 7.86 MiB 9.44 MiB 1.58 MiB
393f8ec 5.53 MiB 6.01 MiB 488.10 KiB
0929dbf 7.86 MiB 9.54 MiB 1.69 MiB
e45c0e1 7.86 MiB 9.44 MiB 1.58 MiB
2b5e090 5.53 MiB 6.00 MiB 485.10 KiB
8541716 5.53 MiB 6.00 MiB 479.96 KiB
9b99523 7.86 MiB 9.44 MiB 1.58 MiB
396cb30 5.53 MiB 6.01 MiB 488.17 KiB
c8596a6 7.86 MiB 9.44 MiB 1.58 MiB
cdf371b 5.53 MiB 6.02 MiB 501.23 KiB

Previous results on branch: fix/stale-callbacks-crash-on-restart

Startup times

Revision Plain With Sentry Diff
df23ec7 1254.21 ms 1249.83 ms -4.37 ms
5c9c1da 1261.27 ms 1263.67 ms 2.40 ms
1858cb4 1253.71 ms 1250.65 ms -3.06 ms

App size

Revision Plain With Sentry Diff
df23ec7 5.53 MiB 6.02 MiB 502.14 KiB
5c9c1da 5.53 MiB 6.02 MiB 502.15 KiB
1858cb4 5.53 MiB 6.02 MiB 502.14 KiB

@buenaflor buenaflor merged commit 3ddf010 into main Nov 20, 2025
47 checks passed
@buenaflor buenaflor deleted the fix/stale-callbacks-crash-on-restart branch November 20, 2025 13: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.

3 participants