我们遇到了生产应用程序面临以下套接字异常的情况,此后无法执行任何其他网络操作。
DioError [DioErrorType.DEFAULT]: SocketException: Failed host lookup: ‘xyz.abc.com’ (OS Error: nodename nor servname provided, or not known, errno = 8)
注意:遇到一个使用iPhone X,iOS 14.4的用户反复出现。
我们正在使用Dio作为网络客户端,与Retrofit一起使用,它在内部使用来自dart的HttpClient。使用Dio无法在模拟环境中重现异常,但是直接使用HttpClient时,在iOS模拟器中可以使用以下代码重现相同的异常。
HttpClient userAgent = new HttpClient();
bool run = true;
while (run) {
try {
await userAgent.getUrl(Uri.parse('https://www.google.com'));
print('Number of api executed');
} catch (e) {
print(e);
if (e is SocketException) {
if ((e as SocketException).osError.errorCode == 8)
print('***** Exception Caught *****');
}
}
}
一旦抛出异常,HttpClient 就无法从那种陈旧的状态中恢复,所有其他 API 请求都会以相同的错误失败。
我们通过强制关闭所有先前的连接并打开一个新的HttpClient来恢复那个陈旧状态。
HttpClient userAgent = new HttpClient();
bool run = true;
while (run) {
try {
await userAgent.getUrl(Uri.parse('https://www.google.com'));
print('Number of api executed');
} catch (e) {
print(e);
if (e is SocketException) {
if ((e as SocketException).osError.errorCode == 8)
print('***** Exception Caught *****');
}
userAgent.close(force: true);
print('Force closing previous connections');
userAgent = HttpClient();
print('Creating new HttpClient instance');
}
}
一个有趣的事实是,每236个请求后都会引发异常。这可能是由于文件描述符过度使用,但iOS有256的限制。在稳定的互联网连接下,在iOS模拟器中每次都可以重现此问题。
虽然我无法通过Dio客户端重现此问题,但在生产中它确实发生了。因此,我正在寻求帮助来了解此问题的根本原因,以及如何预防它?
任何遇到这种情况并且知道如何克服它的人,请帮忙一下。
提前感谢您。
互联网连接
已经打开
。 - Shubham Narkhede