Skip to content

Commit 0a20325

Browse files
committed
Merge branch 'ew/daemon-socket-keepalive' into maint
When "git daemon" is run without --[init-]timeout specified, a connection from a client that silently goes offline can hang around for a long time, wasting resources. The socket-level KEEPALIVE has been enabled to allow the OS to notice such failed connections. * ew/daemon-socket-keepalive: daemon: enable SO_KEEPALIVE for all sockets
2 parents 05219a1 + a43b68a commit 0a20325

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)