Skip to content

Commit 65e48e0

Browse files
authored
ref: Make SentrySDK.replay.start() thread safe (#4455)
Making SentrySDK.replay.start() thread safe to avoid multiple initialization
1 parent 99fe600 commit 65e48e0

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- Speed up getBinaryImages (#4435) for finishing transactions and capturing events
3232
- Align SDK dispatch queue names (#4442) to start with `io.sentry`
3333
- Use UInts in envelope deserialization (#4441)
34+
- Make `SentrySDK.replay.start()` thread safe (#4455)
3435

3536
## 8.38.0
3637

Sources/Sentry/SentryReplayApi.m

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#if SENTRY_TARGET_REPLAY_SUPPORTED
44

55
# import "SentryHub+Private.h"
6+
# import "SentryInternalCDefines.h"
67
# import "SentryOptions+Private.h"
78
# import "SentrySDK+Private.h"
89
# import "SentrySessionReplayIntegration+Private.h"
@@ -37,21 +38,30 @@ - (void)resume
3738
[replayIntegration resume];
3839
}
3940

40-
- (void)start
41+
- (void)start SENTRY_DISABLE_THREAD_SANITIZER("double-checked lock produce false alarms")
4142
{
4243
SentrySessionReplayIntegration *replayIntegration
4344
= (SentrySessionReplayIntegration *)[SentrySDK.currentHub
4445
getInstalledIntegration:SentrySessionReplayIntegration.class];
4546

47+
// Start could be misused and called multiple times, causing it to
48+
// be initialized more than once before being installed.
49+
// Synchronizing it will prevent this problem.
4650
if (replayIntegration == nil) {
47-
SentryOptions *currentOptions = SentrySDK.currentHub.client.options;
48-
replayIntegration =
49-
[[SentrySessionReplayIntegration alloc] initForManualUse:currentOptions];
51+
@synchronized(self) {
52+
replayIntegration = (SentrySessionReplayIntegration *)[SentrySDK.currentHub
53+
getInstalledIntegration:SentrySessionReplayIntegration.class];
54+
if (replayIntegration == nil) {
55+
SentryOptions *currentOptions = SentrySDK.currentHub.client.options;
56+
replayIntegration =
57+
[[SentrySessionReplayIntegration alloc] initForManualUse:currentOptions];
5058

51-
[SentrySDK.currentHub addInstalledIntegration:replayIntegration
52-
name:NSStringFromClass(SentrySessionReplay.class)];
59+
[SentrySDK.currentHub
60+
addInstalledIntegration:replayIntegration
61+
name:NSStringFromClass(SentrySessionReplay.class)];
62+
}
63+
}
5364
}
54-
5565
[replayIntegration start];
5666
}
5767

0 commit comments

Comments
 (0)