Skip to contentSkip to conversation

gc: reject if another gc is running, unless --force is given

PATCH New Nguyễn Thái Ngọc Duy 6 participants 20 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gc: reject if another gc is running, unless --force is given

This may happen when git gc --auto is run automatically, then the user, to avoid wait time, switches to a new terminal, keeps working and git gc --auto is started again because the first gc instance has not clean up the repository.

---
I don't know if the kill(pid, 0) trick works on Windows..

 Documentation/git-gc.txt |  6 +++++-
 builtin/gc.c             | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
Documentation/git-gc.txt +5 -1
index 2402ed6..e158a3b 100644
@@ -9,7 +9,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
 SYNOPSIS
 --------
 [verse]
-'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
+'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force]
 
 DESCRIPTION
 -----------
@@ -72,6 +72,10 @@ automatic consolidation of packs.
 --quiet::
 	Suppress all progress reports.
 
+--force::
+	Force `git gc` to run even if there may be another `git gc`
+	instance running on this repository.
+
 Configuration
 -------------
 
builtin/gc.c +18 -0
index 6be6c8d..07c566c 100644
@@ -169,9 +169,11 @@ static int need_to_gc(void)
 
 int cmd_gc(int argc, const char **argv, const char *prefix)
 {
+	FILE *fp;
 	int aggressive = 0;
 	int auto_gc = 0;
 	int quiet = 0;
+	int force = 0;
 
 	struct option builtin_gc_options[] = {
 		OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -180,6 +182,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 			PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
 		OPT_BOOLEAN(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
 		OPT_BOOLEAN(0, "auto", &auto_gc, N_("enable auto-gc mode")),
+		OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
 		OPT_END()
 	};
 
@@ -225,6 +228,21 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 	} else
 		add_repack_all_option();
 
+	if (!force && (fp = fopen(git_path("gc.pid"), "r")) != NULL) {
+		uintmax_t pid;
+		if (fscanf(fp, "%"PRIuMAX, &pid) == 1 && !kill(pid, 0)) {
+			if (auto_gc)
+				return 0; /* be quiet on --auto */
+			die(_("gc is already running"));
+		}
+		fclose(fp);
+	}
+
+	if ((fp = fopen(git_path("gc.pid"), "w")) != NULL) {
+		fprintf(fp, "%"PRIuMAX"\n", (uintmax_t) getpid());
+		fclose(fp);
+	}
+
 	if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
 		return error(FAILED_RUN, pack_refs_cmd.argv[0]);
 
Signature · 1 line
1.8.2.83.gc99314b
#1 lore raw

PATCH v2 1/1 gc: reject if another gc is running, unless --force is given

This may happen when git gc --auto is run automatically, then the user, to avoid wait time, switches to a new terminal, keeps working and git gc --auto is started again because the first gc instance has not clean up the repository.

This patch tries to avoid multiple gc running, especially in --auto mode. In the worst case, gc may be delayed 12 hours if a daemon reuses the pid stored in gc-%s.pid.

---
v2 deals with the stored pid taken by another process and takes shared repository into account (sort of, it only makes sure gc is not run multiple times on the same machine, not only one gc instance across multiple machines)

I changed mingw.h to add a stub uname() because I don't think MinGW port has that function, but that's totally untested.

 Documentation/git-gc.txt |  6 +++++-
 builtin/gc.c             | 35 +++++++++++++++++++++++++++++++++++
 compat/mingw.h           |  6 ++++++
 git-compat-util.h        |  1 +
 4 files changed, 47 insertions(+), 1 deletion(-)
Documentation/git-gc.txt +5 -1
index 2402ed6..e158a3b 100644
@@ -9,7 +9,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
 SYNOPSIS
 --------
 [verse]
-'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
+'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force]
 
 DESCRIPTION
 -----------
@@ -72,6 +72,10 @@ automatic consolidation of packs.
 --quiet::
 	Suppress all progress reports.
 
+--force::
+	Force `git gc` to run even if there may be another `git gc`
+	instance running on this repository.
+
 Configuration
 -------------
 
builtin/gc.c +35 -0
index 6be6c8d..8a7f24a 100644
@@ -172,6 +172,10 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 	int aggressive = 0;
 	int auto_gc = 0;
 	int quiet = 0;
+	int force = 0;
+	FILE *fp;
+	struct utsname utsname;
+	struct stat st;
 
 	struct option builtin_gc_options[] = {
 		OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -180,6 +184,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 			PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
 		OPT_BOOLEAN(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
 		OPT_BOOLEAN(0, "auto", &auto_gc, N_("enable auto-gc mode")),
+		OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
 		OPT_END()
 	};
 
@@ -225,6 +230,34 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 	} else
 		add_repack_all_option();
 
+	if (uname(&utsname))
+		strcpy(utsname.nodename, "unknown");
+
+	if (!force &&
+	    (fp = fopen(git_path("gc-%s.pid", utsname.nodename), "r")) != NULL &&
+	    !fstat(fileno(fp), &st) &&
+	    /*
+	     * 12 hour limit is very generous as gc should never take
+	     * that long. On the other hand we don't really need a
+	     * strict limit here, running gc --auto one day late is
+	     * not a big problem. --force can be used in manual gc
+	     * after the user verifies that no gc is running.
+	     */
+	    time(NULL) - st.st_mtime <= 12 * 3600) {
+		uintmax_t pid;
+		if (fscanf(fp, "%"PRIuMAX, &pid) == 1 && !kill(pid, 0)) {
+			if (auto_gc)
+				return 0; /* be quiet on --auto */
+			die(_("gc is already running"));
+		}
+		fclose(fp);
+	}
+
+	if ((fp = fopen(git_path("gc-%s.pid", utsname.nodename), "w")) != NULL) {
+		fprintf(fp, "%"PRIuMAX"\n", (uintmax_t) getpid());
+		fclose(fp);
+	}
+
 	if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
 		return error(FAILED_RUN, pack_refs_cmd.argv[0]);
 
@@ -249,5 +282,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 		warning(_("There are too many unreachable loose objects; "
 			"run 'git prune' to remove them."));
 
+	unlink(git_path("gc-%s.pid", utsname.nodename));
+
 	return 0;
 }
