Got a serious NSURLSession background session bug

When a batch of download tasks create and resume, some of them are still downloading. Xcode kill the app, and launch app again, then all the tasks failed immediately(if not reproduce, retry these steps). The worst thing is whenever i add new tasks and resume, all of them failed immediately, until i restart the app.


I can produce this bug everytime, just follow these tips:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"***"];

NSOperationQueue* operationQueue = [[NSOperationQueue alloc] init];

operationQueue.maxConcurrentOperationCount = 1;

urlsession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:operationQueue];

for (int i = 0; i < 100; i++){

NSURLSessionDownloadTask* downloadTask = [urlsession downloadTaskWithRequest:urlRequest];

[ downloadTask resume];

}

Xcode kill the app...

Xcode launch app again...

The only delegate method be called immediately is:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSErrorFailingURLKey=xx'xx'x'xxxx, NSErrorFailingURLStringKey=***}


It's not certain appearing on devices not kill by xcode, but my QA colleagues sometimes got this bug and report to me.

Only background session has this bug, defaultSessionConfiguration won't happen.


I don't know why "No such file or directory" error is called back, any suggestions?

Replies

This sounds like a reoccurrence of an old problem where NSURLSession was intolerant of the app’s container path changing, something that happens every time the app gets reinstalled by Xcode. You should definitely file a bug about this, then post your bug number, just for the record.

You can learn about the history of this problem, and get lots of other general advice, in my Testing Background Session Code pinned post.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I'm seeing the same issue with my app. When I restart downloading of a content that previously failed, it sometimes fails with Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSErrorFailingURLKey=***, NSErrorFailingURLStringKey=***}. It will sometimes recover after trying several times. Sometimes I had to reinstall the app to recover from this issue. I see an other error in console that could be related to this issue.


Error removing item at url:file:///private/var/mobile/Containers/Data/Application/E58771BB-D7DE-5B6E-547FE8B44E25/Library/Caches/com.apple.nsurlsessiond/Downloads/XXXX/,error:Error Domain=NSCocoaErrorDomain code=4 couldn't be removed."UserInfo={NSFilePath=/private/var/mobile/Containers/Data/Application/E58771BB-D7DE-5B6E-547FE8B44E2/Library/Caches/com.apple.nsurlsessiond/Downloads/***,NSUserStringVariant=(Remove), NSUnderlyingError=0X153e32f90{Error Domain=NSPOSIXErrorDomain code=2 "No such file or directory"}}


I do not know why these errors occur and how to workaround this.

Are you seeing this during development? Or when your app is deployed to users?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I observed that this happens both during developement as well as after the app is deployed. I'm tracking this NSPOSIXErrorDomain Error using analytics.

I observed that this happens both during developement as well as after the app is deployed.

Weird things can crop up during development, as I’ve discussed in my Testing Background Session Code pinned post. I wouldn’t worry too much about that side of things (although feel free to file a bug if you can find a way to reproduce it).

If the problem shows up in production that’s another matter entirely. You wrote:

I'm tracking this

NSPOSIXErrorDomain
Error using analytics.

I am tracking an issue (r. 32534132) where, under very hard to reproduce circumstances, NSURLSession will call your

-URLSession:downloadTask:didFinishDownloadingToURL:
delegate method with a file URL that’s completely bogus, and this will result in an error like the one you’re seeing here. In that case, however, the actual URL is very different from the one you posted. Instead of being inside your app’s container:
/var/mobile/
    Containers/Data/Application/
        <ContainerUUID>/
            Library/Caches/com.apple.nsurlsessiond/Downloads/
                <YourBundleID>/
                    CFNetworkDownload_<Random>.tmp

it’s relative to the root of the file system.

/var/mobile/
    Library/Caches/com.apple.nsurlsessiond/Downloads/
        <YourBundleID>/
            CFNetworkDownload_<Random>.tmp

If your analytics see problematic paths like that, I’d be very interested in hearing about it.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I have a same problem. It is happen when I download with "resumeData". I guess that is the tmp file deleted by system in temp directory.


I am tracking an issue (r. 32534132) where, under very hard to reproduce circumstances, NSURLSession will call your

Code Block
-URLSession:downloadTask:didFinishDownloadingToURL:

delegate method with a file URL that’s completely bogus, and this will result in an error like the one you’re seeing here. In that case, however, the actual URL is very different from the one you posted. Instead of being inside your app’s container:
Code Block
/var/mobile/
Containers/Data/Application/
<ContainerUUID>/
Library/Caches/com.apple.nsurlsessiond/Downloads/
<YourBundleID>/
CFNetworkDownload_<Random>.tmp

it’s relative to the root of the file system.

Code Block
/var/mobile/
Library/Caches/com.apple.nsurlsessiond/Downloads/
<YourBundleID>/
CFNetworkDownload_<Random>.tmp


I am encountering this problem? Any updates?


Any updates?

As far as I’m aware that bug (r. 32534132) has not been adequately resolved.

I am encountering this problem?

How reproducible is it for you?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Based from response, the file is temporarily downloaded in
file:///var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/xxxxx/CFNetworkDownload_qb9hwG.tmp

I cannot copy this file to my download location since I am encountering an error:
(NSError) error = 0x00000002818928e0 Error Domain=NSCocoaErrorDomain Code=257

"The file" CFNetworkDownload_qb9hwG.tmp "could not be opened because you do not have permission to view it."
UserInfo = {NSFilePath = /var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/xxxxx/CFNetworkDownload_qb9hwG.tmp, NSUnderlyingError = 0x2818927c0
{Error Domain = NSPOSIXErrorDomain Code = 1 "Operation not permitted"}}

I am assuming the temporary file should be saved in my App Folder.

Additional info:
My app is registered with the Microsoft Identity platform

I am assuming the temporary file should be saved in my App Folder.

Yes, that’s what normally happens and the fact that it doesn’t happen in some cases is the basis of this bug.

Again, how reproducible is it for you?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Again, how reproducible is it for you?

Always, never succeeded in downloading files in background session.

Always, never succeeded in downloading files in background session.

Oh, that’s very interesting. All the other folks who’ve seen this problem can only reproduce it intermittently. Most commonly, they see rare reports of it coming in from the field and extremely rarely, if ever, reproduce it in their office.

Given that I’m going to ask you to file your own bug report about this. Make sure you include a sysdiagnose taken shortly after seeing the problem. Then post the bug number here.

Also, if you can distill the code that reproduces this down to a small test project that you’re willing to share, please attach it to the bug report. But if not that’s OK, getting a reproducible bug report on file is a great first step.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hello, My App uses NSURLSession to send network requests. Recently, on the iOS 17.0 system version, I've noticed an increase in ENOENT errors. However, I'm unable to identify the cause from the logs. I would like to inquire if there have been any changes to NSURLSession in this version? Why would this error code occur?

Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSErrorFailingURLStringKey=https://example.com, NSErrorFailingURLKey=https://example.com, _NSURLErrorRelatedURLSessionTaskErrorKey=(

    "LocalDownloadTask <AC10B665-F59A-469C-876C-F88EEAEC26E1>.<11>"

), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDownloadTask <AC10B665-F59A-469C-876C-F88EEAEC26E1>.<11>}, response:<NSHTTPURLResponse: 0x280b8d000> { URL: https://example.com } { Status Code: 200
// session
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
operationQueue.maxConcurrentOperationCount = 1;
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:operationQueue];

// task
NSURLSessionTask *task = [self.session downloadTaskWithRequest:operation.request];
[task resume];

You’re not using a background session, so your issue is probably unrelated to this one. Let’s continue discussing this on your specific thread.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"