Alamofire.request方法中的明显死锁

时间:2015-11-18 19:24:29

标签: ios swift swift2 alamofire

我有一个奇怪的死锁,只有在使用真实设备进行调试时才会发生。

线程卡在下面的代码中:

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);
            }
        }
    })

}

下面打印所有堆栈跟踪,我看不到与死锁相关的任何内容,但是受影响的线程中的__psynch_mutexwait。 我错过了什么吗?

我正在使用Xcode 7.1,iPhone 6(iOS 9.1)和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 ()

1 个答案:

答案 0 :(得分:1)

使用Alamofire时,我遇到了类似的僵局。 我的情况下的修复与Alamofire本身完全无关,它是由对数组上的objc_sync_enter的调用引起的,这在某种程度上导致奇怪的死锁(可能因为数组是结构而不是对象)在NSOperationBlock内部发生,这完全使NSURLSession陷入僵局Alamofire使用的委托队列。将调用更改为对象而不是数组,它解决了它。