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];
        });
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章