Skip to contentSkip to conversation

core.clientTimeout config for git:// fetches

RFC/PATCH Eric Wong 1 participant 1 message Jun 15, 2016

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

This allows "git fetch" to exit after a preconfigured socket timeout. This is needed in case an undetected server or network failure occurs.

The default timeout is 60 seconds.

http:// and rsync:// transports should be supported in future changes, too.

So far, I've only tested (by hand) with a dummy server setup using "nc -l $some_port".
---

I run "git fetch" from cron to keep mirrors up-to-date. I've noticed some cron processes running for hours/days at a time after network failures. When I straced thoses processes, I saw they were stuck on read() syscalls to remote machines.

I'm not familiar with this code and hope there's a better way to do this. Changing wrapper.c like this in this way doesn't feel right, but neither does sprinkling alarm() calls in various places.

Using a custom GIT_PROXY_COMMAND which implements timeouts on its own would also work, but I'd like to eventually have a consistent parameter unified across all remote transports for consistency and ease-of-use.

The default 60s timeout could be too low on some slow servers with big repos, maybe the default should remain zero (no timeout).

 connect.c |   27 +++++++++++++++++++++++++++
 wrapper.c |   47 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 72 insertions(+), 2 deletions(-)
connect.c +27 -0
index ee1d4b4..c1fa1ab 100644
@@ -320,10 +320,37 @@ static int git_tcp_connect_sock(char *host, int flags)
 
 #endif /* NO_IPV6 */
 
+static int timeout_setup(const char *var, const char *value, void *cb)
+{
+	int *timeout = cb;
+
+	if (strcmp(var, "core.clienttimeout") == 0)
+		*timeout = git_config_int(var, value);
+
+	return 0;
+}
 
 static void git_tcp_connect(int fd[2], char *host, int flags)
 {
 	int sockfd = git_tcp_connect_sock(host, flags);
+	struct timeval tv;
+	int timeout = 60;
+
+	git_config(timeout_setup, &timeout);
+
+	if (timeout > 0) {
+		socklen_t len = sizeof(struct timeval);
+
+		tv.tv_sec = timeout;
+		tv.tv_usec = 0;
+		if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, len) < 0)
+			die_errno("could not set socket receive timeout");
+
+		tv.tv_sec = timeout;
+		tv.tv_usec = 0;
+		if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, len) < 0)
+			die_errno("could not set socket send timeout");
+	}
 
 	fd[0] = sockfd;
 	fd[1] = dup(sockfd);
wrapper.c +45 -2

65 lines not on this page · raw message

Signature · 1 line
Eric Wong