compat/mingw.h +6 -0
index bd0a88b..2c25d2d 100644
@@ -68,6 +68,10 @@ struct itimerval {
 };
 #define ITIMER_REAL 0
 
+struct utsname {
+	char nodename[128];
+};
+
 /*
  * sanitize preprocessor namespace polluted by Windows headers defining
  * macros which collide with git local versions
@@ -86,6 +90,8 @@ static inline int fchmod(int fildes, mode_t mode)
 { errno = ENOSYS; return -1; }
 static inline pid_t fork(void)
 { errno = ENOSYS; return -1; }
+static inline int uname(struct utsname *buf)
+{ errno = ENOSYS; return -1; }
 static inline unsigned int alarm(unsigned int seconds)
 { return 0; }
 static inline int fsync(int fd)
git-compat-util.h +1 -0
index 115cb1d..c6a53cb 100644
@@ -139,6 +139,7 @@
 #include <sys/resource.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
+#include <sys/utsname.h>
 #include <termios.h>
 #ifndef NO_SYS_SELECT_H
 #include <sys/select.h>
Signature · 1 line
1.8.2.83.gc99314b
#2 lore raw

Am 03.08.2013 08:21, schrieb Nguyễn Thái Ngọc Duy:

Quoted 2 lines

I changed mingw.h to add a stub uname() because I don't think MinGW port has that function, but that's totally untested.

Thanks, but we don't have kill(pid, 0), either :-(

-- Hannes

author
#3 lore raw
Quoted 5 lines · Johannes Sixt

Am 03.08.2013 08:21, schrieb Nguyễn Thái Ngọc Duy:

Quoted 2 lines

I changed mingw.h to add a stub uname() because I don't think MinGW port has that function, but that's totally untested.

Thanks, but we don't have kill(pid, 0), either :-(

Yeah, I should have checked. Will this work?

-- 8< --

compat/mingw.c +6 -0
index bb92c43..14d92df 100644
@@ -1086,6 +1086,12 @@ int mingw_kill(pid_t pid, int sig)
 		errno = err_win_to_posix(GetLastError());
 		CloseHandle(h);
 		return -1;
+	} else if (pid > 0 && sig == 0) {
+		HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
+		if (h) {
+			CloseHandle(h);
+			return 0;
+		}
 	}
 
 	errno = EINVAL;

-- 8< --

#4 lore raw

Am 03.08.2013 12:01, schrieb Duy Nguyen:

Quoted 19 lines · Johannes Sixt
Quoted 5 lines

Am 03.08.2013 08:21, schrieb Nguyễn Thái Ngọc Duy:

Quoted 2 lines

I changed mingw.h to add a stub uname() because I don't think MinGW port has that function, but that's totally untested.

Thanks, but we don't have kill(pid, 0), either :-(

Yeah, I should have checked. Will this work?

-- 8< --

compat/mingw.c +0 -0
index bb92c43..14d92df 100644
@@ -1086,6 +1086,12 @@ int mingw_kill(pid_t pid, int sig)
 		errno = err_win_to_posix(GetLastError());
 		CloseHandle(h);
 		return -1;

compat/mingw.c int mingw_kill(pid_t pid, int sig)

Quoted 10 lines
compat/mingw.c +6 -1
+	} else if (pid > 0 && sig == 0) {
+		HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
+		if (h) {
+			CloseHandle(h);
+			return 0;
+		}
 	}
 
 	errno = EINVAL;
-- 8< --

Looks reasonable. PROCESS_QUERY_INFORMATION instead of PROCESS_TERMINATE should be sufficient, and errno = ESRCH; return -1; is missing.

-- Hannes

#5 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 8 lines · Nguyễn Thái Ngọc Duy

This may happen when git gc --auto is run automatically, then the user, to avoid wait time, switches to a new terminal, keeps working and git gc --auto is started again because the first gc instance has not clean up the repository.

This patch tries to avoid multiple gc running, especially in --auto mode. In the worst case, gc may be delayed 12 hours if a daemon reuses the pid stored in gc-%s.pid.

Definitely looks like a good solution. Thanks for this.

I'm currently on vacation, so can't apply and test: sorry.

builtin/gc.c int cmd_gc(int argc, const char **argv, const char *prefix) · line 238

Quoted 3 lines
builtin/gc.c +3 -0
+       if (!force &&
+           (fp = fopen(git_path("gc-%s.pid", utsname.nodename), "r")) != NULL &&
+           !fstat(fileno(fp), &st) &&

It's open for a very short period of time, so lockfile (which we'd normally use) would probably be an overkill.

Quoted 1 line

+ time(NULL) - st.st_mtime <= 12 * 3600) {

Quick question: is this kind of file-lifetime used anywhere else in git.git?

builtin/gc.c int cmd_gc(int argc, const char **argv, const char *prefix) · line 251

Quoted 3 lines
builtin/gc.c +3 -0
+                       if (auto_gc)
+                               return 0; /* be quiet on --auto */
+                       die(_("gc is already running"));

