Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit a15d069

Browse files
Eric Wongjrn
authored andcommitted
http: enable keepalive on TCP sockets
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 <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]>
1 parent e47a858 commit a15d069

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

http.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,24 @@ static int has_cert_password(void)
233233
return 0;
234234
}
235235

236+
/* curl 7.25.0 has CURLOPT_TCP_KEEPALIVE, too, but we support older curl */
237+
static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
238+
{
239+
int ka = 1;
240+
int rc;
241+
socklen_t len = (socklen_t)sizeof(ka);
242+
243+
if (type != CURLSOCKTYPE_IPCXN)
244+
return 0;
245+
246+
rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
247+
if (rc < 0)
248+
warning("unable to set SO_KEEPALIVE on socket %s",
249+
strerror(errno));
250+
251+
return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
252+
}
253+
236254
static CURL *get_curl_handle(void)
237255
{
238256
CURL *result = curl_easy_init();
@@ -298,6 +316,10 @@ static CURL *get_curl_handle(void)
298316
if (curl_http_proxy)
299317
curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
300318

319+
#if LIBCURL_VERSION_NUM >= 0x071000
320+
curl_easy_setopt(result, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
321+
#endif
322+
301323
return result;
302324
}
303325

0 commit comments

Comments
 (0)