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

Replace the entire observers list when adding/removing observers in o… #796

Merged
merged 1 commit into from
Sep 9, 2020
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
12 changes: 9 additions & 3 deletions Bugsnag/Metadata/BugsnagMetadata.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#import "BugsnagStateEvent.h"

@interface BugsnagMetadata ()
@property(nonatomic, readwrite, strong) NSMutableArray *stateEventBlocks;
@property(atomic, readwrite, strong) NSMutableArray *stateEventBlocks;
@end

@implementation BugsnagMetadata
Expand Down Expand Up @@ -102,11 +102,17 @@ - (void)notifyObservers {
}

- (void)addObserverWithBlock:(BugsnagObserverBlock _Nonnull)block {
[self.stateEventBlocks addObject:[block copy]];
// Make a copy to avoid concurrency issues
NSMutableArray *newStateEventBlocks = [self.stateEventBlocks mutableCopy];
[newStateEventBlocks addObject:[block copy]];
self.stateEventBlocks = newStateEventBlocks;
}

- (void)removeObserverWithBlock:(BugsnagObserverBlock _Nonnull)block {
[self.stateEventBlocks removeObject:block];
// Make a copy to avoid concurrency issues
NSMutableArray *newStateEventBlocks = [self.stateEventBlocks mutableCopy];
[newStateEventBlocks removeObject:block];
self.stateEventBlocks = newStateEventBlocks;
}

// MARK: - <NSMutableCopying>
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog

### Bug fixes

* Copy the metadata observer list rather than mutating it directly.
[796](https://github.com/bugsnag/bugsnag-cocoa/pull/796)

* Reorganized the project file
[793](https://github.com/bugsnag/bugsnag-cocoa/pull/793)

Expand Down
34 changes: 34 additions & 0 deletions Tests/BugsnagMetadataTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,38 @@ - (void)testSanitizeNestedArrayDict {
XCTAssertEqual(0, [nestedDict count]);
}

- (void)testConcurrentObserverAccess {
BugsnagMetadata *metadata = [[BugsnagMetadata alloc] initWithDictionary:@{
@"foo": @{
@"bar": @[
@[
@{ @"custom": [NSNull null] }
]
]
}
}];

BugsnagObserverBlock firstObserver = ^(BugsnagStateEvent *_Nonnull event){};
BugsnagObserverBlock secondObserver = ^(BugsnagStateEvent *_Nonnull event){};

[metadata addObserverWithBlock:firstObserver];

bool threadShouldQuit = false;
if (@available(iOS 10.0, tvOS 10.0, *)) {
[NSThread detachNewThreadWithBlock:^{
while(!threadShouldQuit) {
[metadata addObserverWithBlock:secondObserver];
[metadata removeObserverWithBlock:secondObserver];
}
}];
} else {
// Fallback on earlier versions
}

for(int i = 0; i < 10000; i++) {
[metadata addMetadata:[NSNumber numberWithInt:i] withKey:@"bar" toSection:@"foo"];
}
threadShouldQuit = true;
}

@end