Nice.

author
#6 lore raw
Quoted 3 lines · Ramkumar Ramachandra
Quoted 1 line

+ time(NULL) - st.st_mtime <= 12 * 3600) {

Quick question: is this kind of file-lifetime used anywhere else in git.git?

I don't think so.

Signature · 1 line
Duy
#7 lore raw

PATCH v3 1/1 gc: reject if another gc is running, unless --force is given

In reply to Nguyễn Thái Ngọc Duy

This may happen when git gc --auto is run automatically, then the user, to avoid wait time, switches to a new terminal, keeps working and git gc --auto is started again because the first gc instance has not clean up the repository.

This patch tries to avoid multiple gc running, especially in --auto mode. In the worst case, gc may be delayed 12 hours if a daemon reuses the pid stored in gc-%s.pid.

uname() and kill(..,0) are added to MinGW compatibility layer so that it'll work on Windows. Actually uname() is stub so it won't prevent multiple gc running on a shared repo. But that's for Windows contributors to step in.

---
- new code is taken out in a separate function for clarity
- file locking
- implementing kill(pid, 0) on windows (but not tested)

 Documentation/git-gc.txt |  6 ++++-
 builtin/gc.c             | 62 ++++++++++++++++++++++++++++++++++++++++++++++++
 compat/mingw.c           |  6 +++++
 compat/mingw.h           |  6 +++++
 git-compat-util.h        |  1 +
 5 files changed, 80 insertions(+), 1 deletion(-)
Documentation/git-gc.txt +5 -1
index 2402ed6..e158a3b 100644
@@ -9,7 +9,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
 SYNOPSIS
 --------
 [verse]
-'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
+'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force]
 
 DESCRIPTION
 -----------
@@ -72,6 +72,10 @@ automatic consolidation of packs.
 --quiet::
 	Suppress all progress reports.
 
+--force::
+	Force `git gc` to run even if there may be another `git gc`
+	instance running on this repository.
+
 Configuration
 -------------
 
builtin/gc.c +62 -0

85 lines not on this page · raw message

compat/mingw.c +6 -0
index bb92c43..22ee9ef 100644
@@ -1086,6 +1086,12 @@ int mingw_kill(pid_t pid, int sig)
 		errno = err_win_to_posix(GetLastError());
 		CloseHandle(h);
 		return -1;
+	} else if (pid > 0 && sig == 0) {
+		HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
+		if (h) {
+			CloseHandle(h);
+			return 0;
+		}
 	}
 
 	errno = EINVAL;
