{"thread":{"id":"29090","subject":"[PATCH] enable SO_KEEPALIVE for connected TCP sockets","startedAt":"2011-12-06T04:39:36Z","lastAt":"2011-12-06T04:39:36Z","messageCount":1,"participants":["Eric Wong"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"180335","messageId":"20111206043936.GA31920@dcvr.yhbt.net","threadId":"29090","inReplyTo":null,"subject":"[PATCH] enable SO_KEEPALIVE for connected TCP sockets","fromName":"Eric Wong","fromEmail":"normalperson@yhbt.net","sentAt":"2011-12-06T04:39:36Z","receivedAt":"2011-12-06T04:39:36Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"Sockets may never receive notification of some link errors,\ncausing \"git fetch\" or similar processes to hang forever.\nEnabling keepalive messages allows hung processes to error out\nafter a few minutes/hours depending on the keepalive settings of\nthe system.\n\nThis is a problem noticed when running non-interactive\ncronjobs to mirror repositories using \"git fetch\".\n\nSigned-off-by: Eric Wong <normalperson@yhbt.net>\n---\n connect.c |   13 +++++++++++++\n 1 files changed, 13 insertions(+), 0 deletions(-)\n\ndiff --git a/connect.c b/connect.c\nindex 51990fa..d0f59ef 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -175,6 +175,15 @@ static void get_host_and_port(char **host, const char **port)\n \t}\n }\n \n+static void enable_keepalive(int sockfd)\n+{\n+\tint ka = 1;\n+\n+\tif (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)\n+\t\tfprintf(stderr, \"unable to set SO_KEEPALIVE on socket: %s\\n\",\n+\t\t\tstrerror(errno));\n+}\n+\n #ifndef NO_IPV6\n \n static const char *ai_name(const struct addrinfo *ai)\n@@ -239,6 +248,8 @@ static int git_tcp_connect_sock(char *host, int flags)\n \tif (sockfd < 0)\n \t\tdie(\"unable to connect to %s:\\n%s\", host, error_message.buf);\n \n+\tenable_keepalive(sockfd);\n+\n \tif (flags & CONNECT_VERBOSE)\n \t\tfprintf(stderr, \"done.\\n\");\n \n@@ -312,6 +323,8 @@ static int git_tcp_connect_sock(char *host, int flags)\n \tif (sockfd < 0)\n \t\tdie(\"unable to connect to %s:\\n%s\", host, error_message.buf);\n \n+\tenable_keepalive(sockfd);\n+\n \tif (flags & CONNECT_VERBOSE)\n \t\tfprintf(stderr, \"done.\\n\");\n \n-- \nEric Wong\n"}]}