Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Start date cannot be later in time than end date!'

I am using Alamofire and after several hours of my app running on the simulator I got a crash with this error.

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Start date cannot be later in time than end date!'

I got this stack trace in console:

*** First throw call stack:
(
    0   CoreFoundation                      0x0000000111186d4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x0000000110be821e objc_exception_throw + 48
    2   Foundation                          0x00000001107f0e3c -[_NSConcreteDateInterval dealloc] + 0
    3   CFNetwork                           0x00000001131a18e8 -[__NSCFURLSessionTaskMetrics _initWithTask:] + 868
    4   CFNetwork                           0x00000001131a1497 -[NSURLSessionTaskMetrics _initWithTask:] + 100
    5   CFNetwork                           0x0000000112f77bc7 -[__NSCFURLLocalSessionConnection _tick_finishing] + 351
    6   libdispatch.dylib                   0x00000001128e3978 _dispatch_call_block_and_release + 12
    7   libdispatch.dylib                   0x000000011290d0cd _dispatch_client_callout + 8
    8   libdispatch.dylib                   0x00000001128eae17 _dispatch_queue_serial_drain + 236
    9   libdispatch.dylib                   0x00000001128ebb4b _dispatch_queue_invoke + 1073
    10  libdispatch.dylib                   0x00000001128ee385 _dispatch_root_queue_drain + 720
    11  libdispatch.dylib                   0x00000001128ee059 _dispatch_worker_thread3 + 123
    12  libsystem_pthread.dylib             0x0000000112cbc736 _pthread_wqthread + 1299
    13  libsystem_pthread.dylib             0x0000000112cbc211 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Did someone get the similar crash?

Thanks

like image 622
thierryb Avatar asked Nov 21 '16 15:11

thierryb


3 Answers

I had the same crash and did some research today and found this:

http://www.openradar.me/28301343

It looks Apple fixed the issue in iOS 10.2. just thought it may help you!

like image 99
Farshad Sheykhi Avatar answered Sep 18 '22 10:09

Farshad Sheykhi


Yes I just got the same exact crash. It happened in a background thread and it seems to have to do with making a URL session network request. I wonder if it's some sort of multithreading bug having to do with the fact that I'm making two network requests at the same time. I'm using Alamofire as well but not sure if the bug lies in Alamofire or in Apple's code. I've been unable to reproduce it as of now. Maybe you can figure out how to reproduce it and then file an issue in Apple's bug radar or in the Alamofire GitHub repo.

like image 27
Mike Onorato Avatar answered Sep 20 '22 10:09

Mike Onorato


This is a bug in Apple's NSURLSessionTaskMetrics code and happens during a network request when the user's clock gets moved far enough backwards that the request start timestamp is after the request end timestamp. This is reproducible using a network debugging proxy and manually adjusting the clock, and only occurs from iOS 10.0 up to but not including iOS 10.2

If you're using Alamofire, and you don't need NSURLSessionTaskMetrics, you can work around this by using a custom SessionDelegate for your SessionManager and overriding the responds(to aSelector..) function e.g:

class MySessionDelegate: Alamofire.SessionDelegate {
    override public func responds(to aSelector: Selector) -> Bool {
        let result: Bool = super.responds(to: aSelector)
        if #available(iOS 10.2, *) {
            // NSURLSessionTaskMetrics date crash is fixed
            return result
        } else if #available(iOS 10.0, *) {
            // NSURLSessionTaskMetrics date crash is not fixed, turn off metric collection
            if aSelector ==  #selector(self.urlSession(_:task:didFinishCollecting:)) {
                return false
            } else {
                return result
            }
        } else {
            // NSURLSessionTaskMetrics doesn't exist
            return result
        }
    }
}

If you're using the default SessionManager (e.g. calling Alamofire.request(...)) you can create your own SessionManager instead in order to use your custom SessionDelegate:

let sessionManager: Alamofire.SessionManager = {
    let configuration: URLSessionConfiguration = URLSessionConfiguration.default
    configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
    return Alamofire.SessionManager(configuration: configuration, delegate: MySessionDelegate(), serverTrustPolicyManager: nil)
}()

And now instead of calling Alamofire.request(...) you'd call sessionManager.request(...)

like image 31
stu Avatar answered Sep 22 '22 10:09

stu