diff --git a/CHANGELOG.md b/CHANGELOG.md index c6b9d3f1dc7..26c9606588d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Crash when UINavigationController doesn't have rootViewController (#3455) - Crash when synchronizing invalid JSON breadcrumbs to SentryWatchdogTermination (#3458) +- Threading issues in binary image cache (#3468) - Finish transaction for external view controllers (#3440) ## 8.17.0 diff --git a/Sources/Sentry/SentryBinaryImageCache.m b/Sources/Sentry/SentryBinaryImageCache.m index c9d05365187..ab34a36f708 100644 --- a/Sources/Sentry/SentryBinaryImageCache.m +++ b/Sources/Sentry/SentryBinaryImageCache.m @@ -21,16 +21,20 @@ @implementation SentryBinaryImageCache - (void)start { - _cache = [NSMutableArray array]; - sentrycrashbic_registerAddedCallback(&binaryImageWasAdded); - sentrycrashbic_registerRemovedCallback(&binaryImageWasRemoved); + @synchronized(self) { + _cache = [NSMutableArray array]; + sentrycrashbic_registerAddedCallback(&binaryImageWasAdded); + sentrycrashbic_registerRemovedCallback(&binaryImageWasRemoved); + } } - (void)stop { - sentrycrashbic_registerAddedCallback(NULL); - sentrycrashbic_registerRemovedCallback(NULL); - _cache = nil; + @synchronized(self) { + sentrycrashbic_registerAddedCallback(NULL); + sentrycrashbic_registerRemovedCallback(NULL); + _cache = nil; + } } - (void)binaryImageAdded:(const SentryCrashBinaryImage *)image diff --git a/Tests/SentryTests/SentryBinaryImageCacheTests.swift b/Tests/SentryTests/SentryBinaryImageCacheTests.swift index 86c800aa02e..f17ad2186cb 100644 --- a/Tests/SentryTests/SentryBinaryImageCacheTests.swift +++ b/Tests/SentryTests/SentryBinaryImageCacheTests.swift @@ -115,6 +115,26 @@ class SentryBinaryImageCacheTests: XCTestCase { let didNotFind = sut.pathFor(inAppInclude: "Name at 0") expect(didNotFind) == nil } + + func testAddingImagesWhileStoppingAndStartingOnDifferentThread() { + let count = 1_000 + + let expectation = expectation(description: "Add images on background thread") + expectation.expectedFulfillmentCount = count + + for i in 0.. SentryCrashBinaryImage { let name = "Expected Name at \(address)"