-
Notifications
You must be signed in to change notification settings - Fork 318
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
Fix race condition in ImageDownloader
#4587
Conversation
@@ -6,7 +6,7 @@ typealias CachedResponseCompletionHandler = (CachedURLResponse?, Error?) -> Void | |||
typealias ImageDownloadCompletionHandler = (DownloadError?) -> Void | |||
|
|||
protocol ReentrantImageDownloader { | |||
func download(with url: URL, completion: CachedResponseCompletionHandler?) -> Void | |||
func download(with url: URL, completion: @escaping CachedResponseCompletionHandler) -> Void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why we would ever want completion to be nil, changed to non-nil.
Codecov ReportAttention:
@@ Coverage Diff @@
## main #4587 +/- ##
==========================================
- Coverage 59.79% 1.07% -58.72%
==========================================
Files 189 189
Lines 21218 21215 -3
==========================================
- Hits 12687 228 -12459
- Misses 8531 20987 +12456
|
Closing in favor of #4588 |
ImageDownloader uses a custom
NSOperation
subclass to asynchronously download images.Instead of overriding
isConcurrent
flag, this subclass has to overrideisAsynchronous
flag.isConcurrent
is a legacy flag, from the tests I did it seems it's never called by the system.It seems like right now the fact that we don't correctly set
isAsynchronous
can lead to crashes in some cases because by default all NSOperations are considered synchronous and iOS can deallocate them before the download is actually finished. This should be fixed by correctly marking our NSOperation asisAsynchronous == true
.I also fixed another potential issue in
ImageDownloader
where we add a completion handler to an operation only after adding this operation to a queue, in theory this might lead to a race, though I think this could never cause issues in production.Another issue yet unfixed is:
It happens because our custom subclass of NSOperation doesn't handle correctly a state when it's cancelled before NSOperationQueue started it.