Re: [PATCH v2 22/28] fsmonitor--daemon: use a cookie file to sync with file system

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Jeff,

I've found an issue with this patch that I unfortunately failed to fix in
time for Git for Windows v2.32.0. It is a subtle issue, and it only reared
its head in the form of flakiness when running Scalar's Functional Tests
(which is the most comprehensive test suite we have in the absence of
proper integration tests for Git).

The issue is the change in behavior between the previous iteration and
this one when replying with the trivial response:

On Sat, 22 May 2021, Jeff Hostetler via GitGitGadget wrote:

> diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
> index e807aa8f6741..985a82cf39e0 100644
> --- a/builtin/fsmonitor--daemon.c
> +++ b/builtin/fsmonitor--daemon.c
> [...]
> @@ -522,6 +672,7 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
>  		 */
>  		do_flush = 1;
>  		do_trivial = 1;
> +		do_cookie = 1;
>
>  	} else if (!skip_prefix(command, "builtin:", &p)) {
>  		/* assume V1 timestamp or garbage */
> @@ -535,6 +686,7 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
>  				  "fsmonitor: unsupported V1 protocol '%s'"),
>  				 command);
>  		do_trivial = 1;
> +		do_cookie = 1;
>
>  	} else {
>  		/* We have "builtin:*" */
> @@ -544,12 +696,14 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
>  					 "fsmonitor: invalid V2 protocol token '%s'",
>  					 command);
>  			do_trivial = 1;
> +			do_cookie = 1;

In the first iteration of this patch series, these three trivial responses
were sent without first writing a cookie file and then waiting for the
event to arrive.

The symptom of the issue here is that some of Scalar's Functional Test
hung, ostensibly waiting for a cookie file that never arrived.

I am not 100% clear on why this only happened in Scalar's Functional Tests
and not in the regression test in Git's test suite, but here are my
thoughts on this:

- I _suspect_ that the .git/ directory gets deleted on the client side
  after receiving a trivial response and finishing the test case, and
  _before_ the daemon can receive the event. As the directory already got
  deleted, the event never arrives.

- I _think_ that running the test cases in parallel (10 concurrent test
  cases, if memory serves) exacerbates this problem.

- Simply _not_ writing that cookie file (i.e. removing those three
  `do_cookie = 1` assignments above is sufficient to let Scalar's
  Functional Tests pass.

- Those trivial responses do not _actually_ need to be guarded behind that
  cookie file. The worst that could happen is for the FSMonitor daemon to
  over-report paths that need to be `lstat()`ed.

Therefore, I would like to suggest this diff to be squashed into this
patch in a re-roll:

-- snip --
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index 985a82cf39e..4afbb36fe61 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -672,7 +672,6 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
 		 */
 		do_flush = 1;
 		do_trivial = 1;
-		do_cookie = 1;

 	} else if (!skip_prefix(command, "builtin:", &p)) {
 		/* assume V1 timestamp or garbage */
@@ -686,7 +685,6 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
 				  "fsmonitor: unsupported V1 protocol '%s'"),
 				 command);
 		do_trivial = 1;
-		do_cookie = 1;

 	} else {
 		/* We have "builtin:*" */
@@ -696,7 +694,6 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
 					 "fsmonitor: invalid V2 protocol token '%s'",
 					 command);
 			do_trivial = 1;
-			do_cookie = 1;

 		} else {
 			/*
-- snap --

For the record, the Windows installer and macOS/Linux packages available
at https://github.com/microsoft/git/releases/tag/v2.32.0.vfs.0.1 do come
with this fix.

Thanks,
Dscho

>
>  		} else {
>  			/*
>  			 * We have a V2 valid token:
>  			 *     "builtin:<token_id>:<seq_nr>"
>  			 */
> +			do_cookie = 1;
>  		}
>  	}
>
> @@ -558,6 +712,30 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
>  	if (!state->current_token_data)
>  		BUG("fsmonitor state does not have a current token");
>
> +	/*
> +	 * Write a cookie file inside the directory being watched in
> +	 * an effort to flush out existing filesystem events that we
> +	 * actually care about.  Suspend this client thread until we
> +	 * see the filesystem events for this cookie file.
> +	 *
> +	 * Creating the cookie lets us guarantee that our FS listener
> +	 * thread has drained the kernel queue and we are caught up
> +	 * with the kernel.
> +	 *
> +	 * If we cannot create the cookie (or otherwise guarantee that
> +	 * we are caught up), we send a trivial response.  We have to
> +	 * assume that there might be some very, very recent activity
> +	 * on the FS still in flight.
> +	 */
> +	if (do_cookie) {
> +		cookie_result = with_lock__wait_for_cookie(state);
> +		if (cookie_result != FCIR_SEEN) {
> +			error(_("fsmonitor: cookie_result '%d' != SEEN"),
> +			      cookie_result);
> +			do_trivial = 1;
> +		}
> +	}
> +
>  	if (do_flush)
>  		with_lock__do_force_resync(state);
>
> @@ -769,7 +947,9 @@ static int handle_client(void *data,
>  	return result;
>  }
>
> -#define FSMONITOR_COOKIE_PREFIX ".fsmonitor-daemon-"
> +#define FSMONITOR_DIR           "fsmonitor--daemon"
> +#define FSMONITOR_COOKIE_DIR    "cookies"
> +#define FSMONITOR_COOKIE_PREFIX (FSMONITOR_DIR "/" FSMONITOR_COOKIE_DIR "/")
>
>  enum fsmonitor_path_type fsmonitor_classify_path_workdir_relative(
>  	const char *rel)
> @@ -922,6 +1102,9 @@ void fsmonitor_publish(struct fsmonitor_daemon_state *state,
>  		}
>  	}
>
> +	if (cookie_names->nr)
> +		with_lock__mark_cookies_seen(state, cookie_names);
> +
>  	pthread_mutex_unlock(&state->main_lock);
>  }
>
> @@ -1011,7 +1194,9 @@ static int fsmonitor_run_daemon(void)
>
>  	memset(&state, 0, sizeof(state));
>
> +	hashmap_init(&state.cookies, cookies_cmp, NULL, 0);
>  	pthread_mutex_init(&state.main_lock, NULL);
> +	pthread_cond_init(&state.cookies_cond, NULL);
>  	state.error_code = 0;
>  	state.current_token_data = fsmonitor_new_token_data();
>
> @@ -1035,6 +1220,23 @@ static int fsmonitor_run_daemon(void)
>  		state.nr_paths_watching = 2;
>  	}
>
> +	/*
> +	 * We will write filesystem syncing cookie files into
> +	 * <gitdir>/<fsmonitor-dir>/<cookie-dir>/<pid>-<seq>.
> +	 */
> +	strbuf_init(&state.path_cookie_prefix, 0);
> +	strbuf_addbuf(&state.path_cookie_prefix, &state.path_gitdir_watch);
> +
> +	strbuf_addch(&state.path_cookie_prefix, '/');
> +	strbuf_addstr(&state.path_cookie_prefix, FSMONITOR_DIR);
> +	mkdir(state.path_cookie_prefix.buf, 0777);
> +
> +	strbuf_addch(&state.path_cookie_prefix, '/');
> +	strbuf_addstr(&state.path_cookie_prefix, FSMONITOR_COOKIE_DIR);
> +	mkdir(state.path_cookie_prefix.buf, 0777);
> +
> +	strbuf_addch(&state.path_cookie_prefix, '/');
> +
>  	/*
>  	 * Confirm that we can create platform-specific resources for the
>  	 * filesystem listener before we bother starting all the threads.
> @@ -1047,6 +1249,7 @@ static int fsmonitor_run_daemon(void)
>  	err = fsmonitor_run_daemon_1(&state);
>
>  done:
> +	pthread_cond_destroy(&state.cookies_cond);
>  	pthread_mutex_destroy(&state.main_lock);
>  	fsmonitor_fs_listen__dtor(&state);
>
> @@ -1054,6 +1257,11 @@ static int fsmonitor_run_daemon(void)
>
>  	strbuf_release(&state.path_worktree_watch);
>  	strbuf_release(&state.path_gitdir_watch);
> +	strbuf_release(&state.path_cookie_prefix);
> +
> +	/*
> +	 * NEEDSWORK: Consider "rm -rf <gitdir>/<fsmonitor-dir>"
> +	 */
>
>  	return err;
>  }
> diff --git a/fsmonitor--daemon.h b/fsmonitor--daemon.h
> index 89a9ef20b24b..e9fc099bae9c 100644
> --- a/fsmonitor--daemon.h
> +++ b/fsmonitor--daemon.h
> @@ -45,6 +45,11 @@ struct fsmonitor_daemon_state {
>
>  	struct fsmonitor_token_data *current_token_data;
>
> +	struct strbuf path_cookie_prefix;
> +	pthread_cond_t cookies_cond;
> +	int cookie_seq;
> +	struct hashmap cookies;
> +
>  	int error_code;
>  	struct fsmonitor_daemon_backend_data *backend_data;
>
> --
> gitgitgadget
>
>




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux