Skip to content

Commit a43b68a

Browse files
Eric Wonggitster
authored andcommitted
daemon: enable SO_KEEPALIVE for all sockets
While --init-timeout and --timeout options exist and I've never run git-daemon without them, some users may forget to set them and encounter hung daemon processes when connections fail. Enable socket-level timeouts so the kernel can send keepalive probes as necessary to detect failed connections. Signed-off-by: Eric Wong <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90f7b16 commit a43b68a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

daemon.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,15 @@ static void hostinfo_clear(struct hostinfo *hi)
669669
strbuf_release(&hi->tcp_port);
670670
}
671671

672+
static void set_keep_alive(int sockfd)
673+
{
674+
int ka = 1;
675+
676+
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
677+
logerror("unable to set SO_KEEPALIVE on socket: %s",
678+
strerror(errno));
679+
}
680+
672681
static int execute(void)
673682
{
674683
char *line = packet_buffer;
@@ -681,6 +690,7 @@ static int execute(void)
681690
if (addr)
682691
loginfo("Connection from %s:%s", addr, port);
683692

693+
set_keep_alive(0);
684694
alarm(init_timeout ? init_timeout : timeout);
685695
pktlen = packet_read(0, NULL, NULL, packet_buffer, sizeof(packet_buffer), 0);
686696
alarm(0);
@@ -951,6 +961,8 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
951961
continue;
952962
}
953963

964+
set_keep_alive(sockfd);
965+
954966
if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
955967
logerror("Could not bind to %s: %s",
956968
ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
@@ -1010,6 +1022,8 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
10101022
return 0;
10111023
}
10121024

1025+
set_keep_alive(sockfd);
1026+
10131027
if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
10141028
logerror("Could not bind to %s: %s",
10151029
ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),

0 commit comments

Comments
 (0)