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: Threading issues in binary image cache #3468

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions Sources/Sentry/SentryBinaryImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions Tests/SentryTests/SentryBinaryImageCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<count {
DispatchQueue.global().async {
var binaryImage0 = self.createCrashBinaryImage(UInt(i * 10))
self.sut.binaryImageAdded(&binaryImage0)

self.sut.stop()
self.sut.start()
expectation.fulfill()
}
}

waitForExpectations(timeout: 1)
}

func createCrashBinaryImage(_ address: UInt) -> SentryCrashBinaryImage {
let name = "Expected Name at \(address)"
Expand Down
Loading