NSURLSession's delegate methods have no order guarantees

Originator:henriqueponde90
Number:rdar://30450934 Date Originated:02/09/2017
Status:Open Resolved:No
Product:iOS + SDK Product Version:10.2
Classification:Bug Reproducible:Sometimes
 
Area:
Something not on this list

Summary:
There is no guarantee between the ordering of the callback in `getTasksWithCompletionHandler` and the calling of `didBecomeInvalidWithError` if the session is being used in the background by another extension. And that complicated the logic tremendously, since we can't differentiate between

Steps to Reproduce:
1. Create an NSURLSession in an Extension
2. In the main app, on foreground, try to create a session with that session identifier (to reconnect to tasks)
3. If the Extension's NSURLSession is still alive, then the main app's session will sometimes (rdar://28352195) send the delegate method `didBecomeInvalidWithError`.
4. If we call `getTasksWithCompletionHandler` on the main app, that block might be called before `didBecomeInvalidWithError` is invoked.

Note that that call is not even guaranteed. It seemed to be consistent in iOS8 but not anymore. However, even when it is called, there are no ordering guarantees between it and `getTasksWithCompletionHandler` for instance.

Expected Results:
I'd like for the undefined behavior (rdar://28352195) to be fixed, and even then, for there to be some guarantee in the ordering of the calls. That is, the completion of `getTasksWithCompletionHandler` and when `didBecomeInvalidWithError` gets called. Essentially `didBecomeInvalidWithError` should be called first, so that we can differentiate between the session being owned by another process vs the session having no tasks.

Actual Results:
We get that the callback to `didBecomeInvalidWithError` can happen latter than `getTasksWithCompletionHandler`. So essentially we need to wait before possibly `didBecomeInvalidWithError` gets called before determining if tasks don't exist vs the session is in use from another process.

If there was a order guarantee this logic would be so much simpler.

Version:
iOS 10.2

Notes:


Configuration:
iPhone 6S Wi-Fi

Attachments:

Comments

From Apple's responses it seems that this is something to be worked around:

https://forums.developer.apple.com/message/199637 https://forums.developer.apple.com/thread/11554

By henriqueponde90 at Feb. 9, 2017, 10:10 p.m. (reply...)

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!