I have been battling this intermittent error for some time. It is generally random and has been difficult to reproduce until yesterday when I stumbled across a way to reproduce it each time. I can cause the code to throw this error:
Task <70E3909F-8C30-4F34-A8B0-4AF3B41DD81B>.<1> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2103, _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundDownloadTask <70E3909F-8C30-4F34-A8B0-4AF3B41DD81B>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "BackgroundDownloadTask <70E3909F-8C30-4F34-A8B0-4AF3B41DD81B>.<1>", "LocalDownloadTask <70E3909F-8C30-4F34-A8B0-4AF3B41DD81B>.<1>" ), NSLocalizedDescription=The request timed out., _kCFStreamErrorDomainKey=4, NSErrorFailingURLStringKey=https://redacted*, NSErrorFailingURLKey=https://redacted*} *"redacted" is the backend URL, and it is the correct and same path for each
immediately after restarting an actual device. I have been over the following threads with no results:
What is kCFStreamErrorCodeKey=-4 (kCFStreamErrorDomainKey=4)
Request timed out with _kCFStreamErrorCodeKey=60
How to better diagnose -1001 "The request timed out." URLSession errors
Random timed out error on app start
Because I was able to reproduce it, I have been able to get the following logs:
Last bit of information is that I had Network Instruments running, and when this error occurred, I found that the Connection ID was "No Connection" and it appears the request was never actually sent, though it waited the full time out for a backend response.
Any help would be appreciated. This data request is being used after sending a certain APNs to update necessary data in the background, and has been the source of many user complaints.
Thanks for the response. Originally, the timeoutIntervalForResource was set to 60 seconds, and the timeoutIntervalForRequest was default. We were still getting these errors in the wild. The only reason I implemented these times is because I implemented a retry once this specific error came in, and the server has a 15 second timeout. I am not opposed to increasing those times at all, but I am not sure this solves the root issue.
But yes, this data is pretty ephemeral, so "if you can't get me this data in the next <insert time> don't bother trying at all" is exactly what we were going for. We also have an app refresh we call that uses this same code that is called regularly throughout the day. That will also exhibit this error on occasion.
This slipped off my radar for awhile, but my answer here is actually really simple. Based on what you're describing, it sound like you should stop using the background URL session and just use the standard configuration. That is, you try to immediately download it while your app is awake in the background and if you can't download it, then you don't download it. Jumping back to what I said here:
"if you can't get me this data in the next <insert time> don't bother trying at all"
Putting some concrete numbers on that, when "<insert time>" is:
-
"a day or so"-> the background session is a great solution.
-
"12 hours"-> the background session might be worth "trying". That is, sometimes it will work, sometimes it won't, but something is better than nothing.
-
"60 minutes"-> the background session is largely irrelevant. There are times it might work but the results aren't going to be consistent enough to be useful
-
"a few minutes"-> the background session is probably a bad idea. To the extent it "works", that's almost certainly an artifact of your testing environment or some other distortion.
There are odd edge cases and exceptions to this, but the over arching point here is that as download size and resource timeout decrease, the background session makes less and less sense. If you need a 5k file in the next 30 min, then you should just download it directly instead of trying to use the background session.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware