Skip to content

fix(breadcrumbs): Unsubscribe to system event during background to avoid reporting breadcrumbs with wrong timestamps on return to foreground#7803

Merged
denrase merged 4 commits intomainfrom
fix/system-event-breadcrumbs-lifecycle
Apr 17, 2026
Merged

fix(breadcrumbs): Unsubscribe to system event during background to avoid reporting breadcrumbs with wrong timestamps on return to foreground#7803
denrase merged 4 commits intomainfrom
fix/system-event-breadcrumbs-lifecycle

Conversation

@denrase
Copy link
Copy Markdown
Collaborator

@denrase denrase commented Apr 14, 2026

📜 Description

Per Apple's docs (https://developer.apple.com/documentation/uikit/processing-queued-notifications?language=objc), iOS queues notifications while an app is suspended and delivers them on resume. The time we report them, might not align witht he time they were triggered, so they should not provide diagnostic value, so we might as well not use them. This also aligns us with Android behaviour, which implemented the equivalent issue in getsentry/sentry-java#4338.

This PR makes SentrySystemEventBreadcrumbs lifecycle-aware: unsubscribes from system event notifications on didEnterBackgroundNotification, re-subscribes on willEnterForegroundNotification. An isSubscribedToSystemEvents flag makes both operations idempotent.

Also fixes AND/OR matching bug in TestNSNotificationCenterWrapper.removeObserver(_:name:object:).

💡 Motivation and Context

This is being done with #4580 in mind. The reported SIGPIPE crashes show snprintf in the stack trace, but snprintf writes to a stack buffer and cannot generate SIGPIPE. Our hypothesis is that the signal originates elsewhere (e.g. stale network connections breaking after resume) and is delivered asynchronously to the main thread while it happens to be encoding a breadcrumb.

SentryCrash treats SIGPIPE as fatal, so the crash report captures whatever the thread was doing at signal delivery time. So while this will in all likleyhood not fix the underlying problem, it might reduce noise so we can find the root cause.

💚 How did you test it?

Unit tests.

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

Per Apple's docs (https://developer.apple.com/documentation/uikit/processing-queued-notifications?language=objc), iOS queues notifications while an app is suspended and delivers them on resume. The time we report them, might not align witht he time they were triggered, so they should not provide diagnostic value, so we might as well not use them. This also aligns us with Android behaviour, which implemented the equivalent issue in getsentry/sentry-java#4338.

This PR makes SentrySystemEventBreadcrumbs lifecycle-aware: unsubscribes from system event notifications on didEnterBackgroundNotification, re-subscribes on willEnterForegroundNotification. An isSubscribedToSystemEvents flag makes both operations idempotent.

Also fixes AND/OR matching bug in TestNSNotificationCenterWrapper.removeObserver(_:name:object:).

This is being done with #4580 in mind. The reported SIGPIPE crashes show snprintf in the stack trace, but snprintf writes to a stack buffer and cannot generate SIGPIPE. Our hypothesis is that the signal originates elsewhere (e.g. stale network connections breaking after resume) and is delivered asynchronously to the main thread while it happens to be encoding a breadcrumb.

SentryCrash treats SIGPIPE as fatal, so the crash report captures whatever the thread was doing at signal delivery time. So while this will in all likleyhood not fix the underlying problem, it might reduce noise so we can find the root cause.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 14, 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 🐛

  • (breadcrumbs) Unsubscribe to system event during background to avoid reporting breadcrumbs with wrong timestamps on return to foreground by denrase in #7803
  • (replay) Keep replayType as buffer for Session Replay triggered by an error by romtsn in #7804
  • Copy mutable collections before passing to scope observers by antonis in #7807
  • Detect development builds via provisioning profile entitlement by denrase in #7702

Internal Changes 🔧

Deps

  • Bump actions/upload-pages-artifact from 4.0.0 to 5.0.0 by dependabot in #7789
  • Bump actions/github-script from 8.0.0 to 9.0.0 by dependabot in #7793

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 14, 2026

Fails
🚫 Please consider adding a changelog entry for the next release.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

### Fixes

