feat: Add pending task queue for SDK initialization#7377
feat: Add pending task queue for SDK initialization#7377
Conversation
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Internal Changes 🔧
🤖 This preview updates automatically when you update the PR. |
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7377 +/- ##
=============================================
+ Coverage 85.268% 85.314% +0.045%
=============================================
Files 481 482 +1
Lines 28599 28634 +35
Branches 12443 12461 +18
=============================================
+ Hits 24386 24429 +43
+ Misses 4170 4162 -8
Partials 43 43
... and 3 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 17b7e88 | 1223.04 ms | 1253.48 ms | 30.43 ms |
| 29f846e | 1210.51 ms | 1238.66 ms | 28.15 ms |
| d427374 | 1226.81 ms | 1257.74 ms | 30.94 ms |
| 2f28bda | 1221.21 ms | 1255.09 ms | 33.88 ms |
| a424cf3 | 1231.36 ms | 1259.62 ms | 28.25 ms |
| 7e93d85 | 1205.28 ms | 1243.71 ms | 38.44 ms |
| 86b8951 | 1211.20 ms | 1247.92 ms | 36.72 ms |
| 2c4362a | 1231.50 ms | 1255.95 ms | 24.45 ms |
| 24ac927 | 1218.04 ms | 1242.02 ms | 23.98 ms |
| 1357911 | 1224.57 ms | 1261.00 ms | 36.43 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 17b7e88 | 24.14 KiB | 1.06 MiB | 1.04 MiB |
| 29f846e | 24.14 KiB | 1.07 MiB | 1.04 MiB |
| d427374 | 24.14 KiB | 1.09 MiB | 1.07 MiB |
| 2f28bda | 24.14 KiB | 1.05 MiB | 1.03 MiB |
| a424cf3 | 24.14 KiB | 1.06 MiB | 1.04 MiB |
| 7e93d85 | 24.14 KiB | 1.06 MiB | 1.04 MiB |
| 86b8951 | 24.14 KiB | 1.08 MiB | 1.06 MiB |
| 2c4362a | 24.14 KiB | 1.07 MiB | 1.04 MiB |
| 24ac927 | 24.14 KiB | 1.06 MiB | 1.04 MiB |
| 1357911 | 24.14 KiB | 1.07 MiB | 1.04 MiB |
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.
| // This handles the race condition when SDK is started on a background thread | ||
| // and methods like setUser are called before main thread initialization finishes. | ||
| // See https://github.com/getsentry/sentry-cocoa/issues/6872 | ||
| [SentryDependencyContainer.sharedInstance.pendingTaskQueue executePendingTasks]; |
There was a problem hiding this comment.
Race condition causes incorrect ordering of setUser operations
Medium Severity
There's a race condition window between setCurrentHub (line 230) and executePendingTasks (line 236) where isEnabled returns true but pending tasks haven't executed yet. If setUser is called from another thread during this window, it calls the hub directly while older queued tasks are still pending. When executePendingTasks subsequently runs, it executes the older tasks, potentially overwriting the newer value with stale data. This can cause the wrong user to be set when multiple setUser calls happen around SDK initialization time.
Additional Locations (1)
|
While I see this is still a draft, I would like to raise two concerns/topics:
|
…nner/cocoa-1010-setuser-not-work-after-upgade-to-8540
Yeah, that's why I moved it back to draft.
We can go down a rabbithole with enabling initializing the SDK from a background thread has 2 problems:
This fixed setting the user, but for example configuring the scope is harder to fix here. I would recommend moving slowly and as needed |
|
I’m not 100% sure this solution actually addresses the root cause. The core issue seems to be a race condition in I’m mostly brainstorming here, but I see a few possible options — none of them are perfect: Option A: Install the hub immediately in
|
I don't believe there is going to be any perfect solution here. Doing an async initialization will always introduce problems, this is why I went for a solution that was the most friendly with the user:
|


📜 Description
Fixes setUser not working when the SDK is started on a background thread.
When calling SentrySDK.start() on a background thread, the SDK dispatches initialization work to the main thread. During this window, isEnabled returns false, causing setUser calls to be silently ignored. This PR introduces a pending task queue that stores these operations and executes them once the SDK is fully initialized.
💡 Motivation and Context
When users start the SDK on a background thread and immediately call setUser, there's a race condition where the main thread initialization hasn't completed yet. Previously, this would just log a fatal message and the user would never be set. Now, the operation is queued and executed once initialization completes.
Fixes #6872
💚 How did you test it?
📝 Checklist
You have to check all boxes before merging:
- I added tests to verify the changes.
- No new PII added or SDK only sends newly added PII if
- [] I updated the docs if needed.
- I updated the wizard if needed.
- Review from the native team if needed.
- No breaking change or entry added to the changelog.
- No breaking change for hybrid SDKs or communicated to hybrid SDKs.
Cursor Bugbot found 1 potential issue for commit e438753sendDefaultPIIis enabled.