Background NSURLSession data loss if callback thread blocked

Originator:goedm003
Number:rdar://23478912 Date Originated:10-Nov-2015 02:33 AM
Status:Open Resolved:
Product:iOS SDK Product Version:9.1
Classification:Data loss Reproducible:Always
 
Summary:
When using a background NSURLSession, if a long running task is called synchronously (eg, unzipping a file) in 
URLSession:downloadTask:didFinishDownloadingToURL:
when the app is relaunched in the background, subsequent calls to that method for the other downloads will not run, and the app will never receive notification that those downloads completed.

Steps to Reproduce:
1. Create background session
2. Begin several background downloads (3 is a good demo number for this)
3. Background app
4. Wait for application:handleEventsForBackgroundURLSession:completionHandler: to be called
5. Wait for URLSession:downloadTask:didFinishDownloadingToURL: to be called for the first download
6. Run a long running task (unzipping a file is a good real world example, but you can sleep() to the same effect) from that method
7. App will eventually time out (expected), but the tasks that didn't ever send delegate completed messages are lost forever (unexpected)

Expected Results:
App would be woken up again and notified of subsequent task completion, or notified on next launch. At a minimum, those downloads should receive an error message.

Actual Results:
Tasks are lost. App is never notified of their completion, and they are not recoverable via getTasksWithCompletionHandler:

Obviously the correct solution here is to not block the calling thread, but data loss isn't a great outcome (and SUPER annoying/non-obvious to debug, as there's no errors reported)

Version:
Xcode 7.1.1

Notes:
Sample project: https://github.com/maxdisney/urlsession_loss

Configuration:
Not config dependent, but tested on iOS 9.1 iPhone 6S

Attachments:

Comments


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!