didFinishDownloadingToURL: provides invalid URL when running with debugger

Originator:stephen.gazzard
Number:rdar://21950485 Date Originated:22-Jul-2015
Status:Open Resolved:
Product:iOS Product Version:8.4
Classification:Bug Reproducible:Always
 
Summary:
I have been working on implementing NSURLSession with background downloads. Things have been going mostly well as I learn the ins and outs but I ran into a situation that is a little bit strange.

It is possible, as a result of user action, for the user to sometimes trigger a download that may already be in progress in the background if they open the app after it has been shut down. We handle this correctly (we don't actually queue new tasks, for example), but when we create our NSURLSession, we immediately get a ton of delegate callbacks.

As best as I understand how this all works, this is actually the correct behaviour (our new session is associated with the one that has been working in the background all this time). The problem is that in this delegate:

`- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location`

The location property points to a file that doesn't exist! This only seems to happen when running from code (in simulator or on device) and I was able to verify, when running in simulator by actually looking in the file system, that the containing folder was missing entirely. On the device I was able to confirm the same using lldb, NSFileManager and breakpoints. 

The documentation states that the file should exist at that location until the method returns but in these cases, the method has not yet returned, nor has it spun work up onto another task. If I launch the app from springboard with a bunch of logs in and watch the logs in Xcode, I see that it actually works as expected.

It seems that something in the process of installing from Xcode is causing that directory to be erased (as my best guess?), yet the delegate callbacks are still being received.

Steps to Reproduce:
1. Set up an app to use NSURLSession with background downloads
2. Launch the app from Xcode
3. Start several download tasks in the NSURLSession in the background
4. Terminate the app in some way (I used Xcodes stop button sometimes but I also tried inserting a button that crashed my app)
5. Re-run the app from Xcode
6. Re-create an NSURLSession with the same identifier

You will get a lot of callbacks immediately but there will be no file at the path specified. According to the docs, that file should exist until the method returns.

Expected Results:
I expect the path variable passed into the delegate callback to point at the downloaded file.

Actual Results:
You will get a lot of callbacks immediately but there will be no file at the path specified. According to the docs, that file should exist until the method returns.

Version:
iOS 8.4 / 8.3, Xcode Version 6.4 (6E35b),

Notes:


Configuration:
iPhone 6 128GB using wifi, iOS Simulator (any model seems to do it)

Attachments:

Comments

I have exactly the same issue on iOS 10 and watchOS 3 simulator.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!