Skip to content
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

Thread race on HWIFileDownloader.activeDownloadsDictionary #42

Open
rhcpfan opened this issue May 28, 2019 · 0 comments
Open

Thread race on HWIFileDownloader.activeDownloadsDictionary #42

rhcpfan opened this issue May 28, 2019 · 0 comments

Comments

@rhcpfan
Copy link

rhcpfan commented May 28, 2019

I'm getting thread race warnings for HWIFileDownloader.activeDownloadsDictionary (of type NSMutableDictionary<NSNumber *, HWIFileDownloadItem *>).

When I open my application, I verify if a certain number of files are downloaded on the device storage (±80 files). For the ones that are missing, I'm starting the download process (in parallel).

The race condition that I get is related to

- (void)startDownloadWithDownloadToken:(nonnull NSString *)aDownloadToken
                         fromRemoteURL:(nullable NSURL *)aRemoteURL
                       usingResumeData:(nullable NSData *)aResumeData

At line 313, we add the aDownloadItem in the activeDownloadsDictionary ([self.activeDownloadsDictionary setObject:aDownloadItem forKey:@(aDownloadID)];)

At the same time, on another thread, a download has finished and

- (void)handleSuccessfulDownloadToLocalFileURL:(nonnull NSURL *)aLocalFileURL
                                  downloadItem:(nonnull HWIFileDownloadItem *)aDownloadItem
                                    downloadID:(NSUInteger)aDownloadID

is called. Inside this method, we remove the download item from the activeDownloadsDictionary ([self.activeDownloadsDictionary removeObjectForKey:@(aDownloadID)];).

Here's the full Thread Sanitizer output

==================
WARNING: ThreadSanitizer: race on NSMutableDictionary (pid=18849)
  Modifying access of NSMutableDictionary at 0x7b08002e6180 by thread T10:
    #0 -[__NSDictionaryM setObject:forKey:] <null> (CoreFoundation:x86_64+0x19160b)
    #1 -[HWIFileDownloader startDownloadWithDownloadToken:fromRemoteURL:usingResumeData:] <null> (HWIFileDownload:x86_64+0x63ba)
    #2 -[HWIFileDownloader startDownloadWithIdentifier:fromRemoteURL:] <null> (HWIFileDownload:x86_64+0x4848)
    #3 -[DVTemplateDownloadStore startDownloadForTemplateFileModel:] <null>
    #4 -[DVTemplateDownloadStore startDownloadingIncompleteFilesInTemplateArray:] <null>
    #5 __52-[DVMainViewController verifyUserTemplatesIntegrity]_block_invoke <null>
    #6 __tsan::invoke_and_release_block(void*) <null> (libclang_rt.tsan_iossim_dynamic.dylib:x86_64+0x676ab)
    #7 _dispatch_client_callout <null> (libdispatch.dylib:x86_64+0x3db4)

  Previous modifying access of NSMutableDictionary at 0x7b08002e6180 by main thread:
    #0 -[__NSDictionaryM removeObjectForKey:] <null> (CoreFoundation:x86_64+0x1912ef)
    #1 -[HWIFileDownloader handleSuccessfulDownloadToLocalFileURL:downloadItem:downloadID:] <null> (HWIFileDownload:x86_64+0x1a6fd)
    #2 -[HWIFileDownloader URLSession:task:didCompleteWithError:] <null> (HWIFileDownload:x86_64+0x105f9)
    #3 __51-[NSURLSession delegate_task:didCompleteWithError:]_block_invoke.182 <null> (CFNetwork:x86_64+0x1bf001)
    #4 _dispatch_client_callout <null> (libdispatch.dylib:x86_64+0x3db4)
    #5 start <null> (libdyld.dylib:x86_64+0x1540)

  Location is heap block of size 32 at 0x7b08002e6180 allocated by main thread:
    #0 calloc <null> (libclang_rt.tsan_iossim_dynamic.dylib:x86_64+0x4a6b2)
    #1 class_createInstance <null> (libobjc.A.dylib:x86_64+0xf34f)
    #2 -[HWIFileDownloader initWithDelegate:maxConcurrentDownloads:] <null> (HWIFileDownload:x86_64+0x1b4b)
    #3 -[HWIFileDownloader initWithDelegate:] <null> (HWIFileDownload:x86_64+0x179f)
    #4 __40+[DVTemplateDownloadStore sharedManager]_block_invoke <null>
    #5 dispatch_once <null> (libclang_rt.tsan_iossim_dynamic.dylib:x86_64+0x682d4)
    #6 +[DVTemplateDownloadStore sharedManager] <null>
    #7 -[TemplateModel isDownloaded] <null>
    #8 -[DVMainViewController initTemplateView] <null> 
    #9 -[DVMainViewController viewWillAppear:] <null>
    #10 -[UIViewController _setViewAppearState:isAnimating:] <null> (UIKitCore:x86_64+0x351436)
    #11 start <null> (libdyld.dylib:x86_64+0x1540)

  Thread T10 (tid=1095123, running) is a GCD worker thread

SUMMARY: ThreadSanitizer: race on NSMutableDictionary (/Applications/Xcode-10.2.1.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation:x86_64+0x19160b) in -[__NSDictionaryM setObject:forKey:]
==================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant