diff --git a/cache.h b/cache.h index 7b6b89fc4c0c28..ad725d502101aa 100644 --- a/cache.h +++ b/cache.h @@ -1662,6 +1662,7 @@ extern void write_file(const char *path, const char *fmt, ...); /* pager.c */ extern void setup_pager(void); +extern void wait_for_pager_atexit(void); extern int pager_in_use(void); extern int pager_use_color; extern int term_columns(void); diff --git a/git.c b/git.c index 2c5d6d67118886..01ab10d3685e9f 100644 --- a/git.c +++ b/git.c @@ -782,6 +782,12 @@ int cmd_main(int argc, const char **argv) cmd = slash + 1; } + /* + * wait_for_pager_atexit will close stdout/stderr so it needs to be + * registered first so that it will execute last and not close the + * handles until all other atexit handlers have finished + */ + atexit(wait_for_pager_atexit); trace_command_performance(argv); /* diff --git a/pager.c b/pager.c index a768797fcfcc44..bdc75d0b4fad5b 100644 --- a/pager.c +++ b/pager.c @@ -26,9 +26,12 @@ static void wait_for_pager(int in_signal) finish_command(&pager_process); } -static void wait_for_pager_atexit(void) + +static int run_wait_for_pager_atexit = 0; +void wait_for_pager_atexit(void) { - wait_for_pager(0); + if (run_wait_for_pager_atexit) + wait_for_pager(0); } static void wait_for_pager_signal(int signo) @@ -137,7 +140,7 @@ void setup_pager(void) /* this makes sure that the parent terminates after the pager */ sigchain_push_common(wait_for_pager_signal); - atexit(wait_for_pager_atexit); + run_wait_for_pager_atexit = 1; } int pager_in_use(void)