Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Potential deadlock when starting the SDK #3970

Merged
merged 1 commit into from
May 13, 2024
Merged

Conversation

philipphofmann
Copy link
Member

@philipphofmann philipphofmann commented May 13, 2024

📜 Description

Fix a deadlock when two threads access SentrySDK.options and SentrySDK.currentHub, which used the same object in synchronized. This problem is fixed now by using two independent locks for SentrySDK.options and SentrySDK.currentHub.

💡 Motivation and Context

Fixes GH-3956, Fixes GH-3899

💚 How did you test it?

Unit tests and as explained in #3956.

📝 Checklist

You have to check all boxes before merging:

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs 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.

🔮 Next steps

Fix a deadlock when two threads access SentrySDK.options and
SentrySDK.currentHub, which used the same object in synchronized.
This problem is fixed now by using two independent locks for
SentrySDK.options and SentrySDK.currentHub.

Fixes GH-3956, GH-3899
Copy link

codecov bot commented May 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.850%. Comparing base (e072ad1) to head (f3f0c40).

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #3970       +/-   ##
=============================================
+ Coverage   90.844%   90.850%   +0.005%     
=============================================
  Files          593       594        +1     
  Lines        45974     46078      +104     
  Branches     16383     16436       +53     
=============================================
+ Hits         41765     41862       +97     
+ Misses        4139      4036      -103     
- Partials        70       180      +110     
Files Coverage Δ
Sources/Sentry/SentrySDK.m 86.335% <100.000%> (+0.171%) ⬆️
Tests/SentryTests/SentrySDKTests.swift 95.171% <100.000%> (+0.160%) ⬆️

... and 40 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e072ad1...f3f0c40. Read the comment docs.

Copy link

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1228.94 ms 1245.88 ms 16.94 ms
Size 21.58 KiB 625.82 KiB 604.24 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
7bb0873 1226.18 ms 1247.30 ms 21.12 ms
dc0db9e 1246.06 ms 1260.46 ms 14.40 ms
26af50b 1223.78 ms 1245.93 ms 22.16 ms
62c15d4 1235.30 ms 1260.82 ms 25.52 ms
b35ccd0 1233.92 ms 1256.69 ms 22.78 ms
3f366ee 1242.28 ms 1260.80 ms 18.52 ms
b2f82fa 1236.94 ms 1262.86 ms 25.92 ms
904d7fa 1225.73 ms 1249.22 ms 23.49 ms
42ef6ba 1201.67 ms 1214.35 ms 12.67 ms
6c31077 1226.24 ms 1237.24 ms 11.00 ms

App size

Revision Plain With Sentry Diff
7bb0873 22.85 KiB 407.09 KiB 384.24 KiB
dc0db9e 20.76 KiB 419.62 KiB 398.86 KiB
26af50b 21.58 KiB 542.38 KiB 520.80 KiB
62c15d4 22.85 KiB 411.14 KiB 388.29 KiB
b35ccd0 21.58 KiB 573.13 KiB 551.55 KiB
3f366ee 20.76 KiB 427.84 KiB 407.08 KiB
b2f82fa 20.76 KiB 419.62 KiB 398.86 KiB
904d7fa 20.76 KiB 432.87 KiB 412.11 KiB
42ef6ba 21.58 KiB 417.86 KiB 396.28 KiB
6c31077 22.84 KiB 401.66 KiB 378.81 KiB

Copy link
Contributor

@brustolin brustolin left a comment

Choose a reason for hiding this comment

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

This probably solve the problem. But Im wondering whether reading the options need to be synchronised. We never change it or one of its properties, and the two functions related to options changed in this PR are internal.

@philipphofmann
Copy link
Member Author

But Im wondering whether reading the options need to be synchronised.

That's an excellent question. We access the options in various places in the SDK, and if users access things like captureMessage, which calls to the currentHub, on a background thread while starting the SDK, we could run into race conditions or other bugs. The overhead should be minimal, so I think it's good to keep the synchronization for now.

@philipphofmann philipphofmann merged commit 3b4110a into main May 13, 2024
70 of 71 checks passed
@philipphofmann philipphofmann deleted the fix/deadlock branch May 13, 2024 12:48
threema-matteo pushed a commit to threema-ch/sentry-cocoa that referenced this pull request May 21, 2024
Fix a deadlock when two threads access SentrySDK.options and
SentrySDK.currentHub, which used the same object in synchronized.
This problem is fixed now by using two independent locks for
SentrySDK.options and SentrySDK.currentHub.

Fixes getsentryGH-3956, Fixes getsentryGH-3899
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants