Skip to content

Commit

Permalink
WIP: Try to really force the fsmonitor--daemon to quit
Browse files Browse the repository at this point in the history
  • Loading branch information
dscho authored and Kevin Willford committed Feb 21, 2020
1 parent 7142a23 commit e44a165
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions builtin/fsmonitor--daemon.c
Expand Up @@ -67,6 +67,9 @@ static int handle_client(struct ipc_command_listener *data,
trace2_data_string("fsmonitor", the_repository, "command", command);

if (!strcmp(command, "quit")) {
if (fsmonitor_listen_stop(state))
error("Could not terminate watcher thread");
sleep_millisec(50);
return SIMPLE_IPC_QUIT;
}

Expand Down Expand Up @@ -165,7 +168,6 @@ int fsmonitor_queue_path(struct fsmonitor_daemon_state *state,

static int fsmonitor_run_daemon(int background)
{
pthread_t thread;
struct fsmonitor_daemon_state state = { { 0 } };
struct ipc_data ipc_data = {
.data = {
Expand All @@ -183,7 +185,7 @@ static int fsmonitor_run_daemon(int background)
pthread_mutex_init(&state.initial_mutex, NULL);
pthread_mutex_lock(&state.initial_mutex);

if (pthread_create(&thread, NULL,
if (pthread_create(&state.watcher_thread, NULL,
(void *(*)(void *)) fsmonitor_listen, &state) < 0)
return error(_("could not start fsmonitor listener thread"));

Expand Down
8 changes: 8 additions & 0 deletions compat/fsmonitor/win32.c
Expand Up @@ -30,6 +30,14 @@ static int process_entry(struct fsmonitor_daemon_state *state,
return 0;
}

int fsmonitor_listen_stop(struct fsmonitor_daemon_state *state)
{
if (!TerminateThread(state->watcher_thread.handle, 1))
return -1;

return 0;
}

struct fsmonitor_daemon_state *fsmonitor_listen(struct fsmonitor_daemon_state *state)
{
HANDLE dir;
Expand Down
2 changes: 2 additions & 0 deletions fsmonitor.h
Expand Up @@ -111,6 +111,7 @@ struct fsmonitor_daemon_state {
struct fsmonitor_queue_item *first;
struct fsmonitor_queue_item *last;
uint64_t latest_update;
pthread_t watcher_thread;
pthread_mutex_t queue_update_lock, initial_mutex;
int error_code;
};
Expand All @@ -124,6 +125,7 @@ int fsmonitor_queue_path(struct fsmonitor_daemon_state *state,

/* This needs to be implemented by the backend */
struct fsmonitor_daemon_state *fsmonitor_listen(struct fsmonitor_daemon_state *state);
int fsmonitor_listen_stop(struct fsmonitor_daemon_state *state);
#endif

#endif
7 changes: 7 additions & 0 deletions t/t7527-internal-fsmonitor.sh
Expand Up @@ -9,6 +9,13 @@ git fsmonitor--daemon --is-supported || {
test_done
}

# Tell the fsmonitor--daemon to stop, even if `--stop` failed
test_atexit '
git fsmonitor--daemon --stop ||
test -n "$debug" ||
rm -r .git
'

test_expect_success 'can start and stop the daemon' '
test_when_finished \
"test_might_fail git -C test fsmonitor--daemon --stop" &&
Expand Down

0 comments on commit e44a165

Please sign in to comment.