From 9dae0e7b961ba5c6dda545ec2c6a40733babdd79 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 5 Dec 2019 19:33:10 +0800 Subject: [PATCH 1/2] Fix the thread safe issue with Downloader and DownloaderOperation during cancel --- SDWebImage/Core/SDWebImageDownloader.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/SDWebImage/Core/SDWebImageDownloader.m b/SDWebImage/Core/SDWebImageDownloader.m index 80d30b59..5b931ea2 100644 --- a/SDWebImage/Core/SDWebImageDownloader.m +++ b/SDWebImage/Core/SDWebImageDownloader.m @@ -403,7 +403,12 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext; NSOperation *returnOperation = nil; for (NSOperation *operation in self.downloadQueue.operations) { if ([operation respondsToSelector:@selector(dataTask)]) { - if (operation.dataTask.taskIdentifier == task.taskIdentifier) { + // So we lock the operation here, and in `SDWebImageDownloaderOperation`, we use `@synchonzied (self)`, to ensure the thread safe between these two classes. + NSURLSessionTask *operationTask; + @synchronized (operation) { + operationTask = operation.dataTask; + } + if (operationTask.taskIdentifier == task.taskIdentifier) { returnOperation = operation; break; } From 1601418d51e947cf4f8a9884820e8114eb334a43 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 5 Dec 2019 19:40:45 +0800 Subject: [PATCH 2/2] Remove one unused import --- SDWebImage/Core/SDWebImageDefine.m | 1 - 1 file changed, 1 deletion(-) diff --git a/SDWebImage/Core/SDWebImageDefine.m b/SDWebImage/Core/SDWebImageDefine.m index 173f092a..496392c4 100644 --- a/SDWebImage/Core/SDWebImageDefine.m +++ b/SDWebImage/Core/SDWebImageDefine.m @@ -9,7 +9,6 @@ #import "SDWebImageDefine.h" #import "UIImage+Metadata.h" #import "NSImage+Compatibility.h" -#import "UIImage+ExtendedCacheData.h" #import "SDAssociatedObject.h" #pragma mark - Image scale