Skip to content

ref: add SentryMutex and migrate locks#8214

Merged
philprime merged 9 commits into
mainfrom
ref/sentry-mutex
Jun 26, 2026
Merged

ref: add SentryMutex and migrate locks#8214
philprime merged 9 commits into
mainfrom
ref/sentry-mutex

Conversation

@philprime

@philprime philprime commented Jun 25, 2026

Copy link
Copy Markdown
Member

Summary

Introduces SentryMutex<T>, a value-wrapping mutex inspired by Swift's Synchronization.Mutex (iOS 18+ / macOS 15+). Since the SDK's deployment target is well below those versions, we backport the same API using os_unfair_lock — the same underlying primitive Mutex uses on Apple platforms. When the minimum deployment target eventually reaches iOS 18+, SentryMutex can be replaced with a simple typealias SentryMutex = Mutex.

Migrates 15 Swift source files from NSLock / NSRecursiveLock + separate backing properties to SentryMutex<T>, which wraps the protected value directly and enforces that all access goes through withLock. This eliminates a class of bugs where a property could be accessed without holding its lock.

Files not migrated use NSRecursiveLock for re-entrant locking (SentrySession, SentryDependencyContainer, HangTracker, SentryReachability, SentryAppStateManager) — SentryMutex is 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 lock
  • SentryMutex<T> makes the protected value inaccessible without withLock, matching Synchronization.Mutex semantics
  • os_unfair_lock is faster than NSLock (no ObjC message dispatch overhead)
  • Prepares for eventual adoption of Synchronization.Mutex when deployment targets allow

#skip-changelog

Closes #8215

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.
Comment thread Sources/Swift/Core/Helper/SentryBinaryImageCache.swift
Move sentrycrashbic callback registration back inside the
mutex to prevent a race between start() and stop(), matching
the original behavior.
Comment thread Sources/Swift/Core/Helper/SentryBinaryImageCache.swift
Comment thread Sources/Swift/Core/Helper/SentryBinaryImageCache.swift Outdated
Comment thread Sources/Swift/Core/Helper/SentryBinaryImageCache.swift
@sentry

sentry Bot commented Jun 25, 2026

Copy link
Copy Markdown

📲 Install Builds

iOS

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

⚙️ sentry-cocoa Build Distribution Settings

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.
Comment thread Sources/Swift/Core/Helper/SentryBinaryImageCache.swift Outdated
@philprime philprime requested a review from supervacuus June 25, 2026 09:28
@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1221.49 ms 1260.76 ms 39.27 ms
Size 24.14 KiB 1.22 MiB 1.20 MiB

Baseline results on branch: main

Startup times

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

Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated
Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated
Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated
Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated
Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated
Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated
Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated
Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated
- 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
Comment thread Tests/SentryTests/Swift/Tools/SentryMutexTests.swift
Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated
Comment thread Sources/Swift/Concurrency/SentryMutex.swift Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

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

Comment thread Sources/Swift/Integrations/UserFeedback/SentryShakeDetector.swift
@philprime philprime enabled auto-merge (squash) June 26, 2026 07:41
@philprime philprime merged commit a1ff75f into main Jun 26, 2026
309 of 311 checks passed
@philprime philprime deleted the ref/sentry-mutex branch June 26, 2026 07:57
itaybre pushed a commit that referenced this pull request Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved ready-to-merge Use this label to trigger all PR workflows skip-changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ref: add SentryMutex and migrate locks

4 participants