compat/mingw.h +6 -0
index bd0a88b..2c25d2d 100644
@@ -68,6 +68,10 @@ struct itimerval {
 };
 #define ITIMER_REAL 0
 
+struct utsname {
+	char nodename[128];
+};
+
 /*
  * sanitize preprocessor namespace polluted by Windows headers defining
  * macros which collide with git local versions
@@ -86,6 +90,8 @@ static inline int fchmod(int fildes, mode_t mode)
 { errno = ENOSYS; return -1; }
 static inline pid_t fork(void)
 { errno = ENOSYS; return -1; }
+static inline int uname(struct utsname *buf)
+{ errno = ENOSYS; return -1; }
 static inline unsigned int alarm(unsigned int seconds)
 { return 0; }
 static inline int fsync(int fd)
git-compat-util.h +1 -0
index 115cb1d..c6a53cb 100644
@@ -139,6 +139,7 @@
 #include <sys/resource.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
+#include <sys/utsname.h>
 #include <termios.h>
 #ifndef NO_SYS_SELECT_H
 #include <sys/select.h>
Signature · 1 line
1.8.2.83.gc99314b
#8 lore raw

builtin/gc.c static int need_to_gc(void) · line 170

Quoted 9 lines · Nguyễn Thái Ngọc Duy
builtin/gc.c +1 -0
index 6be6c8d..1f33908 100644
@@ -167,11 +167,66 @@ static int need_to_gc(void)
 	return 1;
 }
 
+static int gc_running(int force)

Sounds like a bool asking "Is a GC running? Yes, or no?". Since there is no room for "force" to enter in order to answer that question, I have to guess that this function is somewhat misnamed.

Quoted 8 lines
builtin/gc.c +8 -0
+{
+	static struct lock_file lock;
+	struct utsname utsname;
+	struct stat st;
+	uintmax_t pid;
+	FILE *fp;
+	int fd, should_exit;
+

builtin/gc.c static int need_to_gc(void) · line 188

Quoted 10 lines
builtin/gc.c +10 -0
+	if (uname(&utsname))
+		strcpy(utsname.nodename, "unknown");
+
+	fd = hold_lock_file_for_update(&lock,
+			git_path("gc-%s.pid", utsname.nodename), 0);
+	if (!force) {
+		if (fd < 0)
+			return 1;
+
+		fp = fopen(git_path("gc-%s.pid", utsname.nodename), "r");

I would have imagined that you would use a lockfile gc.pid and write nodename and pid to it (and if nodename matches, you know pid may have a chance to actually match another instance of "gc", while there will not way it matches if nodename is different, and do something intelligent about it). By letting GC that is running on another node to be completely unnoticed, this change is closing the door to "do something intelligent about it", like giving it the same 12 hour limit.

Quoted 24 lines
builtin/gc.c +24 -0
+		should_exit =
+			fp != NULL &&
+			!fstat(fileno(fp), &st) &&
+			/*
+			 * 12 hour limit is very generous as gc should
+			 * never take that long. On the other hand we
+			 * don't really need a strict limit here,
+			 * running gc --auto one day late is not a big
+			 * problem. --force can be used in manual gc
+			 * after the user verifies that no gc is
+			 * running.
+			 */
+			time(NULL) - st.st_mtime <= 12 * 3600 &&
+			fscanf(fp, "%"PRIuMAX, &pid) == 1 &&
+			!kill(pid, 0);
+		if (fp != NULL)
+			fclose(fp);
+		if (should_exit) {
+			if (fd >= 0)
+				rollback_lock_file(&lock);
+			return 1;
+		}
+	}
+

builtin/gc.c static int need_to_gc(void) · line 222

Quoted 10 lines
builtin/gc.c +10 -0
+	if (fd >= 0) {
+		struct strbuf sb = STRBUF_INIT;
+		strbuf_addf(&sb, "%"PRIuMAX"\n", (uintmax_t) getpid());
+		write_in_full(fd, sb.buf, sb.len);
+		strbuf_release(&sb);
+		commit_lock_file(&lock);
+	}
+
+	return 0;
+}

After reading what the whole function does, I think the purpose of this function is to take gc-lock (with optionally force). Perhaps a name along the lines of "lock_gc", "gc_lock", "lock_repo_for_gc", would be more appropriate.

#9 lore raw
Quoted 1 line · Junio C Hamano

[...]

The other comments mostly make sense.

Quoted 4 lines

After reading what the whole function does, I think the purpose of this function is to take gc-lock (with optionally force). Perhaps a name along the lines of "lock_gc", "gc_lock", "lock_repo_for_gc", would be more appropriate.

The whole point of this exercise is to _not_ lock up the repo during gc, so I can do minimal commit/ worktree/ ref update operations when it's running. I can't expect the reflog to work, so complex history-rewriting operations should be avoided; that's about it, I think.

#10 lore raw
Quoted 5 lines · Ramkumar Ramachandra
Quoted 1 line · Junio C Hamano

[...]

The other comments mostly make sense.

Quoted 7 lines
Quoted 4 lines

After reading what the whole function does, I think the purpose of this function is to take gc-lock (with optionally force). Perhaps a name along the lines of "lock_gc", "gc_lock", "lock_repo_for_gc", would be more appropriate.

The whole point of this exercise is to _not_ lock up the repo during gc,...

I do not think it is a misnomer to call the entity that locks other instances of gc's "a lock on the repository for gc". Nothing in Duy's code suggests any other commands paying attention to this mechanism and stalling, and I think my comments were clear enough that I was not suggesting such a change.

So I am not sure what you are complaining.

#11 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 7 lines
Quoted 4 lines

After reading what the whole function does, I think the purpose of this function is to take gc-lock (with optionally force). Perhaps a name along the lines of "lock_gc", "gc_lock", "lock_repo_for_gc", would be more appropriate.

The whole point of this exercise is to _not_ lock up the repo during gc,...

Quoted 7 lines

I do not think it is a misnomer to call the entity that locks other instances of gc's "a lock on the repository for gc". Nothing in Duy's code suggests any other commands paying attention to this mechanism and stalling, and I think my comments were clear enough that I was not suggesting such a change.

So I am not sure what you are complaining.

Not complaining; I wrote it down because of your "lock_repo_for_gc" suggestion.

#12 lore raw

PATCH v4 1/1 gc: reject if another gc is running, unless --force is given

In reply to Nguyễn Thái Ngọc Duy

This may happen when git gc --auto is run automatically, then the user, to avoid wait time, switches to a new terminal, keeps working and git gc --auto is started again because the first gc instance has not clean up the repository.

This patch tries to avoid multiple gc running, especially in --auto mode. In the worst case, gc may be delayed 12 hours if a daemon reuses the pid stored in gc.pid.

kill(pid, 0) support is added to MinGW port so it should work on Windows too.

---
this patch is getting cooler:

- uname() is dropped in favor of gethostbyname(), which is supported by MinGW port.
- host name is stored in gc.pid as junio suggested so...
- now we can say "gc is already running on _this host_ with _this pid_..."

 Documentation/git-gc.txt |  6 ++++-
 builtin/gc.c             | 67 ++++++++++++++++++++++++++++++++++++++++++++++++
 compat/mingw.c           |  6 +++++
 3 files changed, 78 insertions(+), 1 deletion(-)
Documentation/git-gc.txt +5 -1
index 2402ed6..e158a3b 100644
@@ -9,7 +9,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
 SYNOPSIS
 --------
 [verse]
-'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
+'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force]
 
 DESCRIPTION
 -----------
@@ -72,6 +72,10 @@ automatic consolidation of packs.
 --quiet::
 	Suppress all progress reports.
 
+--force::
+	Force `git gc` to run even if there may be another `git gc`
+	instance running on this repository.
+
 Configuration
 -------------
 
builtin/gc.c +67 -0

90 lines not on this page · raw message

compat/mingw.c +6 -0
index bb92c43..22ee9ef 100644
@@ -1086,6 +1086,12 @@ int mingw_kill(pid_t pid, int sig)
 		errno = err_win_to_posix(GetLastError());
 		CloseHandle(h);
 		return -1;
+	} else if (pid > 0 && sig == 0) {
+		HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
+		if (h) {
+			CloseHandle(h);
+			return 0;
+		}
 	}
 
 	errno = EINVAL;
Signature · 1 line
1.8.2.83.gc99314b
#13 lore raw
Quoted 17 lines · Nguyễn Thái Ngọc Duy
builtin/gc.c +12 -0
index 6be6c8d..99682f0 100644
@@ -167,11 +167,69 @@ static int need_to_gc(void)
+ ...
+	fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
+				       LOCK_DIE_ON_ERROR);
+	if (!force) {
+		fp = fopen(git_path("gc.pid"), "r");
+		memset(locking_host, 0, sizeof(locking_host));
+		should_exit =
+			fp != NULL &&
+			!fstat(fileno(fp), &st) &&
+			/*
+			 * 12 hour limit is very generous as gc should
+			 * never take that long. On the other hand we

builtin/gc.c static int need_to_gc(void) · line 188

Quoted 10 lines
builtin/gc.c +10 -0
+			 * don't really need a strict limit here,
+			 * running gc --auto one day late is not a big
+			 * problem. --force can be used in manual gc
+			 * after the user verifies that no gc is
+			 * running.
+			 */
+			time(NULL) - st.st_mtime <= 12 * 3600 &&
+			fscanf(fp, "%"PRIuMAX" %127c", &pid, locking_host) == 2 &&
+			!strcmp(locking_host, my_host) &&
+			!kill(pid, 0);

If there is a lockfile we can read, and if we can positively determine that the process named in the lockfile is still running, then we definitely do not want to do another "gc". That part is good.

If the lock is very stale, 12-hour test will kick in, and we do not read who locked it, nor check if the locker is still alive. By doing so, we avoid misidentifying a new process that is unrelated to the locker who died and left the lockfile behind, which is a good thing. The logic to ignore such lockfile as "unknown" applies equally to a remote locker on a lockfile that is old. So the logic for an old lockfile is good, too.

When we see a recent lockfile created by a "gc" running elsewhere, we do not set "should_exit". Is that a good thing? I am wondering if the last two lines should be:

- !strcmp(locking_host, my_host) &&
- !kill(pid, 0);
+ (strcmp(locking_host, my_host) || !kill(pid, 0));

instead.

Thanks, looking good.

author
#14 lore raw
Quoted 9 lines · Junio C Hamano

When we see a recent lockfile created by a "gc" running elsewhere, we do not set "should_exit". Is that a good thing? I am wondering if the last two lines should be:

(quoted diff) +1 -2
-       !strcmp(locking_host, my_host) &&
-       !kill(pid, 0);
+       (strcmp(locking_host, my_host) || !kill(pid, 0));

instead.

Yes I think it should (we still have the 12-hour check to override stale locks anyway). Should I send another patch or you do it yourself (seeing that you have this chunk pasted here, you might have it saved somewhere already)

Signature · 1 line
Duy
#15 lore raw
Quoted 9 lines · Duy Nguyen
Quoted 8 lines · Junio C Hamano

When we see a recent lockfile created by a "gc" running elsewhere, we do not set "should_exit". Is that a good thing? I am wondering if the last two lines should be:

(quoted diff) +1 -2
-       !strcmp(locking_host, my_host) &&
-       !kill(pid, 0);
+       (strcmp(locking_host, my_host) || !kill(pid, 0));
Quoted 6 lines
Quoted 1 line

instead.

Yes I think it should (we still have the 12-hour check to override stale locks anyway). Should I send another patch or you do it yourself (seeing that you have this chunk pasted here, you might have it saved somewhere already)

The above was typed in my MUA ;-), but it is an easy update I can do so will do so anyway.

Thanks for double-checking.

#16 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 57 lines · Nguyễn Thái Ngọc Duy

This may happen when git gc --auto is run automatically, then the user, to avoid wait time, switches to a new terminal, keeps working and git gc --auto is started again because the first gc instance has not clean up the repository.

This patch tries to avoid multiple gc running, especially in --auto mode. In the worst case, gc may be delayed 12 hours if a daemon reuses the pid stored in gc.pid.

kill(pid, 0) support is added to MinGW port so it should work on Windows too.

---
this patch is getting cooler:

- uname() is dropped in favor of gethostbyname(), which is supported by MinGW port.
- host name is stored in gc.pid as junio suggested so...
- now we can say "gc is already running on _this host_ with _this pid_..."

 Documentation/git-gc.txt |  6 ++++-
 builtin/gc.c             | 67 ++++++++++++++++++++++++++++++++++++++++++++++++
 compat/mingw.c           |  6 +++++
 3 files changed, 78 insertions(+), 1 deletion(-)
Documentation/git-gc.txt +5 -1
index 2402ed6..e158a3b 100644
@@ -9,7 +9,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
 SYNOPSIS
 --------
 [verse]
-'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
+'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force]
 
 DESCRIPTION
 -----------
@@ -72,6 +72,10 @@ automatic consolidation of packs.
 --quiet::
        Suppress all progress reports.
 
+--force::
+       Force `git gc` to run even if there may be another `git gc`
+       instance running on this repository.
+
 Configuration
 -------------
 
builtin/gc.c +0 -0
index 6be6c8d..99682f0 100644
@@ -167,11 +167,69 @@ static int need_to_gc(void)
        return 1;
 }

builtin/gc.c static int need_to_gc(void) · line 178

Quoted 10 lines
builtin/gc.c +9 -0
 
+/* return NULL on success, else hostname running the gc */
+static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
+{
+       static struct lock_file lock;
+       static char locking_host[128];
+       char my_host[128];
+       struct strbuf sb = STRBUF_INIT;
+       struct stat st;
+       uintmax_t pid;

pid_t is always an signed type, therefore unintmax_t does not make sense as a catch all value

fork() returns -1 on failure, and its return type is pid_t. i don't know what fantasy unix system has an unsigned pid_t

Quoted 15 lines
builtin/gc.c +15 -0
+       FILE *fp;
+       int fd, should_exit;
+
+       if (gethostname(my_host, sizeof(my_host)))
+               strcpy(my_host, "unknown");
+
+       fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
+                                      LOCK_DIE_ON_ERROR);
+       if (!force) {
+               fp = fopen(git_path("gc.pid"), "r");
+               memset(locking_host, 0, sizeof(locking_host));
+               should_exit =
+                       fp != NULL &&
+                       !fstat(fileno(fp), &st) &&
+                       /*

builtin/gc.c static int need_to_gc(void) · line 203

Quoted 10 lines
builtin/gc.c +10 -0
+                        * 12 hour limit is very generous as gc should
+                        * never take that long. On the other hand we
+                        * don't really need a strict limit here,
+                        * running gc --auto one day late is not a big
+                        * problem. --force can be used in manual gc
+                        * after the user verifies that no gc is
+                        * running.
+                        */
+                       time(NULL) - st.st_mtime <= 12 * 3600 &&
+                       fscanf(fp, "%"PRIuMAX" %127c", &pid, locking_host) == 2 &&

similar comment wrt PRIuMAX

builtin/gc.c static int need_to_gc(void) · line 212

Quoted 9 lines
builtin/gc.c +9 -0
+                       !strcmp(locking_host, my_host) &&
+                       !kill(pid, 0);
+               if (fp != NULL)
+                       fclose(fp);
+               if (should_exit) {
+                       if (fd >= 0)
+                               rollback_lock_file(&lock);
+                       *ret_pid = pid;
+                       return locking_host;

why not exponential backoff?

Quoted 71 lines
builtin/gc.c +24 -0
+               }
+       }
+
+       strbuf_addf(&sb, "%"PRIuMAX" %s",
+                   (uintmax_t) getpid(), my_host);
+       write_in_full(fd, sb.buf, sb.len);
+       strbuf_release(&sb);
+       commit_lock_file(&lock);
+
+       return NULL;
+}
+
 int cmd_gc(int argc, const char **argv, const char *prefix)
 {
        int aggressive = 0;
        int auto_gc = 0;
        int quiet = 0;
+       int force = 0;
+       const char *name;
+       pid_t pid;
 
        struct option builtin_gc_options[] = {
                OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -180,6 +238,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
                        PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
                OPT_BOOLEAN(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
                OPT_BOOLEAN(0, "auto", &auto_gc, N_("enable auto-gc mode")),
+               OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
                OPT_END()
        };
 
@@ -225,6 +284,14 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
        } else
                add_repack_all_option();
 
+       name = lock_repo_for_gc(force, &pid);
+       if (name) {
+               if (auto_gc)
+                       return 0; /* be quiet on --auto */
+               die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
+                   name, (uintmax_t)pid);
+       }
+
        if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
                return error(FAILED_RUN, pack_refs_cmd.argv[0]);
 
compat/mingw.c +6 -1
index bb92c43..22ee9ef 100644
@@ -1086,6 +1086,12 @@ int mingw_kill(pid_t pid, int sig)
                errno = err_win_to_posix(GetLastError());
                CloseHandle(h);
                return -1;
+       } else if (pid > 0 && sig == 0) {
+               HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
+               if (h) {
+                       CloseHandle(h);
+                       return 0;
+               }
        }
 
        errno = EINVAL;
--

1.8.2.83.gc99314b

--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#17 lore raw
Quoted 4 lines · Andres Perera
Quoted 4 lines
builtin/gc.c +4 -0
+/* return NULL on success, else hostname running the gc */
+static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
+{
+       static struct lock_file lock;
Quoted 8 lines

builtin/gc.c static int need_to_gc(void) · line 178

Quoted 5 lines
builtin/gc.c +5 -0
+       static char locking_host[128];
+       char my_host[128];
+       struct strbuf sb = STRBUF_INIT;
+       struct stat st;
+       uintmax_t pid;

pid_t is always an signed type, therefore unintmax_t does not make sense as a catch all value

Good eyes.

Quoted 11 lines

builtin/gc.c static int need_to_gc(void) · line 212

Quoted 9 lines
builtin/gc.c +9 -0
+                       !strcmp(locking_host, my_host) &&
+                       !kill(pid, 0);
+               if (fp != NULL)
+                       fclose(fp);
+               if (should_exit) {
+                       if (fd >= 0)
+                               rollback_lock_file(&lock);
+                       *ret_pid = pid;
+                       return locking_host;

why not exponential backoff?

If the other guy is doing a GC, and we decide that we should exit, it is *not* because we want to wait until the other guy is done. It is because we know we do not have to do the work --- the other guy is doing what we were about to do, and it will do it for us anyway.

So I do not think it makes any sense to do exponential backoff if "gc --auto" is asking "should we exit" to this logic.

An explicit "gc", on the other hand, may benefit from backoff, but then the user can choose to do so himself, and more importantly, the user can see "ah, another one is running so enough cruft will be cleaned up anyway" and choose not to run it.

author
#18 lore raw
In reply to Andres Perera
Quoted 5 lines · Andres Perera
Quoted 1 line · Nguyễn Thái Ngọc Duy

+ uintmax_t pid;

pid_t is always an signed type, therefore unintmax_t does not make sense as a catch all value

I only catch real process id. In practice we don't have processes with negative pid_t, do we? I can't find any document about this, but at least waitpid seems to treat negative pid (except -1) just as an indicator while the true pid is the positive counterpart.

Quoted 2 lines

fork() returns -1 on failure, and its return type is pid_t. i don't know what fantasy unix system has an unsigned pid_t

Signature · 1 line
Duy
#19 lore raw
Quoted 11 lines · Duy Nguyen
Quoted 5 lines · Andres Perera
Quoted 1 line · Nguyễn Thái Ngọc Duy

+ uintmax_t pid;

pid_t is always an signed type, therefore unintmax_t does not make sense as a catch all value

I only catch real process id. In practice we don't have processes with negative pid_t, do we? I can't find any document about this, but at least waitpid seems to treat negative pid (except -1) just as an indicator while the true pid is the positive counterpart.

Negative pids are used for denoting process groups in various interfaces. No process can have a negative pid.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."