git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] gc: remove gc.pid file at end of execution
@ 2013-09-26  9:18 Matthieu Moy
  2013-09-28  0:33 ` Jonathan Nieder
  0 siblings, 1 reply; 5+ messages in thread
From: Matthieu Moy @ 2013-09-26  9:18 UTC (permalink / raw)
  To: git, gitster; +Cc: pclouds, Matthieu Moy

This file isn't really harmful, but isn't useful either, and can create
minor annoyance for the user:

* It's confusing, as the presence of a *.pid file often implies that a
  process is currently running. A user running "ls .git/" and finding
  this file may incorrectly guess that a "git gc" is currently running.

* Leaving this file means that a "git gc" in an already gc-ed repo is
  no-longer a no-op. A user running "git gc" in a set of repositories,
  and then synchronizing this set (e.g. rsync -av, unison, ...) will see
  all the gc.pid files as changed, which creates useless noise.

This patch unlinks the file after the garbage collection is done, so that
gc.pid is actually present only during execution.

Future versions of Git may want to use the information left in the gc.pid
file (e.g. for policies like "don't attempt to run a gc if one has
already been ran less than X hours ago"). If so, this patch can safely be
reverted. For now, let's not bother the users.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 builtin/gc.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/builtin/gc.c b/builtin/gc.c
index 891a2c2..db39963 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -316,5 +316,9 @@ 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."));
 
+	/*
+	 * The file isn't harmful, but isn't useful either.
+	 */
+	unlink(git_path("gc.pid"));
 	return 0;
 }
-- 
1.8.4.474.g128a96c

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] gc: remove gc.pid file at end of execution
  2013-09-26  9:18 [PATCH] gc: remove gc.pid file at end of execution Matthieu Moy
@ 2013-09-28  0:33 ` Jonathan Nieder
  2013-09-28  3:42   ` Duy Nguyen
  2013-10-16 21:38   ` Junio C Hamano
  0 siblings, 2 replies; 5+ messages in thread
From: Jonathan Nieder @ 2013-09-28  0:33 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: git, gitster, pclouds

Matthieu Moy wrote:

> This file isn't really harmful, but isn't useful either, and can create
> minor annoyance for the user:

Would something like the following make sense, to ensure the gc.pid file is
always removed on normal exit?

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

diff --git c/builtin/gc.c i/builtin/gc.c
index 6e0d81a..fbbc144 100644
--- c/builtin/gc.c
+++ i/builtin/gc.c
@@ -14,6 +14,7 @@
 #include "cache.h"
 #include "parse-options.h"
 #include "run-command.h"
+#include "sigchain.h"
 #include "argv-array.h"
 
 #define FAILED_RUN "failed to run %s"
@@ -167,6 +168,21 @@ static int need_to_gc(void)
 	return 1;
 }
 
