From 853325139f380e992c39e44f5c547929393a7b86 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 27 Dec 2019 14:39:33 +0800 Subject: [PATCH] Fix the thread safe issue with Downloader and DownloaderOperation during cancel #2903 --- SDWebImage/SDWebImageDownloader.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 30a2132a..46c81ed2 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -354,7 +354,12 @@ 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; }