cpp-httplib icon indicating copy to clipboard operation
cpp-httplib copied to clipboard

Client Get operation stalls in getaddrinfo when network is down

Open TheMostDiligent opened this issue 2 years ago • 6 comments

Hello!

We are seeing the following issue when the network is completely unavailable: the Get operations stalls in the getaddrinfo for about 30 seconds. We set the connection timeout and read timeout, but they don't have any effect.

Is it possible that this logic also respects the connection timeout?

TheMostDiligent avatar Jun 27 '23 17:06 TheMostDiligent

@TheMostDiligent thanks for the feedback. As far as I know, getaddrinfo is a blocking function and doesn't provide timeout option. So there is no way for me to deal with it... Do you know how other HTTP libraries handle this situation?

yhirose avatar Jun 27 '23 19:06 yhirose

I am not very profound in HTTP handling. However, I tried to research the subject a bit and apparently there is an alternative function: getaddrinfo_a that supports asynchronous wait mode (GAI_NOWAIT). In this mode, the function exits immediately and an event is triggered when the operation completes. I think this will allow to wait for the event in the loop and abort the loop if timeout elapses. Or even better, if the user-provided callback returns false similar to the callback of the Get function.

TheMostDiligent avatar Jun 27 '23 19:06 TheMostDiligent

It looks like only Linux supports it... How could it be possible to do the same in Windows and MacOS?

yhirose avatar Jun 27 '23 20:06 yhirose

It looks like only Linux supports it

It does seem so. On Mac, there is DNSServiceGetAddrInfo that allows async wait. This article describes it in detail. Looks like it is quite a hassle to implement this...

On Windows, I actually did not see that problem.

TheMostDiligent avatar Jun 27 '23 23:06 TheMostDiligent

It seems like we may use GetAddrInfoEx which supports timeout parameter. https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfoexw

In any case, supporting this feature on all three platforms and ensuring they work correctly will not likely be an easy task for me. So please don't expect me to implement it in the near future. A pull request is always welcome! :)

yhirose avatar Jun 28 '23 00:06 yhirose