+static char *pidfile;
+
+static void remove_pidfile(void)
+{
+	if (pidfile)
+		unlink(pidfile);
+}
+
+static void remove_pidfile_on_signal(int signo)
+{
+	remove_pidfile();
+	sigchain_pop(signo);
+	raise(signo);
+}
+
 /* return NULL on success, else hostname running the gc */
 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
 {
@@ -179,13 +195,19 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
 	FILE *fp;
 	int fd, should_exit;
 
+	if (pidfile)
+		/* already locked */
+		return NULL;
+
 	if (gethostname(my_host, sizeof(my_host)))
 		strcpy(my_host, "unknown");
 
-	fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
+	pidfile = git_pathdup("gc.pid");
+
+	fd = hold_lock_file_for_update(&lock, pidfile,
 				       LOCK_DIE_ON_ERROR);
 	if (!force) {
-		fp = fopen(git_path("gc.pid"), "r");
+		fp = fopen(pidfile, "r");
 		memset(locking_host, 0, sizeof(locking_host));
 		should_exit =
 			fp != NULL &&
@@ -208,6 +230,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
 		if (should_exit) {
 			if (fd >= 0)
 				rollback_lock_file(&lock);
+			pidfile = NULL;
 			*ret_pid = pid;
 			return locking_host;
 		}
@@ -219,6 +242,9 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
 	strbuf_release(&sb);
 	commit_lock_file(&lock);
 
+	sigchain_push_common(remove_pidfile_on_signal);
+	atexit(remove_pidfile);
+
 	return NULL;
 }
 

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] gc: remove gc.pid file at end of execution
  2013-09-28  0:33 ` Jonathan Nieder
@ 2013-09-28  3:42   ` Duy Nguyen
  2013-10-16 21:38   ` Junio C Hamano
  1 sibling, 0 replies; 5+ messages in thread
From: Duy Nguyen @ 2013-09-28  3:42 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Matthieu Moy, Git Mailing List, Junio C Hamano

On Sat, Sep 28, 2013 at 7:33 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Matthieu Moy wrote:
>
>> This file isn't really harmful, but isn't useful either, and can create
>> minor annoyance for the user:
>
> Would something like the following make sense, to ensure the gc.pid file is
> always removed on normal exit?
>
> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
>
> diff --git c/builtin/gc.c i/builtin/gc.c
> index 6e0d81a..fbbc144 100644
> --- c/builtin/gc.c
> +++ i/builtin/gc.c
> @@ -14,6 +14,7 @@
>  #include "cache.h"
>  #include "parse-options.h"
>  #include "run-command.h"
> +#include "sigchain.h"
>  #include "argv-array.h"
>
>  #define FAILED_RUN "failed to run %s"
> @@ -167,6 +168,21 @@ static int need_to_gc(void)
>         return 1;
>  }
>
> +static char *pidfile;
> +
> +static void remove_pidfile(void)
> +{
> +       if (pidfile)
> +               unlink(pidfile);
> +}
> +
> +static void remove_pidfile_on_signal(int signo)
> +{
> +       remove_pidfile();
> +       sigchain_pop(signo);
> +       raise(signo);
> +}
> +
>  /* return NULL on success, else hostname running the gc */
>  static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
>  {
> @@ -179,13 +195,19 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
>         FILE *fp;
>         int fd, should_exit;
>
> +       if (pidfile)
> +               /* already locked */
> +               return NULL;
> +
>         if (gethostname(my_host, sizeof(my_host)))
>                 strcpy(my_host, "unknown");
>
> -       fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
> +       pidfile = git_pathdup("gc.pid");
> +
> +       fd = hold_lock_file_for_update(&lock, pidfile,
>                                        LOCK_DIE_ON_ERROR);
>         if (!force) {
> -               fp = fopen(git_path("gc.pid"), "r");
> +               fp = fopen(pidfile, "r");
>                 memset(locking_host, 0, sizeof(locking_host));
>                 should_exit =
>                         fp != NULL &&
> @@ -208,6 +230,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
>                 if (should_exit) {
>                         if (fd >= 0)
>                                 rollback_lock_file(&lock);
> +                       pidfile = NULL;
>                         *ret_pid = pid;
>                         return locking_host;
>                 }
> @@ -219,6 +242,9 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
>         strbuf_release(&sb);

It may be a bit simpler to delay setting pidfile until we get here.
lock.filename still contains gc.pid until commit_lock_file is called.

>         commit_lock_file(&lock);
>
> +       sigchain_push_common(remove_pidfile_on_signal);
> +       atexit(remove_pidfile);
> +
>         return NULL;
>  }
-- 
Duy

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] gc: remove gc.pid file at end of execution
  2013-09-28  0:33 ` Jonathan Nieder
  2013-09-28  3:42   ` Duy Nguyen
@ 2013-10-16 21:38   ` Junio C Hamano
  2013-10-16 23:11     ` [PATCH v2] " Jonathan Nieder
  1 sibling, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2013-10-16 21:38 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Matthieu Moy, git, pclouds

Jonathan Nieder <jrnieder@gmail.com> writes:

> Matthieu Moy wrote:
>
>> This file isn't really harmful, but isn't useful either, and can create
>> minor annoyance for the user:
>
> Would something like the following make sense, to ensure the gc.pid file is
> always removed on normal exit?

Has anything further happened to this discussion?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] gc: remove gc.pid file at end of execution
  2013-10-16 21:38   ` Junio C Hamano
