Alamofire: Software caused connection abort Code 53

Created on 3 Jan 2019  路  11Comments  路  Source: Alamofire/Alamofire

I tried to test Alamofire Beta SDK

        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            let dataRequest = AF.request(self.urlstring)
            dataRequest.response { (response) in


                DispatchQueue.main.async {
                    var backToString = String(data: response.data!,
                                              encoding: String.Encoding.utf8) as String!
                    print(backToString)
                }

            }
        }

Ideally it should work normal when user switch application state from foreground to background and comes back after some time

But it shows error

2019-01-03 17:03:28.140261+0530 TestAlamofire[27857:8982853] Task <7287F46A-EBAE-46E6-93BF-E052DCD8FBD8>.<3> HTTP load failed (error code: 53 [1:53])
2019-01-03 17:03:28.140705+0530 TestAlamofire[27857:8982997] Task <7287F46A-EBAE-46E6-93BF-E052DCD8FBD8>.<3> finished with error - code: 53
2019-01-03 17:03:28.141127+0530 TestAlamofire[27857:8982997] Task <7287F46A-EBAE-46E6-93BF-E052DCD8FBD8>.<3> load failed with error Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort" UserInfo={_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <7287F46A-EBAE-46E6-93BF-E052DCD8FBD8>.<3>, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <7287F46A-EBAE-46E6-93BF-E052DCD8FBD8>.<3>"
), _kCFStreamErrorCodeKey=53} [53]

Alamofire Environment

Alamofire version: Beta 5.0
Xcode version: 10.1
Swift version: 4.2
Platform(s) running Alamofire: iOS 12
macOS version running Xcode: 10.13.6

TestAlamofire 2.zip

TestAlamofire 2.zip

NOTE: This is also happening in stable releases but works fine in iOS < 12.
Please let us know the possible workaround for this issue.

support

Most helpful comment

For anyone interested, we built a retrier for requests that works around this issue by retrying couple of times. This is written for Alamofire 4, but should be straightforward to rewrite for Alamofire 5.

struct Code53RequestRetrier: RequestRetrier {
    func should(_ manager: Alamofire.SessionManager, retry request: Alamofire.Request, with error: Error, completion: @escaping RequestRetryCompletion) {
        let nsError = error as NSError
        guard request.retryCount < 3 else { completion(false, 0); return }
        if nsError.domain == "NSPOSIXErrorDomain" && nsError.code == 53 {
            print("馃毀retrying, Software caused connection abort")
            completion(true, 0.5)
            return
        }
        if nsError.domain == "NSURLErrorDomain" && nsError.code == -1005 {
            print("馃毀retrying, network connection was lost")
            completion(true, 0.5)
            return
        }
        completion(false, 0)
    }
}

Sent with GitHawk

All 11 comments

iOS 12 seems to have changed some of the logic around how much time URLSessionTasks get to run in the background without taking a background assertion. Previously, it seems like the system let it run for a few seconds after the transition to the background. In iOS 12, it seems like it cancels the tasks immediately upon transition. If you'd like this to work, you should look into using background tasks. Luckily, with the EventMonitor protocol in Alamofire 5, support backgrounds tasks becomes pretty trivial.

@jshier : Atleast can you give example EventMonitor so that i can play around with that. as docs are not present for beta

Inline documentation exists, so you can check the generated docs.

@jshier : I'm getting nothing from their can you please give me a demo code. All seems to be URLSession delegates which alamofire use internally.

Is That fixed in Alamofire 5.0.0 ?

There's no bug here. If you need to handle background tasks, you can implement them using the linked documentation. As for EventMonitor documentation, its inline documentation is fully up to date and available in our API documentation. Usage guides for advanced features like this are being worked on but not ready yet as we work towards the final 5.0.0 release.

For anyone interested, we built a retrier for requests that works around this issue by retrying couple of times. This is written for Alamofire 4, but should be straightforward to rewrite for Alamofire 5.

struct Code53RequestRetrier: RequestRetrier {
    func should(_ manager: Alamofire.SessionManager, retry request: Alamofire.Request, with error: Error, completion: @escaping RequestRetryCompletion) {
        let nsError = error as NSError
        guard request.retryCount < 3 else { completion(false, 0); return }
        if nsError.domain == "NSPOSIXErrorDomain" && nsError.code == 53 {
            print("馃毀retrying, Software caused connection abort")
            completion(true, 0.5)
            return
        }
        if nsError.domain == "NSURLErrorDomain" && nsError.code == -1005 {
            print("馃毀retrying, network connection was lost")
            completion(true, 0.5)
            return
        }
        completion(false, 0)
    }
}

Sent with GitHawk

That's a good point @DenTelezhkin. And Alamofire 5 now has an error retrier built in: RetryPolicy. It doesn't support error 53 right now, just URLErrors, but you could use it as a base for a retrier that does support. In fact, we should allow it to be more flexible in the future so it can be used for things like this.

Just FYI: This issue is caused sometimes (not always) when performing request on UIApplicationWillEnterForegroundNotification.

@DenTelezhkin How do you include that retrier in your Alamofire code?

@yusuftor Simply set retrier on Alamofire.SessionManager instance:

manager.retrier = Code53RequestRetrier()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hengchengfei picture hengchengfei  路  3Comments

shivang2902 picture shivang2902  路  3Comments

borek2 picture borek2  路  3Comments

tib picture tib  路  3Comments

matthijsotterloo picture matthijsotterloo  路  3Comments