Skip to content

Commit

Permalink
Merge pull request #1632 from bugsnag/PLAT-11642-bugsnagsessiontracke…
Browse files Browse the repository at this point in the history
…r-race-condition

[PLAT-11642] BugsnagSessionTracker race condition
  • Loading branch information
kstenerud authored Feb 27, 2024
2 parents 8f9733c + a4cc872 commit 920af29
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Bugsnag/BugsnagSessionTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ - (void)startNewSession {
config:self.config
codeBundleId:self.codeBundleId];
BugsnagDevice *device = [BugsnagDevice deviceWithKSCrashReport:@{@"system": systemInfo}];
[device appendRuntimeInfo:self.extraRuntimeInfo];
@synchronized (self.extraRuntimeInfo) {
[device appendRuntimeInfo:self.extraRuntimeInfo];
}

BugsnagSession *newSession = [[BugsnagSession alloc] initWithId:[[NSUUID UUID] UUIDString]
startedAt:[NSDate date]
Expand Down Expand Up @@ -194,7 +196,9 @@ - (void)startNewSession {
- (void)addRuntimeVersionInfo:(NSString *)info
withKey:(NSString *)key {
if (info != nil && key != nil) {
self.extraRuntimeInfo[key] = info;
@synchronized (self.extraRuntimeInfo) {
self.extraRuntimeInfo[key] = info;
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions Tests/BugsnagTests/BugsnagSessionTrackerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,31 @@ - (void)testStartInForeground {
XCTAssertNotNil(self.sessionTracker.runningSession, @"There should be a running session after starting tracker in foreground");
}

- (void)testMultithreadedAddRuntimeVersionInfo {
NSCondition *endCondition = [NSCondition new];
__block int runningThreads = 0;
BugsnagConfiguration *config = nil;
BugsnagSessionTracker *sessionTracker = [[BugsnagSessionTracker alloc] initWithConfig:config client:nil];

for (int i = 0; i < 100; i++) {
NSString *info = [NSString stringWithFormat:@"Info %d", i];
NSString *key = [NSString stringWithFormat:@"Key %d", i];
NSThread *thread = [[NSThread alloc] initWithBlock:^{
runningThreads++;
for(int j = 0; j < 10000; j++) {
[sessionTracker addRuntimeVersionInfo:info withKey:key];
}
if (--runningThreads <= 0) {
[endCondition signal];
}
}];
[thread start];
}

[NSThread sleepForTimeInterval:0.1];
while (runningThreads > 0) {
[endCondition wait];
}
}

@end

0 comments on commit 920af29

Please sign in to comment.