perf: Stop holding the global mutex during scope access - #2091
Conversation
e3facb1 to
5a1d374
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## jpnurmi/perf/rwscope #2091 +/- ##
========================================================
+ Coverage 74.47% 75.21% +0.73%
========================================================
Files 100 103 +3
Lines 27403 27930 +527
Branches 4968 5094 +126
========================================================
+ Hits 20408 21007 +599
+ Misses 5724 5574 -150
- Partials 1271 1349 +78 🚀 New features to boost your workflow:
|
5a1d374 to
58d161c
Compare
58d161c to
47cf101
Compare
47cf101 to
816f367
Compare
limbonaut
left a comment
There was a problem hiding this comment.
Caught a race: two threads can update the same tag, for example, but their notifications can arrive in the opposite order. An observer can therefore end up remembering an older value instead of the value actually stored in the scope. See comments below for more details.
3475564 to
3b8961a
Compare
There was a problem hiding this comment.
Sweet! Benchmarks with the recent changes:
Median benchmark-reported real time, in milliseconds.
| Benchmark | Threads | Master | PR | Speedup |
|---|---|---|---|---|
| Logs | 1 | 0.00332 | 0.00348 | 0.96× |
| Logs | 8 | 0.01571 | 0.00619 | 2.54× |
| Logs | 16 | 0.04656 | 0.00917 | 5.08× |
| Logs | 32 | 0.14999 | 0.02016 | 7.44× |
| Metrics | 1 | 0.00266 | 0.00279 | 0.95× |
| Metrics | 8 | 0.02256 | 0.00552 | 4.09× |
| Metrics | 16 | 0.05678 | 0.00922 | 6.16× |
| Metrics | 32 | 0.13757 | 0.01945 | 7.07× |
031883a to
ea0c4fb
Compare
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 ea0c4fb. Configure here.
bd0d0a7 to
c5493f6
Compare
The global scope mutex still serializes readers despite the per-scope read-write locks, limiting concurrent log and metric capture. Retain the scope during access instead of holding the global mutex. Coordinate cleanup with outstanding references and serialize observer callbacks with a separate mutex. Keep the global mutex for scope initialization and lifetime coordination.
Block new scope callers while existing references drain, and skip stale cleanup after concurrent initialization. Allow reentrant access to finish without accessing thread-local state from signal handlers.
Hold the observer lock across each observed scope mutation and its notification so callbacks cannot receive an older update after a newer one.
c5493f6 to
34af03d
Compare

Make sentry-native multi-threading friendly by allowing concurrent scope data readers.
The global scope data mutex became a serious bottleneck for multi-threaded logs and metrics. Most notably, when wired up with
UE_LOGthat may capture Unreal Engine logs from 100+ threads.The previous step (#2042) enabled RW locks on the scope data. This final step removes the remaining, now superfluous global mutex around the data. With this, multiple logger threads can enrich incoming log items with scope data/attributes in parallel.
In theory, other concurrent scope operations may benefit too, but logs and metrics are the most important high-volume scenario.
Before
After
Close: #1862