Skip to content

Commit

Permalink
http: enable keepalive on TCP sockets
Browse files Browse the repository at this point in the history
This is a follow up to commit e47a858 (enable SO_KEEPALIVE for
connected TCP sockets, 2011-12-06).

Sockets may never receive notification of some link errors,
causing "git fetch" or similar processes to hang forever.
Enabling keepalive messages allows hung processes to error out
after a few minutes/hours depending on the keepalive settings of
the system.

I noticed this problem with some non-interactive cronjobs getting
hung when talking to HTTP servers.

Signed-off-by: Eric Wong <normalperson@yhbt.net>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
  • Loading branch information
Eric Wong authored and Jonathan Nieder committed Oct 14, 2013
1 parent e47a858 commit a15d069
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ static int has_cert_password(void)
return 0;
}

/* curl 7.25.0 has CURLOPT_TCP_KEEPALIVE, too, but we support older curl */
static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
{
int ka = 1;
int rc;
socklen_t len = (socklen_t)sizeof(ka);

if (type != CURLSOCKTYPE_IPCXN)
return 0;

rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
if (rc < 0)
warning("unable to set SO_KEEPALIVE on socket %s",
strerror(errno));

return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
}

static CURL *get_curl_handle(void)
{
CURL *result = curl_easy_init();
Expand Down Expand Up @@ -298,6 +316,10 @@ static CURL *get_curl_handle(void)
if (curl_http_proxy)
curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);

#if LIBCURL_VERSION_NUM >= 0x071000
curl_easy_setopt(result, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
#endif

return result;
}

Expand Down

0 comments on commit a15d069

Please sign in to comment.