Handle atexit list internaly for unthreaded builds
authorEtienne Buira <etienne.buira@gmail.com>
Sat, 18 Oct 2014 12:31:15 +0000 (14:31 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sun, 19 Oct 2014 22:38:30 +0000 (15:38 -0700)
Wrap atexit()s calls on unthreaded builds to handle callback list
internally.

This is needed because on unthreaded builds, asyncs inherits parent's
atexit() list, that gets run as soon as the async exit()s (and again at
the end of async's parent process). That led to remove temporary files
too early.

Also remove a by-atexit-callback guard against this kind of issue in
clone.c, as this patch makes it redundant.

Fixes test 5537 (temporary shallow file vanished before unpack-objects
could open it)

BTW remove an unused variable in shallow.c.

Helped-by: Duy Nguyen <pclouds@gmail.com>
Helped-by: Andreas Schwab <schwab@linux-m68k.org>
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Etienne Buira <etienne.buira@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clone.c
git-compat-util.h
run-command.c
shallow.c
index 3927edfb6ede269929dc704cbb928a7516a8a0a7..4340477925b3ce0d665d1ddabba29f0d83c58fb1 100644 (file)
@@ -390,7 +390,6 @@ static void clone_local(const char *src_repo, const char *dest_repo)
 
 static const char *junk_work_tree;
 static const char *junk_git_dir;
-static pid_t junk_pid;
 static enum {
        JUNK_LEAVE_NONE,
        JUNK_LEAVE_REPO,
@@ -417,8 +416,6 @@ static void remove_junk(void)
                break;
        }
 
-       if (getpid() != junk_pid)
-               return;
        if (junk_git_dir) {
                strbuf_addstr(&sb, junk_git_dir);
                remove_dir_recursively(&sb, 0);
@@ -759,8 +756,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
        struct refspec *refspec;
        const char *fetch_pattern;
 
-       junk_pid = getpid();
-
        packet_trace_identity("clone");
        argc = parse_options(argc, argv, prefix, builtin_clone_options,
                             builtin_clone_usage, 0);
index fb41118c0705c628bed093c4a915a58aa01a5a1a..ffbedd8fb9edd8a0344b22238f0034f385fbd3c6 100644 (file)
@@ -594,6 +594,11 @@ int inet_pton(int af, const char *src, void *dst);
 const char *inet_ntop(int af, const void *src, char *dst, size_t size);
 #endif
 
+#ifdef NO_PTHREADS
+#define atexit git_atexit
+extern int git_atexit(void (*handler)(void));
+#endif
+
 extern void release_pack_memory(size_t);
 
 typedef void (*try_to_free_t)(size_t);
index 761f0fde40c91a3cc910c092c5aa9ad56f2f0042..41d94c111edad552a375921dce464f0d10d56319 100644 (file)
@@ -620,6 +620,45 @@ static int async_die_is_recursing(void)
        return ret != NULL;
 }
 
+#else
+
+static struct {
+       void (**handlers)(void);
+       size_t nr;
+       size_t alloc;
+} git_atexit_hdlrs;
+
+static int git_atexit_installed;
+
+static void git_atexit_dispatch()
+{
+       size_t i;
+
+       for (i=git_atexit_hdlrs.nr ; i ; i--)
+               git_atexit_hdlrs.handlers[i-1]();
+}
+
+static void git_atexit_clear()
+{
+       free(git_atexit_hdlrs.handlers);
+       memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
+       git_atexit_installed = 0;
+}
+
+#undef atexit
+int git_atexit(void (*handler)(void))
+{
+       ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
+       git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
+       if (!git_atexit_installed) {
+               if (atexit(&git_atexit_dispatch))
+                       return -1;
+               git_atexit_installed = 1;
+       }
+       return 0;
+}
+#define atexit git_atexit
+
 #endif
 
 int start_async(struct async *async)
@@ -678,6 +717,7 @@ int start_async(struct async *async)
                        close(fdin[1]);
                if (need_out)
                        close(fdout[0]);
+               git_atexit_clear();
                exit(!!async->proc(proc_in, proc_out, async->data));
        }
 
index 57f4afa6b40585c663d0df78494ce2108611ce4b..0cc3d47634b675bc25cb2936987fffe2f7d0518d 100644 (file)
--- a/shallow.c
+++ b/shallow.c
@@ -226,7 +226,6 @@ static void remove_temporary_shallow_on_signal(int signo)
 
 const char *setup_temporary_shallow(const struct sha1_array *extra)
 {
-       static int installed_handler;
        struct strbuf sb = STRBUF_INIT;
        int fd;
 
@@ -237,10 +236,8 @@ const char *setup_temporary_shallow(const struct sha1_array *extra)
                strbuf_addstr(&temporary_shallow, git_path("shallow_XXXXXX"));
                fd = xmkstemp(temporary_shallow.buf);
 
-               if (!installed_handler) {
-                       atexit(remove_temporary_shallow);
-                       sigchain_push_common(remove_temporary_shallow_on_signal);
-               }
+               atexit(remove_temporary_shallow);
+               sigchain_push_common(remove_temporary_shallow_on_signal);
 
                if (write_in_full(fd, sb.buf, sb.len) != sb.len)
                        die_errno("failed to write to %s",