ref: add SentryMutex and migrate locks#8214
Merged
Merged
Conversation
Backport of Synchronization.Mutex API (iOS 18+) using os_unfair_lock for older deployment targets.
Document SentryMutex as preferred locking primitive for new Swift code. Expose isSessionPaused as computed property for test access after State struct migration.
5 tasks
Move sentrycrashbic callback registration back inside the mutex to prevent a race between start() and stop(), matching the original behavior.
philprime
commented
Jun 25, 2026
This reverts commit 3bf7671.
📲 Install BuildsiOS
|
Register callbacks outside the lock since sentrycrashbic_registerAddedCallback synchronously replays loaded images, each re-entering the lock. Unregistering stays inside the lock as it does not replay.
philprime
commented
Jun 25, 2026
Contributor
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 9787f05 | 1233.67 ms | 1269.00 ms | 35.33 ms |
| 119dc37 | 1217.64 ms | 1250.44 ms | 32.80 ms |
| 31a83fd | 1218.63 ms | 1247.18 ms | 28.55 ms |
| 5cc1f4e | 1223.49 ms | 1258.69 ms | 35.20 ms |
| 92bcc8f | 1233.43 ms | 1270.20 ms | 36.77 ms |
| 943b250 | 1214.69 ms | 1257.32 ms | 42.63 ms |
| 78aad96 | 1232.02 ms | 1262.42 ms | 30.40 ms |
| 1caa212 | 1229.06 ms | 1251.23 ms | 22.17 ms |
| 2446b3d | 1218.36 ms | 1253.98 ms | 35.62 ms |
| 6844d69 | 1228.25 ms | 1265.20 ms | 36.95 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 9787f05 | 24.14 KiB | 1.18 MiB | 1.15 MiB |
| 119dc37 | 24.14 KiB | 1.16 MiB | 1.13 MiB |
| 31a83fd | 24.14 KiB | 1.17 MiB | 1.15 MiB |
| 5cc1f4e | 24.14 KiB | 1.18 MiB | 1.16 MiB |
| 92bcc8f | 24.14 KiB | 1.15 MiB | 1.13 MiB |
| 943b250 | 24.14 KiB | 1.17 MiB | 1.15 MiB |
| 78aad96 | 24.14 KiB | 1.18 MiB | 1.16 MiB |
| 1caa212 | 24.14 KiB | 1.18 MiB | 1.16 MiB |
| 2446b3d | 24.14 KiB | 1.17 MiB | 1.15 MiB |
| 6844d69 | 24.14 KiB | 1.20 MiB | 1.17 MiB |
Previous results on branch: ref/sentry-mutex
Startup times
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| e9adabd | 1237.49 ms | 1264.13 ms | 26.64 ms |
| f81e632 | 1211.73 ms | 1249.77 ms | 38.04 ms |
| 425d324 | 1219.31 ms | 1252.27 ms | 32.96 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| e9adabd | 24.14 KiB | 1.22 MiB | 1.20 MiB |
| f81e632 | 24.14 KiB | 1.22 MiB | 1.20 MiB |
| 425d324 | 24.14 KiB | 1.22 MiB | 1.20 MiB |
jamieQ
reviewed
Jun 25, 2026
- Use UnsafeMutablePointer for os_unfair_lock to guarantee a stable heap-allocated address - Make SentryMutex internal (not public API) - Drop @unchecked Sendable from Storage class - Update doc comment to reference OSAllocatedUnfairLock
jamieQ
reviewed
Jun 25, 2026
itaybre
reviewed
Jun 25, 2026
NinjaLikesCheez
approved these changes
Jun 26, 2026
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 035b5cc. Configure here.
This was referenced Jun 26, 2026
itaybre
pushed a commit
that referenced
this pull request
Jun 26, 2026
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.

Summary
Introduces
SentryMutex<T>, a value-wrapping mutex inspired by Swift'sSynchronization.Mutex(iOS 18+ / macOS 15+). Since the SDK's deployment target is well below those versions, we backport the same API usingos_unfair_lock— the same underlying primitiveMutexuses on Apple platforms. When the minimum deployment target eventually reaches iOS 18+,SentryMutexcan be replaced with a simpletypealias SentryMutex = Mutex.Migrates 15 Swift source files from
NSLock/NSRecursiveLock+ separate backing properties toSentryMutex<T>, which wraps the protected value directly and enforces that all access goes throughwithLock. This eliminates a class of bugs where a property could be accessed without holding its lock.Files not migrated use
NSRecursiveLockfor re-entrant locking (SentrySession,SentryDependencyContainer,HangTracker,SentryReachability,SentryAppStateManager) —SentryMutexis non-recursive by design and those call graphs need refactoring first.Motivation
NSLock+ separate property is error-prone: nothing prevents accessing the property without the lockSentryMutex<T>makes the protected value inaccessible withoutwithLock, matchingSynchronization.Mutexsemanticsos_unfair_lockis faster thanNSLock(no ObjC message dispatch overhead)Synchronization.Mutexwhen deployment targets allow#skip-changelog
Closes #8215