Alamofire.request 方法中的明显死锁

Posted

技术标签:

【中文标题】Alamofire.request 方法中的明显死锁【英文标题】:Apparent deadlock in Alamofire.request method 【发布时间】:2015-11-18 19:24:29 【问题描述】:

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

线程卡在下面的代码中:

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 ()

【问题讨论】:

让我们看看你的完成处理程序 它甚至没有完成执行。如果我只在 print("dfsfdsf") 中传递 (error, result) 也会发生同样的情况 @SauloT 这个怎么样?我现在有完全相同的问题。你解决了吗? 嗨@SauloT,你找到什么了吗?我完全迷路了...... :-( 【参考方案1】:

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

【讨论】:

以上是关于Alamofire.request 方法中的明显死锁的主要内容,如果未能解决你的问题,请参考以下文章

通话中的 Alamofire 额外参数“方法”

Alamofire发布请求错误调用中的额外参数“方法”

var 请求:Alamofire.Request?使用未声明类型的 Alamofire

调用 Alamofire swift 4 中的额外参数“方法”

alamofire post 方法中的 response.result.value 为 nil

AlamoFire request() 仅在 viewDidLoad() 中工作