We are in a situation where the production app is facing the following socket exception and not able to perform any other network operation after this.
DioError [DioErrorType.DEFAULT]: SocketException: Failed host lookup: ‘xyz.abc.com’ (OS Error: nodename nor servname provided, or not known, errno = 8)
Note: Encountered repetitively with one user having iPhone X, iOS 14.4
We are using Dio as a network client, with Retrofit, which internally uses the HttpClient from the dart. With Dio the exception is not reproducible with the simulated environment but using HttpClient directly, the same exception can be reproduced with the following code in iOS simulator.
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 *****');
}
}
}
Once the exception was thrown, the HttpClient was not able to recover from that stale state and all other API requests were started failing with the same error.
We were able to recover from that stale state by force closing all the previous connections and opening up a new 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');
}
}
One interesting fact is after every 236 requests the exception is raising. It could be because of file descriptors over usage but iOS has a limit of 256.
With a stable internet connection, this issue reproducible every time in iOS simulator.
Although I am not able to reproduce the issue with Dio client but as in production it is occurring. So I am seeking help to understand the root cause of this issue, also how we can prevent it?
Anyone who has come across this kind of situation and how you have overcome it, please help me.
Thanks in advance.


That's a strange error.
This might not answer your question, but may push us towards figuring out what's going on.
The code snippet (copied from question) will open up a new
streamwith each.getUrl()call and will not close them. (I'm assuming this is intentional to create the socket exception?)At some point, a limit (of open streams) is hit. I guess that magic number is 236 in your case.
So at that point, is when you're seeing the
nodename or servname providedexception?(Btw, as an aside, I think that error is coming from the underlying host operating system's DNS service, although I'm not sure if it's due to the request spam, the number of open connections, etc. This may not be relevant info.)
So, if you used the
HttpClientin a typical way, making requests & closing those open streams, such as this:... Are you still seeing the same
nodename or servname providederror pop up?With this "typical usage" code immediately above, the
userAgentcan be reused until auserAgent.close()call is made (and the HttpClient is permanently closed. Trying to use it again would throw aBad Stateexception).I'd be interested to hear if the nodename error still occurs with this modified code.
Re: the second code snippet from the question.
In the catch block, the
HttpClientis closed, then a newHttpClientis created. This effectively closes all the open streams that were opened in thetryblock (and I assume, resetting the limit of open streams.)If you adjusted the 2nd code example to use:
Could you run that indefinitely?