From abc96214fa7c8d3401935d03b2415012651ad309 Mon Sep 17 00:00:00 2001 From: Nick Dowell Date: Wed, 4 Aug 2021 17:18:34 +0100 Subject: [PATCH] Fix ThreadSanitizer data race in BugsnagBreadcrumbs --- Bugsnag/Breadcrumbs/BugsnagBreadcrumbs.m | 7 +++++-- CHANGELOG.md | 7 +++++++ Tests/BugsnagBreadcrumbsTest.m | 4 ++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Bugsnag/Breadcrumbs/BugsnagBreadcrumbs.m b/Bugsnag/Breadcrumbs/BugsnagBreadcrumbs.m index e1ade0277..1fcce3642 100644 --- a/Bugsnag/Breadcrumbs/BugsnagBreadcrumbs.m +++ b/Bugsnag/Breadcrumbs/BugsnagBreadcrumbs.m @@ -157,8 +157,11 @@ - (void)addBreadcrumbWithData:(NSData *)data writeToDisk:(BOOL)writeToDisk { dispatch_async(BSGGlobalsFileSystemQueue(), ^{ // Avoid writing breadcrumbs that have already been deleted from the in-memory store. // This can occur when breadcrumbs are being added faster than they can be written. - unsigned int nextFileNumber = self.nextFileNumber; - BOOL isStale = (self.maxBreadcrumbs < nextFileNumber) && (fileNumber < (nextFileNumber - self.maxBreadcrumbs)); + BOOL isStale; + @synchronized (self) { + unsigned int nextFileNumber = self.nextFileNumber; + isStale = (self.maxBreadcrumbs < nextFileNumber) && (fileNumber < (nextFileNumber - self.maxBreadcrumbs)); + } NSError *error = nil; diff --git a/CHANGELOG.md b/CHANGELOG.md index a6462f6f1..2d2ec6144 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Changelog ========= +## TBD + +### Bug fixes + +* Fix ThreadSanitizer data race in `BugsnagBreadcrumbs`. + [#1160](https://github.com/bugsnag/bugsnag-cocoa/pull/1160) + ## 6.10.3 (2021-08-04) ### Bug fixes diff --git a/Tests/BugsnagBreadcrumbsTest.m b/Tests/BugsnagBreadcrumbsTest.m index efbe90fb5..52abe2ca8 100644 --- a/Tests/BugsnagBreadcrumbsTest.m +++ b/Tests/BugsnagBreadcrumbsTest.m @@ -451,6 +451,10 @@ - (void)testCrashReportWriter { } - (void)testCrashReportWriterConcurrency { +#if defined(__has_feature) && __has_feature(thread_sanitizer) + NSLog(@"Skipping test because ThreadSanitizer deadlocks if other threads are suspended"); + return; +#endif // // The aim of this test is to ensure that BugsnagBreadcrumbsWriteCrashReport will insert only valid JSON // into a crash report when other threads are (paused while) updating the breadcrumbs linked list.