Re: curl_easy_recv and disconnections
Date: Fri, 18 Jan 2013 13:42:57 -0700
On Wed, Jan 16, 2013 at 7:46 AM, Daniel Stenberg <daniel_at_haxx.se> wrote:
> On Sun, 13 Jan 2013, Craig Davison wrote:
>> When using the socket API, recv() will return a length of 0 when the peer
>> has disconnected. In my own testing, it looks like curl_easy_recv() will
>> return CURLE_UNSUPPORTED_PROTOCOL (1) in this case, but that does not seem
>> to be documented anywhere. Is that the correct error code to look for?
> That seems completely inappropriate and if that is indeed what happens I
> think we should work on fixing it!
I had a look at the libcurl code. curl_easy_recv() calls easy_connection().
easy_connection() calls Curl_getconnectinfo(). If Curl_getconnectinfo()
sees that the server has shut down the connection, it returns
CURL_SOCKET_BAD, and easy_connection() returns CURLE_UNSUPPORTED_PROTOCOL
with the error message "Failed to get recent socket". curl_easy_recv() in
turn returns the error code from easy_connection().
> Also, rarely, curl_easy_recv() will return CURLE_OK, but set n=0. What
>> does this mean? Does it also mean the peer has disconnected?
> Well, we need a way to differentiate EWOULDLBOCK from a closed connection,
> but other than that I would say that 0 would imply disconnect but I haven't
> actually studied the code right now, nor done any particular experiments...
CURLE_OK/n=0 seems to happen when my connection does not use SSL or a
proxy, and the function curl_easy_recv() uses to read is Curl_recv_plain.
In that case, the n=0 comes directly from recv().
curl_easy_recv will return CURLE_AGAIN if reading would block. This is
documented and working properly.
So it seems that both CURLE_UNSUPPORTED_PROTOCOL and CURLE_OK/n=0 indicate
the connection has been shut down. I think this needs to be mentioned in
the documentation. But (IMO) I think ideally curl_easy_recv should never
return 0, and in any case return a new error code like
CURLE_PEER_DISCONNECTED if the connection is closed. The same would go for
List admin: http://cool.haxx.se/list/listinfo/curl-library
Received on 2013-01-18