Skip to content

fix: Add persistence for context to fix watchdog termination events #5242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 52 commits into
base: main
Choose a base branch
from

Conversation

philprime
Copy link
Contributor

@philprime philprime commented May 16, 2025

📜 Description

  • Adds file persistence of the scope context to serialize for watchdog termination events

  • Changes the watchdog termination scope observer as a collective entry point forwarding events to multiple processors to segment the logic into individual, testable components

  • Refactors the existing breadcrumb persistence logic into a SentryWatchdogTerminationBreadcrumbProcessor. The implementation is not changed in this PR but prepared for future refactoring. A lot of the file operations are split with the SentryFileManager and would require more work.

  • Adds a SentryWatchdogTerminationContextProcessor to process and persist the context to the file system. It uses the newly introduced `Sentry

  • To not add even more logic to the SentryFileManager I added a wrapper store SentryScopeContextPersistentStore which handles encoding/decoding and file operations using the file manager.

  • The SentryWatchdogTerminationContextProcessor is dispatching the saving via the persistent store on a background thread to reduce the impact on the main thread.

  • The dispatch queue wrapper is created by the SentryDispatchFactory

In general I tried to bring a more constructor-based dependency injection style architecture into this PR, to make them replaceable and testable as individual units.

Originally the logic of SentryScopeContextPersistentStore was in the SentryWatchdogTerminationContextProcessor, therefore it's functionality is now slim and we could consider moving the method directly to the SentryWatchdogTerminationScopeObserver, but that would then. not match with the breadcrumb processor.

I believe the additional file is fine for now and can be refactored again at a future point.

💡 Motivation and Context

Closes #5182

💚 How did you test it?

📝 Checklist

You have to check all boxes before merging:

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

Blocked by

Copy link

codecov bot commented May 16, 2025

Codecov Report

Attention: Patch coverage is 99.31973% with 9 lines in your changes missing coverage. Please review.

Project coverage is 92.996%. Comparing base (864caac) to head (ee02a86).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...tence/SentryScopeContextPersistentStoreTests.swift 98.009% 4 Missing ⚠️
...stence/TestSentryScopeContextPersistentStore.swift 60.000% 4 Missing ⚠️
Sources/Sentry/SentryDependencyContainer.m 96.428% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #5242       +/-   ##
=============================================
+ Coverage   92.805%   92.996%   +0.190%     
=============================================
  Files          691       702       +11     
  Lines        86693     87809     +1116     
  Branches     30112     31512     +1400     
=============================================
+ Hits         80456     81659     +1203     
+ Misses        6140      6050       -90     
- Partials        97       100        +3     
Files with missing lines Coverage Δ
...ors/SentryWatchdogTerminationBreadcrumbProcessor.m 100.000% <100.000%> (ø)
Sources/Sentry/SentryFileManager.m 94.744% <100.000%> (+0.307%) ⬆️
Sources/Sentry/SentrySDK.m 88.800% <100.000%> (+0.060%) ⬆️
...es/Sentry/SentryWatchdogTerminationScopeObserver.m 55.555% <100.000%> (-19.170%) ⬇️
Sources/Sentry/SentryWatchdogTerminationTracker.m 100.000% <100.000%> (ø)
...try/SentryWatchdogTerminationTrackingIntegration.m 84.285% <100.000%> (-0.090%) ⬇️
...rs/SentryWatchdogTerminationContextProcessor.swift 100.000% <100.000%> (ø)
...ersistence/SentryScopeContextPersistentStore.swift 100.000% <100.000%> (ø)
...yTests/Helper/SentryDependencyContainerTests.swift 100.000% <100.000%> (ø)
...ts/SentryTests/Helper/SentryFileManagerTests.swift 97.083% <100.000%> (+0.035%) ⬆️
... and 13 more

... and 25 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 864caac...ee02a86. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

github-actions bot commented May 16, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1224.10 ms 1245.00 ms 20.90 ms
Size 23.76 KiB 835.19 KiB 811.43 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
676fe22 1220.71 ms 1240.14 ms 19.43 ms
06bc323 1223.12 ms 1252.88 ms 29.76 ms
9158639 1216.86 ms 1236.89 ms 20.03 ms
b9a9dca 1196.09 ms 1225.14 ms 29.05 ms
2334b81 1246.76 ms 1254.48 ms 7.72 ms
a716092 1234.94 ms 1255.18 ms 20.24 ms
f1c36e0 1210.96 ms 1228.22 ms 17.27 ms
2124551 1246.10 ms 1254.84 ms 8.74 ms
0ecf042 1228.25 ms 1250.67 ms 22.42 ms
9f0d9e0 1226.31 ms 1242.70 ms 16.40 ms

App size

Revision Plain With Sentry Diff
676fe22 21.58 KiB 698.12 KiB 676.54 KiB
06bc323 22.30 KiB 832.29 KiB 809.99 KiB
9158639 22.31 KiB 821.12 KiB 798.81 KiB
b9a9dca 21.58 KiB 414.59 KiB 393.01 KiB
2334b81 23.76 KiB 821.44 KiB 797.68 KiB
a716092 21.58 KiB 424.34 KiB 402.76 KiB
f1c36e0 21.58 KiB 670.39 KiB 648.81 KiB
2124551 22.85 KiB 411.69 KiB 388.84 KiB
0ecf042 21.58 KiB 631.82 KiB 610.24 KiB
9f0d9e0 21.58 KiB 424.28 KiB 402.70 KiB

Previous results on branch: philprime/issue-5182

Startup times

Revision Plain With Sentry Diff
c5b273a 1212.13 ms 1235.94 ms 23.81 ms
c34e9ed 1223.86 ms 1249.84 ms 25.98 ms
bcd1b71 1217.71 ms 1229.96 ms 12.25 ms
c9efa02 1229.42 ms 1251.57 ms 22.16 ms
69ce027 1232.45 ms 1251.65 ms 19.21 ms
9b69890 1244.68 ms 1258.16 ms 13.48 ms
8c53c21 1223.46 ms 1246.42 ms 22.96 ms
496853a 1225.18 ms 1244.33 ms 19.15 ms
35d6c88 1229.02 ms 1254.54 ms 25.52 ms
a24ff85 1209.27 ms 1239.76 ms 30.48 ms

App size

Revision Plain With Sentry Diff
c5b273a 23.76 KiB 829.41 KiB 805.65 KiB
c34e9ed 23.76 KiB 829.34 KiB 805.59 KiB
bcd1b71 23.76 KiB 829.42 KiB 805.66 KiB
c9efa02 23.76 KiB 829.31 KiB 805.55 KiB
69ce027 23.76 KiB 829.42 KiB 805.66 KiB
9b69890 23.76 KiB 874.87 KiB 851.11 KiB
8c53c21 23.76 KiB 829.42 KiB 805.66 KiB
496853a 23.76 KiB 830.09 KiB 806.33 KiB
35d6c88 23.76 KiB 821.23 KiB 797.47 KiB
a24ff85 23.76 KiB 830.18 KiB 806.42 KiB

@philprime philprime self-assigned this May 16, 2025
@philprime philprime marked this pull request as ready for review May 21, 2025 14:27
@philprime philprime requested a review from philipphofmann May 22, 2025 08:42
@philprime
Copy link
Contributor Author

This PR grew quite in size. I am now going to extract smaller PRs for necessary changes than can be seen as isolated changes (i.e. fixing compiler flags or serialization).

@metcalfealex
Copy link

👋 Any idea when this fix might get merged? We are very interested in it 😄 as its blocking us from upgrading Sentry from 8.43.0 to the latest version (we are using Sentry RN and would like to upgrade to pick up the Xcode 16.3 fixes)

@philprime philprime marked this pull request as ready for review May 30, 2025 07:51
@philprime
Copy link
Contributor Author

Hi @metcalfealex I believe the PR is ready for review now. As soon as my colleagues get a chance to review it, we'll make sure to merge it soon. This issue has the highest priority right now, but grew larger in size than expected.

Copy link
Contributor

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryFileManager.m

SentryWatchdogTerminationContextProcessor *watchdogTerminationContextProcessor;
#endif

#if SENTRY_TEST || SENTRY_TEST_CI || defined(SENTRY_TEST) || defined(SENTRY_TEST_CI)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to do this because. SENTRY_TEST || SENTRY_TEST_CI alone did not work for macOS and defined(SENTRY_TEST) || defined(SENTRY_TEST_CI) did not work for iOS.

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.

Missing device info on watchdog_termination events
4 participants