Skip to content

Commit

Permalink
Safely get mergedTasks On responseQueue
Browse files Browse the repository at this point in the history
There is a race condition in -[AFImageDownloader downloadImageForURLRequest:withReceiptID:success:failure:] that can cause a crash.

#3876
#3984
  • Loading branch information
Brian Beversdorf committed Jul 12, 2018
1 parent 009e3bb commit 1c52c76
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion UIKit+AFNetworking/AFImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
dispatch_async(self.responseQueue, ^{
__strong __typeof__(weakSelf) strongSelf = weakSelf;
AFImageDownloaderMergedTask *mergedTask = strongSelf.mergedTasks[URLIdentifier];
AFImageDownloaderMergedTask *mergedTask = [strongSelf safelyGetMergedTask: URLIdentifier];
if ([mergedTask.identifier isEqual:mergedTaskIdentifier]) {
mergedTask = [strongSelf safelyRemoveMergedTaskWithURLIdentifier:URLIdentifier];
if (error) {
Expand Down Expand Up @@ -404,6 +404,14 @@ - (BOOL)isActiveRequestCountBelowMaximumLimit {
return self.activeRequestCount < self.maximumActiveDownloads;
}

- (AFImageDownloaderMergedTask *)safelyGetMergedTask:(NSString *)URLIdentifier {
__block AFImageDownloaderMergedTask *mergedTask;
dispatch_sync(self.synchronizationQueue, ^(){
mergedTask = self.mergedTasks[URLIdentifier];
});
return mergedTask;
}

@end

#endif

0 comments on commit 1c52c76

Please sign in to comment.