@ 2013-10-16 23:11     ` Jonathan Nieder
  0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Nieder @ 2013-10-16 23:11 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Matthieu Moy, git, pclouds

This file isn't really harmful, but isn't useful either, and can create
minor annoyance for the user:

* It's confusing, as the presence of a *.pid file often implies that a
  process is currently running. A user running "ls .git/" and finding
  this file may incorrectly guess that a "git gc" is currently running.

* Leaving this file means that a "git gc" in an already gc-ed repo is
  no-longer a no-op. A user running "git gc" in a set of repositories,
  and then synchronizing this set (e.g. rsync -av, unison, ...) will see
  all the gc.pid files as changed, which creates useless noise.

This patch unlinks the file after the garbage collection is done, so that
gc.pid is actually present only during execution.

Future versions of Git may want to use the information left in the gc.pid
file (e.g. for policies like "don't attempt to run a gc if one has
already been ran less than X hours ago"). If so, this patch can safely be
reverted. For now, let's not bother the users.

Explained-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Improved-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
Junio C Hamano wrote:

> Has anything further happened to this discussion?

Here's a patch implementing Duy's suggestion.

 builtin/gc.c  | 24 ++++++++++++++++++++++++
 t/t6500-gc.sh |  5 +++++
 2 files changed, 29 insertions(+)

diff --git a/builtin/gc.c b/builtin/gc.c
index 891a2c2..c14190f 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -14,6 +14,7 @@
 #include "cache.h"
 #include "parse-options.h"
 #include "run-command.h"
+#include "sigchain.h"
 #include "argv-array.h"
 
 #define FAILED_RUN "failed to run %s"
@@ -35,6 +36,21 @@ static struct argv_array repack = ARGV_ARRAY_INIT;
 static struct argv_array prune = ARGV_ARRAY_INIT;
 static struct argv_array rerere = ARGV_ARRAY_INIT;
 
+static char *pidfile;
+
+static void remove_pidfile(void)
+{
+	if (pidfile)
+		unlink(pidfile);
+}
+
+static void remove_pidfile_on_signal(int signo)
+{
+	remove_pidfile();
+	sigchain_pop(signo);
+	raise(signo);
+}
+
 static int gc_config(const char *var, const char *value, void *cb)
 {
 	if (!strcmp(var, "gc.packrefs")) {
@@ -179,6 +195,10 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
 	FILE *fp;
 	int fd, should_exit;
 
+	if (pidfile)
+		/* already locked */
+		return NULL;
+
 	if (gethostname(my_host, sizeof(my_host)))
 		strcpy(my_host, "unknown");
 
@@ -219,6 +239,10 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
 	strbuf_release(&sb);
 	commit_lock_file(&lock);
 
+	pidfile = git_pathdup("gc.pid");
+	sigchain_push_common(remove_pidfile_on_signal);
+	atexit(remove_pidfile);
+
 	return NULL;
 }
 
diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh
index b1a6365..63194d8 100755
--- a/t/t6500-gc.sh
+++ b/t/t6500-gc.sh
@@ -9,6 +9,11 @@ test_expect_success 'gc empty repository' '
 	git gc
 '
 
+test_expect_success 'gc does not leave behind pid file' '
+	git gc &&
+	test_path_is_missing .git/gc.pid
+'
+
 test_expect_success 'gc --gobbledegook' '
 	test_expect_code 129 git gc --nonsense 2>err &&
 	test_i18ngrep "[Uu]sage: git gc" err
-- 
1.8.4-50-g437ce60

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-10-16 23:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-26  9:18 [PATCH] gc: remove gc.pid file at end of execution Matthieu Moy
2013-09-28  0:33 ` Jonathan Nieder
2013-09-28  3:42   ` Duy Nguyen
2013-10-16 21:38   ` Junio C Hamano
2013-10-16 23:11     ` [PATCH v2] " Jonathan Nieder

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).