简体   繁体   中英

Apparent deadlock in Alamofire.request method

I'm having a strange deadlock that happens only while debugging with a real device.

The thread is stuck in the code bellow:

static func GenericPost(url: String, parameters: [String: AnyObject], completion: (error: NSError?, result: AnyObject?) -> Void){

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in



        let req = Alamofire
            .request(.POST, url, parameters: parameters); /// The execution stops here !!!!!!!!

        req.responseJSON { response in
            switch response.result {
            case .Success:
                let json = response.result.value as? [String: AnyObject];

                completion(error: nil, result: res);

            case .Failure(let error):
                completion(error: error, result: nil);
            }
        }
    })

}

All stacks traces are printed bellow and I don't see anything related to the deadlock, but the __psynch_mutexwait in the affected thread. Am I missing something?

I'm using Xcode 7.1, iPhone 6 (iOS 9.1) and Alamofire 3.1.2

Thread 1Queue : com.apple.main-thread (serial)
#0  0x0000000198610a40 in mach_msg_trap ()
#1  0x00000001986108bc in mach_msg ()
#2  0x00000001830c4108 in __CFRunLoopServiceMachPort ()
#3  0x00000001830c1e0c in __CFRunLoopRun ()
#4  0x0000000182ff0ca0 in CFRunLoopRunSpecific ()
#5  0x000000018e22c088 in GSEventRunModal ()
#6  0x0000000188708ffc in UIApplicationMain ()
#7  0x000000010010d660 in main at /Users/username/Documents/work/xbroker-portal-do-X-ios/xBrokersPortalDoX/AppDelegate.swift:20
#8  0x000000019850e8b8 in start ()

Thread 2Queue : com.apple.root.default-qos (concurrent)
#0  0x000000019862af90 in __psynch_mutexwait ()
#1  0x00000001986f639c in _pthread_mutex_lock_wait ()
#2  0x000000010126312c in swift_conformsToProtocol ()
#3  0x0000000101263ee8 in _conformsToProtocol(swift::OpaqueValue const*, swift::Metadata const*, swift::ProtocolDescriptor const*, swift::WitnessTable const**) ()
#4  0x00000001012620f8 in _dynamicCastToExistential(swift::OpaqueValue*, swift::OpaqueValue*, swift::Metadata const*, swift::ExistentialTypeMetadata const*, swift::DynamicCastFlags) ()
#5  0x000000010126182c in swift_dynamicCast ()
#6  0x0000000101164114 in _print_unlocked<A, B where ...> (A, inout B) -> () ()
#7  0x0000000101147200 in String.init<A>(stringInterpolationSegment : A) -> String ()
#8  0x00000001003ea614 in static Manager.(defaultHTTPHeaders).(variable initialization expression).(closure #1).(closure #2) at /Users/username/Documents/work/xbroker-portal-do-X-ios/Pods/Alamofire/Source/Manager.swift:72
#9  0x00000001003e989c in static Manager.(defaultHTTPHeaders).(variable initialization expression).(closure #1) at /Users/username/Documents/work/xbroker-portal-do-X-ios/Pods/Alamofire/Source/Manager.swift:81
#10 0x00000001003df284 in globalinit_33_7DD0B2C79D9EB0D097BCB0C6343B5D40_func1 at /Users/username/Documents/work/xbroker-portal-do-X-ios/Pods/Alamofire/Source/Manager.swift:46
#11 0x000000010214dc68 in _dispatch_client_callout ()
#12 0x000000010214eae0 in dispatch_once_f ()
#13 0x00000001003df184 in Manager.defaultHTTPHeaders.unsafeMutableAddressor at /Users/username/Documents/work/xbroker-portal-do-X-ios/Pods/Alamofire/Source/Manager.swift:46
#14 0x00000001003e96dc in static Manager.(sharedInstance).(variable initialization expression).(closure #1) at /Users/username/Documents/work/xbroker-portal-do-X-ios/Pods/Alamofire/Source/Manager.swift:38
#15 0x00000001003df13c in globalinit_33_7DD0B2C79D9EB0D097BCB0C6343B5D40_func0 at /Users/username/Documents/work/xbroker-portal-do-X-ios/Pods/Alamofire/Source/Manager.swift:36
#16 0x000000010214dc68 in _dispatch_client_callout ()
#17 0x000000010214eae0 in dispatch_once_f ()
#18 0x00000001003df264 in Manager.sharedInstance.unsafeMutableAddressor at /Users/username/Documents/work/xbroker-portal-do-X-ios/Pods/Alamofire/Source/Manager.swift:36
#19 0x00000001003d8014 in request(Method, URLStringConvertible, parameters : [String : AnyObject]?, encoding : ParameterEncoding, headers : [String : String]?) -> Request at /Users/username/Documents/work/xbroker-portal-do-X-ios/Pods/Alamofire/Source/Alamofire.swift:126
#20 0x0000000100105d30 in static GenericServices.(GenericPost(GenericServices.Type) -> (String, parameters : [String : AnyObject], completion : (error : NSError?, result : AnyObject?) -> ()) -> ()).(closure #1) at /Users/username/Documents/work/xbroker-portal-do-X-ios/xBrokersPortalDoX/GenericServices.swift:21
#21 0x00000001000cf7e4 in thunk ()
#22 0x000000010214dca8 in _dispatch_call_block_and_release ()
#23 0x000000010214dc68 in _dispatch_client_callout ()
#24 0x000000010215cec8 in _dispatch_root_queue_drain ()
#25 0x000000010215c590 in _dispatch_worker_thread3 ()
#26 0x00000001986f1470 in _pthread_wqthread ()
Enqueued from com.apple.main-thread (Thread 1)Queue : com.apple.main-thread (serial)
#0  0x000000010215d864 in _dispatch_async_f_slow ()
#1  0x0000000100105320 in static GenericServices.GenericPost(String, parameters : [String : AnyObject], completion : (error : NSError?, result : AnyObject?) -> ()) -> () at /Users/username/Documents/work/xbroker-portal-do-X-ios/xBrokersPortalDoX/GenericServices.swift:43
#2  0x000000010012f964 in static LoginServices.doLogin(String, Password : String, completion : (error : NSError?, result : AnyObject?) -> ()) -> () at /Users/username/Documents/work/xbroker-portal-do-X-ios/xBrokersPortalDoX/LoginServices.swift:18
#3  0x00000001000cceb8 in LoginViewController.actLogin(AnyObject) -> () at /Users/username/Documents/work/xbroker-portal-do-X-ios/xBrokersPortalDoX/LoginViewController.swift:99
#4  0x00000001000ccfc0 in @objc LoginViewController.actLogin(AnyObject) -> () ()
#5  0x00000001886d7cfc in -[UIApplication sendAction:to:from:forEvent:] ()
#6  0x00000001886d7c78 in -[UIControl sendAction:to:forEvent:] ()
#7  0x00000001886bf930 in -[UIControl _sendActionsForEvents:withEvent:] ()
#8  0x00000001886d7590 in -[UIControl touchesEnded:withEvent:] ()
#9  0x00000001886d71c0 in -[UIWindow _sendTouchesForEvent:] ()
#10 0x00000001886cfcdc in -[UIWindow sendEvent:] ()
#11 0x00000001886a04a4 in -[UIApplication sendEvent:] ()
#12 0x000000018869e76c in _UIApplicationHandleEventQueue ()
#13 0x00000001830c4544 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#14 0x00000001830c3fd8 in __CFRunLoopDoSources0 ()
#15 0x00000001830c1cd8 in __CFRunLoopRun ()
#16 0x0000000182ff0ca0 in CFRunLoopRunSpecific ()
#17 0x000000018e22c088 in GSEventRunModal ()
#18 0x0000000188708ffc in UIApplicationMain ()
#19 0x000000010010d660 in main at /Users/username/Documents/work/xbroker-portal-do-X-ios/xBrokersPortalDoX/AppDelegate.swift:20
#20 0x000000019850e8b8 in start ()

Thread 3Queue : com.apple.libdispatch-manager (serial)
#0  0x000000019862c4fc in kevent_qos ()
#1  0x0000000102162328 in _dispatch_mgr_invoke ()
#2  0x000000010214fee4 in _dispatch_mgr_thread ()

Thread 7#0  0x000000019862bb6c in __workq_kernreturn ()
#1  0x00000001986f1530 in _pthread_wqthread ()
com.apple.NSURLConnectionLoader (8)#0   0x0000000198610a40 in mach_msg_trap ()
#1  0x00000001986108bc in mach_msg ()
#2  0x00000001830c4108 in __CFRunLoopServiceMachPort ()
#3  0x00000001830c1e0c in __CFRunLoopRun ()
#4  0x0000000182ff0ca0 in CFRunLoopRunSpecific ()
#5  0x0000000182889b84 in +[NSURLConnection(Loader) _resourceLoadLoop:] ()
#6  0x000000018404fc80 in __NSThread__start__ ()
#7  0x00000001986f3b28 in _pthread_body ()
#8  0x00000001986f3a8c in _pthread_start ()
com.apple.CFSocket.private (9)#0    0x000000019862b368 in __select ()
#1  0x00000001830ca670 in __CFSocketManager ()
#2  0x00000001986f3b28 in _pthread_body ()
#3  0x00000001986f3a8c in _pthread_start ()
com.apple.CoreMotion.MotionThread (12)#0    0x0000000198610a40 in mach_msg_trap ()
#1  0x00000001986108bc in mach_msg ()
#2  0x00000001830c4108 in __CFRunLoopServiceMachPort ()
#3  0x00000001830c1e0c in __CFRunLoopRun ()
#4  0x0000000182ff0ca0 in CFRunLoopRunSpecific ()
#5  0x000000018303e44c in CFRunLoopRun ()
#6  0x0000000183a762e4 in ___lldb_unnamed_function2150$$CoreMotion ()
#7  0x00000001986f3b28 in _pthread_body ()
#8  0x00000001986f3a8c in _pthread_start ()

Thread 13#0 0x000000019862bb6c in __workq_kernreturn ()
#1  0x00000001986f1530 in _pthread_wqthread ()

I experienced a similar deadlock to this one when using Alamofire. The fix in my case was totally unrelated to Alamofire itself, it was caused by a call to objc_sync_enter on an array, this somehow caused strange deadlocks (probably because array is a struct and not an object) to happen inside NSOperationBlock which totally deadlocked the NSURLSession delegate queue used by Alamofire. changed the call to be on an object instead of an array and it solved it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM