Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[ios] Fix a race condition issue in the Mapbox metrics class. #15029

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 11 additions & 6 deletions platform/darwin/src/MGLNetworkConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ - (NSURLSessionConfiguration *)defaultSessionConfiguration {
}

- (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType {
if (urlString && ![self.events objectForKey:urlString]) {
[self.events setObject:@{ MGLStartTime: [NSDate date], MGLResourceType: resourceType } forKey:urlString];
NSString *retainedUrlString = urlString;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is sufficient. AFAIK urlString should be retained by this method anyway - how about using copy here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider using copy but an increase in the retain count would do the job better.

NSString *retainedResourceType = resourceType;
if (retainedUrlString && !self.events[retainedUrlString]) {
self.events[retainedUrlString] = @{ MGLStartTime: [NSDate date], MGLResourceType: retainedResourceType };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to change the events property to be atomic:

@property (atomic, strong) NSMutableDictionary<NSString*, NSDictionary*> *events;

since reading/writing/deleting could happen at the same time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property gets called multiple times. I consider the atomicity but in practice found no reason to make this an atomic property when the crash is raised for a different reason.

}
retainedUrlString = nil;
retainedResourceType = nil;
}

- (void)stopDownloadEventForResponse:(NSURLResponse *)response {
Expand All @@ -80,15 +84,16 @@ - (void)cancelDownloadEventForResponse:(NSURLResponse *)response {

- (void)sendEventForURLResponse:(NSURLResponse *)response withAction:(NSString *)action
{
if ([response isKindOfClass:[NSURLResponse class]]) {
NSString *urlString = response.URL.relativePath;
NSURLResponse *retainedResponse = response;
if ([retainedResponse isKindOfClass:[NSURLResponse class]]) {
NSString *urlString = retainedResponse.URL.relativePath;
if (urlString && [self.events objectForKey:urlString]) {
NSDictionary *eventAttributes = [self eventAttributesForURL:response withAction:action];
NSDictionary *eventAttributes = [self eventAttributesForURL:retainedResponse withAction:action];
[self.metricsDelegate networkConfiguration:self didGenerateMetricEvent:eventAttributes];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should dispatch this delegate call on the main queue.

[self.events removeObjectForKey:urlString];
}
}

retainedResponse = nil;
}

- (NSDictionary *)eventAttributesForURL:(NSURLResponse *)response withAction:(NSString *)action
Expand Down
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
### Offline maps

* Fixed an issue where offline regions could report the wrong number of tiles. ([#14958](https://github.com/mapbox/mapbox-gl-native/pull/14958))
* Fixed an issue that may cause a crash when downloading a list of `MGLOfflinePack` objects. ([#15029](https://github.com/mapbox/mapbox-gl-native/pull/15029))

### Packaging

Expand Down