Running NSURLSessionTask receives NSPOSIXErrorDomain Code=53 "Software caused connection abort" when app is relaunched from background

We have an app which issues a "poll request" to obtain data from a backend service. In certain scenarios, an app switch is performed, temporarily sending the user to another app where an action is performed, after which the user is sent back to our app.


In iOS 11 the behaviour when our app is brought back from the background is, that we receive a NSURLErrorDomain Code=-1005 "Network connection lost" response, and therefore we re-issue our "poll request".


In iOS 12 however, when the same scenario occurs, we receive the error.code -1005 only the first time. On subsequent returns from the app we switched to, iOS instead report NSPOSIXErrorDomain Code=53 "Software caused connection abort".


The question is, if this is a bug in iOS 12, or an intended behaviour which we should handle somehow. In the latter case, is it possibly because we should configure our NSURLSessionTask differently, or should we simply accept the fact that we might have an error.code = 53 returned, and handle that like we would eg. an error.code = -1005 ?


This is the code used for configuring/starting our NSURLSessionTask (the urlSession used basically is an [NSURLSessionConfiguration ephemeralSessionConfiguration] just with a couple of HTTPAdditionalHeaders set):


+ (void)startRequest:(NSURLRequest *)request completion:(RequestCompletionHandler)completioHandler {
    
    __weak __typeof(self)weakSelf = self;
    NSURLSessionTask *sessionTask = [[JBRequestSession urlSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        if (error) {
            if (error.code == NSURLErrorCancelled) {
                // Task was cancelled - Do not do anything
                return;
            }
            else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (completioHandler) {
                        completioHandler(nil, error);
                    }
                });
                return;
            }
        }
        
        NSHTTPURLResponse *httpURLResponse = (NSHTTPURLResponse*)response;
        [weakSelf saveCookiesForResponse:httpURLResponse];
        
        NSDictionary *json = nil;
        if ([data length] > 0) {
            NSError *jsonError;
            json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
            if (jsonError) {
                if (!error) {
                    error = jsonError;
                }
            }
        }


        NSString *jsonPrint = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        if ([weakSelf didGetServerErrorInResponse:httpURLResponse withOriginalBodyContent:jsonPrint andJson:json error:&error]) {
            // Server response error code
            if ([JBRequestSessionErrorHandler requestFailedWithSessionTimoutForResponse:response error:error]){
                // Session time out - cancel all tasks
                [weakSelf cancelAllTasks];
            }
        } 
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completioHandler) {
                completioHandler(json, error);
            }
        });
    }];
    
    [sessionTask resume];  // Start request
}

Replies

I've run into the same issue on iOS12. We're using an older version of AFNetworking, so I suspect it has to do with the way that background connections are being terminated. Have you found a workaround or cause?

Following up, we determined that the issue was caused because our app continued to issue new NSURLConnection requests after going into the background and wasn't explicitly making them background tasks. As we didn't need background syncing, putting in code to prevent new requests from going out once the app was in the background eliminated this error.

Unfortunately, we haven't been able to find the cause. Our workaround for now is to check for ECONNABORTED (aka. errorcode 53), and if this happens issue a new request.

I was getting the same error you got when making HTTP requests when the app was in the background. This https://stackoverflow.com/questions/11864553/ios-http-request-while-in-background solved the issue for me.

Has this bug been fixed by Apple now?
Looking at the thread here, it looks like that is the case but it is still happening for me on iOS 12.2.

It's unclear if this is related but I had similar errors. They were caused by my failure to call invalidateAndCancel on an NSURLSession when the app went into background.