App打開微信授權登錄,返回同時請求一個接口,接口偶爾會報錯 ”網絡連接中斷,Code=-1005“問題

問題:

App中點擊微信登錄,拉起微信授權後返回,立即調用後臺的授權接口,接口偶爾會報錯"網絡連接已中斷。",報錯信息打印如下:

Error Domain=NSURLErrorDomain Code=-1005 "網絡連接已中斷。" UserInfo={_kCFStreamErrorCodeKey=-4, NSUnderlyingError=0x2825a6610 
{Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x2808b4190 [0x1fd465e20]>
{length = 16, capacity = 16, bytes = 0x100222b80ad264120000000000000000}, _kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <94A040D7-450A-4CD7-B2BF-85FDF0764ABF>.<2>,
 _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <94A040D7-450A-4CD7-B2BF-85FDF0764ABF>.<2>"
), NSLocalizedDescription=網絡連接已中斷。
, 
NSErrorFailingURLStringKey=http://10.210.100.18:8888/tspUser/v1/third/authLogin, NSErrorFailingURLKey=http://10.210.100.18:8888/tspUser/v1/third/authLogin, _kCFStreamErrorDomainKey=4}

原因:

首先排除網絡的原因,設備網絡連接狀態是正常的,問題比較無頭緒,搜索相關問題,有幾個點需要關注:
IOS開發:記錄處理 Error Domain=NSURLErrorDomain Code=-1005 "網絡連接已中斷。” Http請求報錯

Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.”

提到兩個方面的問題:

  • 服務器的KeepAliveTimeout的配置過短? 其他接口請求比較正常,不太可能是這裏的問題;
  • 應用打開了微信,我們的應用退到過後臺,會不會applicationWillEnterForeground 代理方法中有什麼異常處理? 從微信返回應用後,處理了網絡請求,那查詢下AFN請求是否出現過問題?

查詢發現,Apple 官方文檔有對於 NSURLSession 的一個問題說明:
Handling “The network connection was lost” Errors

A: NSURLErrorNetworkConnectionLost is error -1005 in the NSURLErrorDomain error domain, and is displayed to users as “The network connection was lost”. This error means that the underlying TCP connection that’s carrying the HTTP request disconnected while the HTTP request was in progress (see below for more information about this). In some circumstances NSURLSession may retry such requests automatically (specifically, if the request is idempotent) but in other circumstances that’s not allowed by the HTTP standards.

綜合查詢的信息,懷疑是應用從後臺回到前臺的時候,接口請求出了問題, 然後進一步去到AFN官方,查詢是否出現過類似問題:

[iOS 12 Beta 11] NSPOSIXErrorDomain Code=53: Software caused connection abort#4279

I didn't use AFNetworking, but I reproduce same error code.
That Error occurred when attempting network communication at the timing of UIApplication.willEnterForegroundNotification.
I changed the timing to UIApplication.didBecomeActiveNotification, the problem no longer occurs.
I do not know it will be helpful, but I hope it will fix :)

部分開發者猜測是iOS系統在真機 回到前臺的時候如果立刻使用NSURLSession可能會有連接問題

回覆中有人提到,蘋果社區有open這個問題的bug, 他們內部也還在查找並盡力修復:

investigating on the problem

Thanks to all guys :)
I've received yesterday communications Apple side: they said that they are still investigating on the problem... the bug is apple side and they don't know when the issue will be solved.
By the way, I don't think that I can use any of the workarounds described here in my project.
I'm waiting for a solution apple side then ;)
I will update you as soon as possible when I'll have communications apple side :)


解決:
  1. 目前認爲,Apple端對於 應用進入前臺立即發起的網絡請求,NSURLSession 可能複用了之前的連接, 但是失效了. 客戶端通過延時請求,問題暫時解決:
        // 避免從微信回到app接口調用 報錯 -1005連接斷開
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getUserAccountBindResultWithResponse:response];
        });
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章