Skip to content

Commit

Permalink
enable SO_KEEPALIVE for connected TCP sockets
Browse files Browse the repository at this point in the history
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.

This is a problem noticed when running non-interactive
cronjobs to mirror repositories using "git fetch".

Signed-off-by: Eric Wong <normalperson@yhbt.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Eric Wong authored and Junio C Hamano committed Dec 6, 2011
1 parent c2857fb commit e47a858
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ static void get_host_and_port(char **host, const char **port)
}
}

static void enable_keepalive(int sockfd)
{
int ka = 1;

if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
strerror(errno));
}

#ifndef NO_IPV6

static const char *ai_name(const struct addrinfo *ai)
Expand Down Expand Up @@ -239,6 +248,8 @@ static int git_tcp_connect_sock(char *host, int flags)
if (sockfd < 0)
die("unable to connect to %s:\n%s", host, error_message.buf);

enable_keepalive(sockfd);

if (flags & CONNECT_VERBOSE)
fprintf(stderr, "done.\n");

Expand Down Expand Up @@ -315,6 +326,8 @@ static int git_tcp_connect_sock(char *host, int flags)
if (sockfd < 0)
die("unable to connect a socket (%s)", strerror(saved_errno));

enable_keepalive(sockfd);

if (flags & CONNECT_VERBOSE)
fprintf(stderr, "done.\n");

Expand Down

0 comments on commit e47a858

Please sign in to comment.