- Unsubscribe to system event during background to avoid reporting breadcrumbs with wrong timestamps on return to foreground ([#7803](https://github.com/getsentry/sentry-cocoa/pull/7803))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description or adding a skip-changelog label.

Generated by 🚫 dangerJS against 7180e07

@denrase denrase changed the title fix: Unsubscribe system event breadcrumbs during background fix(breadcrumbs): Unsubscribe system event breadcrumbs during background Apr 14, 2026
@denrase denrase marked this pull request as ready for review April 14, 2026 15:51
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.423%. Comparing base (1b7b7b4) to head (7180e07).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #7803       +/-   ##
=============================================
+ Coverage   85.422%   85.423%   +0.001%     
=============================================
  Files          487       487               
  Lines        29195     29226       +31     
  Branches     12630     12650       +20     
=============================================
+ Hits         24939     24966       +27     
- Misses        4205      4210        +5     
+ Partials        51        50        -1     
Files with missing lines Coverage Δ
...tils/Sources/TestNSNotificationCenterWrapper.swift 100.000% <100.000%> (ø)
...ons/Breadcrumbs/SentrySystemEventBreadcrumbs.swift 98.604% <100.000%> (+0.200%) ⬆️

... and 5 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 1b7b7b4...7180e07. Read the comment docs.

Copy link
Copy Markdown
Contributor

@itaybre itaybre left a comment

Choose a reason for hiding this comment

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

LGTM, with one suggestion

Comment thread CHANGELOG.md Outdated
@denrase denrase changed the title fix(breadcrumbs): Unsubscribe system event breadcrumbs during background fix(breadcrumbs): Unsubscribe to system event during background to avoid reporting breadcrumbs with wrong timestamps on return to foreground (#7702) Apr 17, 2026
@denrase denrase changed the title fix(breadcrumbs): Unsubscribe to system event during background to avoid reporting breadcrumbs with wrong timestamps on return to foreground (#7702) fix(breadcrumbs): Unsubscribe to system event during background to avoid reporting breadcrumbs with wrong timestamps on return to foreground Apr 17, 2026
@denrase denrase added the ready-to-merge Use this label to trigger all PR workflows label Apr 17, 2026
@denrase denrase enabled auto-merge (squash) April 17, 2026 08:46
@sentry
Copy link
Copy Markdown

sentry bot commented Apr 17, 2026

📲 Install Builds

iOS

🔗 App Name App ID Version Configuration
SDK-Size io.sentry.sample.SDK-Size 9.10.0 (1) Release

⚙️ sentry-cocoa Build Distribution Settings

@github-actions
Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1236.20 ms 1268.22 ms 32.01 ms
Size 24.14 KiB 1.13 MiB 1.11 MiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
1d7a181 1232.27 ms 1264.61 ms 32.34 ms
6ea2191 1206.67 ms 1224.57 ms 17.90 ms
f567e36 1215.04 ms 1245.10 ms 30.06 ms
0bff410 1218.59 ms 1254.13 ms 35.54 ms
870ddae 1224.24 ms 1249.45 ms 25.21 ms
59aa682 1226.30 ms 1261.74 ms 35.43 ms
55da7eb 1223.38 ms 1264.72 ms 41.33 ms
5807865 1230.73 ms 1259.50 ms 28.77 ms
c9fc5be 1219.12 ms 1246.60 ms 27.48 ms
79cee27 1214.65 ms 1238.66 ms 24.01 ms

App size

Revision Plain With Sentry Diff
1d7a181 24.14 KiB 1.11 MiB 1.09 MiB
6ea2191 24.14 KiB 1.13 MiB 1.11 MiB
f567e36 24.14 KiB 1.13 MiB 1.10 MiB
0bff410 24.14 KiB 1.13 MiB 1.11 MiB
870ddae 24.14 KiB 1.10 MiB 1.08 MiB
59aa682 24.14 KiB 1.13 MiB 1.10 MiB
55da7eb 24.14 KiB 1.11 MiB 1.09 MiB
5807865 24.14 KiB 1.06 MiB 1.04 MiB
c9fc5be 24.14 KiB 1.10 MiB 1.07 MiB
79cee27 24.14 KiB 1.10 MiB 1.08 MiB

@denrase denrase merged commit 301d380 into main Apr 17, 2026
256 of 272 checks passed
@denrase denrase deleted the fix/system-event-breadcrumbs-lifecycle branch April 17, 2026 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Use this label to trigger all PR workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants