fix: Thread-safe scope observer iteration and propagation context locking#7897
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7897 +/- ##
=============================================
- Coverage 86.192% 85.563% -0.630%
=============================================
Files 487 487
Lines 29651 29660 +9
Branches 12894 12901 +7
=============================================
- Hits 25557 25378 -179
- Misses 4046 4232 +186
- Partials 48 50 +2
... and 16 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit fa0890b. Configure here.
38e1d2e to
960f0c0
Compare
📲 Install BuildsiOS
|
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 29d546e | 1224.06 ms | 1257.05 ms | 32.98 ms |
| e56c394 | 1229.37 ms | 1245.86 ms | 16.49 ms |
| a7c42d9 | 1217.25 ms | 1253.98 ms | 36.73 ms |
| 787537a | 1218.35 ms | 1251.72 ms | 33.38 ms |
| 7814719 | 1220.98 ms | 1254.14 ms | 33.16 ms |
| 1155f0c | 1222.41 ms | 1251.18 ms | 28.76 ms |
| 6b08499 | 1231.61 ms | 1241.90 ms | 10.29 ms |
| 9b154ad | 1218.94 ms | 1253.60 ms | 34.66 ms |
| bdd8e0e | 1233.35 ms | 1266.96 ms | 33.60 ms |
| 670aba7 | 1219.98 ms | 1257.20 ms | 37.23 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 29d546e | 24.14 KiB | 1.15 MiB | 1.13 MiB |
| e56c394 | 24.14 KiB | 1.16 MiB | 1.13 MiB |
| a7c42d9 | 24.14 KiB | 1.15 MiB | 1.13 MiB |
| 787537a | 24.14 KiB | 1.15 MiB | 1.12 MiB |
| 7814719 | 24.14 KiB | 1.15 MiB | 1.13 MiB |
| 1155f0c | 24.14 KiB | 1.16 MiB | 1.13 MiB |
| 6b08499 | 24.14 KiB | 1.15 MiB | 1.13 MiB |
| 9b154ad | 24.14 KiB | 1.16 MiB | 1.13 MiB |
| bdd8e0e | 24.14 KiB | 1.16 MiB | 1.13 MiB |
| 670aba7 | 24.14 KiB | 1.16 MiB | 1.13 MiB |
|
@antonis looks like tests are failing with this change: |
…king Protect observer array with _observersLock and snapshot copy to prevent mutation-during-enumeration crashes. Replace broken @synchronized(_propagationContext) with dedicated lock object.
960f0c0 to
4eb1b14
Compare
|
Thank you for adding the |
📜 Description
Fixes race conditions in
SentryScopethat causeEXC_BREAKPOINTcrashes during concurrent scope observer enumeration and mutation.SentryScope.m:
_observersLock+observerSnapshot— Adds a dedicated lock and snapshot method returning[self.observers copy]. All 21 observer iteration sites are migrated fromself.observersto[self observerSnapshot], eliminating mutation-during-enumeration on theNSMutableArray.addScopeObserver:is also protected by the same lock._propagationContextLock— Replaces@synchronized(_propagationContext)which locked on the ivar value itself — a bug since the ivar is reassigned inside the block, allowing concurrent calls to lock on different objects. Adds explicit@synthesize+ manual getter/setter with a stable lock object.💡 Motivation and Context
Fixes getsentry/sentry-react-native#6131
A similar fix was applied with #7807 but didn't cover the observer array or propagation context.
💚 How did you test it?
testAddObserverWhileEnumeratingObservers— stress test that exercises the exact crash scenario: concurrentaddScopeObserver:calls while other threads iterate observers via scope setters.testModifyingFromMultipleThreadstestModifyingFromMultipleThreads_withObserverAsyncDispatchtestScopeObserver_passesDistinctCopyToObserverstestScopeObserver_setPropagationContext_UpdatesTraceContext📝 Checklist
sendDefaultPIIis enabled.