Skip to content

Commit aa6a093

Browse files
committed
Merge branch 'kn/fsmonitor-event-listener-fix' into seen
Under high load, fsmonitor process can hang at start-up, which has been corrected. Comments? * kn/fsmonitor-event-listener-fix: fsmonitor: fix hangs by delayed fs event listening
2 parents 1246c05 + cb8a9c6 commit aa6a093

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

builtin/fsmonitor--daemon.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ static enum fsmonitor_cookie_item_result with_lock__wait_for_cookie(
172172
trace_printf_key(&trace_fsmonitor, "cookie-wait: '%s' '%s'",
173173
cookie->name, cookie_pathname.buf);
174174

175+
while (fsmonitor_get_listen_error_code(state) == 0)
176+
sleep_millisec(50);
177+
175178
/*
176179
* Create the cookie file on disk and then wait for a notification
177180
* that the listener thread has seen it.
@@ -606,6 +609,23 @@ void fsmonitor_force_resync(struct fsmonitor_daemon_state *state)
606609
pthread_mutex_unlock(&state->main_lock);
607610
}
608611

612+
int fsmonitor_get_listen_error_code(struct fsmonitor_daemon_state *state)
613+
{
614+
int error_code;
615+
616+
pthread_mutex_lock(&state->listen_lock);
617+
error_code = state->listen_error_code;
618+
pthread_mutex_unlock(&state->listen_lock);
619+
return error_code;
620+
}
621+
622+
void fsmonitor_set_listen_error_code(struct fsmonitor_daemon_state *state, int error_code)
623+
{
624+
pthread_mutex_lock(&state->listen_lock);
625+
state->listen_error_code = error_code;
626+
pthread_mutex_unlock(&state->listen_lock);
627+
}
628+
609629
/*
610630
* Format an opaque token string to send to the client.
611631
*/
@@ -1285,6 +1305,7 @@ static int fsmonitor_run_daemon(void)
12851305
hashmap_init(&state.cookies, cookies_cmp, NULL, 0);
12861306
pthread_mutex_init(&state.main_lock, NULL);
12871307
pthread_cond_init(&state.cookies_cond, NULL);
1308+
pthread_mutex_init(&state.listen_lock, NULL);
12881309
state.listen_error_code = 0;
12891310
state.health_error_code = 0;
12901311
state.current_token_data = fsmonitor_new_token_data();

compat/fsmonitor/fsm-listen-darwin.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,15 @@ void fsm_listen__loop(struct fsmonitor_daemon_state *state)
515515
goto force_error_stop_without_loop;
516516
}
517517
data->stream_started = 1;
518+
fsmonitor_set_listen_error_code(state, 1);
518519

519520
pthread_mutex_lock(&data->dq_lock);
520521
pthread_cond_wait(&data->dq_finished, &data->dq_lock);
521522
pthread_mutex_unlock(&data->dq_lock);
522523

523524
switch (data->shutdown_style) {
524525
case FORCE_ERROR_STOP:
525-
state->listen_error_code = -1;
526+
fsmonitor_set_listen_error_code(state, -1);
526527
/* fall thru */
527528
case FORCE_SHUTDOWN:
528529
ipc_server_stop_async(state->ipc_server_data);
@@ -534,7 +535,7 @@ void fsm_listen__loop(struct fsmonitor_daemon_state *state)
534535
return;
535536

536537
force_error_stop_without_loop:
537-
state->listen_error_code = -1;
538+
fsmonitor_set_listen_error_code(state, -1);
538539
ipc_server_stop_async(state->ipc_server_data);
539540
return;
540541
}

compat/fsmonitor/fsm-listen-win32.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,14 +732,13 @@ void fsm_listen__loop(struct fsmonitor_daemon_state *state)
732732
DWORD dwWait;
733733
int result;
734734

735-
state->listen_error_code = 0;
736-
737735
if (start_rdcw_watch(data->watch_worktree) == -1)
738736
goto force_error_stop;
739737

740738
if (data->watch_gitdir &&
741739
start_rdcw_watch(data->watch_gitdir) == -1)
742740
goto force_error_stop;
741+
fsmonitor_set_listen_error_code(state, 1);
743742

744743
for (;;) {
745744
dwWait = WaitForMultipleObjects(data->nr_listener_handles,
@@ -797,7 +796,7 @@ void fsm_listen__loop(struct fsmonitor_daemon_state *state)
797796
}
798797

799798
force_error_stop:
800-
state->listen_error_code = -1;
799+
fsmonitor_set_listen_error_code(state, -1);
801800

802801
force_shutdown:
803802
/*

fsmonitor--daemon.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct fsmonitor_daemon_state {
5151
int cookie_seq;
5252
struct hashmap cookies;
5353

54+
pthread_mutex_t listen_lock;
5455
int listen_error_code;
5556
int health_error_code;
5657
struct fsm_listen_data *listen_data;
@@ -167,5 +168,8 @@ void fsmonitor_publish(struct fsmonitor_daemon_state *state,
167168
*/
168169
void fsmonitor_force_resync(struct fsmonitor_daemon_state *state);
169170

171+
int fsmonitor_get_listen_error_code(struct fsmonitor_daemon_state *state);
172+
void fsmonitor_set_listen_error_code(struct fsmonitor_daemon_state *state, int error_code);
173+
170174
#endif /* HAVE_FSMONITOR_DAEMON_BACKEND */
171175
#endif /* FSMONITOR_DAEMON_H */

0 commit comments

Comments
 (0)