[WIP] perf: use rwlock for scope data access - #1877
Conversation
Instructions and example for changelogPlease add an entry to Example: ## Unreleased
### Features
- use rwlock for scope data access ([#1877](https://github.com/getsentry/sentry-native/pull/1877))If none of the above apply, you can opt out of this check by adding |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## jpnurmi/ref/attachments #1877 +/- ##
==========================================================
Coverage ? 75.05%
==========================================================
Files ? 104
Lines ? 26840
Branches ? 4853
==========================================================
Hits ? 20145
Misses ? 5367
Partials ? 1328 🚀 New features to boost your workflow:
|
495cd85 to
05fe336
Compare
3c7c966 to
4948141
Compare
4948141 to
0b0e77c
Compare
| sentry__cond_wait(&g_scope_idle, &g_lock); | ||
| } | ||
| if (g_scope_initialized) { | ||
| g_scope_initialized = false; | ||
| cleanup_scope(&g_scope); | ||
| cleanup_global_data(g_scope.data); | ||
| g_scope.data = NULL; | ||
| cleanup_observers(&g_scope); | ||
| sentry__mutex_free(&g_scope.observers_lock); | ||
| } | ||
| sentry__mutex_unlock(&g_lock); | ||
| } |
There was a problem hiding this comment.
Bug: The condition variable g_scope_idle is initialized but never destroyed, causing a kernel handle leak on some Windows platforms.
Severity: LOW
Suggested Fix
Implement a cleanup path for the g_scope_idle condition variable. This can be done by adding a call to a new sentry__cond_free(&g_scope_idle) function within sentry__scope_cleanup(), ensuring it is only called when g_scope_idle_initialized is true. The sentry__cond_free function would need to be implemented to close the relevant handles on Windows.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: src/sentry_scope.c#L1219-L1234
Potential issue: The condition variable `g_scope_idle` is initialized on first use
within `sentry__scope_begin()` but is never destroyed. On pre-Vista Windows platforms,
the implementation of `sentry__cond_init` allocates kernel handles for a semaphore and
an event. Since there is no corresponding cleanup function called during shutdown (e.g.,
in `sentry__scope_cleanup`), these handles are leaked. This leads to a resource leak on
affected Windows versions.
There was a problem hiding this comment.
To be fixed with the help of:
dab304d to
b899738
Compare
b899738 to
853172c
Compare
853172c to
dcbac0c
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 3 total unresolved issues (including 2 from previous reviews).
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit dcbac0c. Configure here.
f197136 to
802a09a
Compare
Add a non-recursive sentry_rwlock_t abstraction backed by platform reader/writer locks. Cover static and dynamic initialization, reader sharing, writer exclusion, and mixed read/write stress in unit tests.
Convert the POSIX rwlock operations from statement macros to static inline functions so callers can use them from scoped lock macro expressions.
Add a per-scope rwlock and use scoped read/write locking for simple by-value fields. Keep borrowed scope properties unchanged for now.
Add ref-style scope accessors for user and fingerprint values so callers get a retained reference that was incremented under the scope read lock. Guard user and fingerprint replacement with the scope write lock and keep observer notifications outside the locked section.
Store release, environment, and transaction as sentry_value_t fields with retained scope accessors. Pass value references through observer notifications and add a generic consuming sentry__value_replace helper for scope value replacement.
Move scope payload access behind sentry_scope_data_t helpers so scope-level code no longer reaches through scope->data to individual fields.
Track active scope accesses and delay cleanup until they finish, while releasing the lifetime guard before backend scope flushing.
Register backend attachment observers for the SDK lifetime and remove the attachment callbacks from sentry_backend_t. Backends now react only after an attachment is committed to the scope. This keeps duplicate detection and backend registration in the same serialized scope operation, preventing concurrent additions from registering the same file twice.
802a09a to
abcc49d
Compare

Warning
WIP 🚧🔨⏳⛔
Mostly curious what the AI review bots have to say for now...
Allow concurrent scope data readers.
TODO
sentry-playstation
sentry-switch
sentry-xbox
Before
After
Close: #1862