git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 11/11] fsmonitor: refactor initialization of fsmonitor_last_update token

From
JGJeff Hostetler via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 3, 2021, 15:34 UTC
Message-ID
<5410d3ab61d1c9bb2a4d737bca8a81625e333da0.1612366490.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.860.v2.git.1612366490.gitgitgadget@gmail.com>
From: Jeff Hostetler <jeffhost@microsoft.com>

Isolate and document initialization of `istate->fsmonitor_last_update`. This field should contain a fsmonitor-specific opaque token, but we need to initialize it before we can actually talk to a fsmonitor process, so we create a generic default value.

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
---
 fsmonitor.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
diff --git a/fsmonitor.c b/fsmonitor.c
index 64deeda597e..e12214b3007 100644
--- a/fsmonitor.c
+++ b/fsmonitor.c
@@ -343,16 +343,45 @@ void refresh_fsmonitor(struct index_state *istate)
 	istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
 }
 
+/*
+ * The caller wants to turn on FSMonitor.  And when the caller writes
+ * the index to disk, a FSMonitor extension should be included.  This
+ * requires that `istate->fsmonitor_last_update` not be NULL.  But we
+ * have not actually talked to a FSMonitor process yet, so we don't
+ * have an initial value for this field.
+ *
+ * For a protocol V1 FSMonitor process, this field is a formatted
+ * "nanoseconds since epoch" field.  However, for a protocol V2
+ * FSMonitor process, this field is an opaque token.
+ *
+ * Historically, `add_fsmonitor()` has initialized this field to the
+ * current time for protocol V1 processes.  There are lots of race
+ * conditions here, but that code has shipped...
+ *
+ * The only true solution is to use a V2 FSMonitor and get a current
+ * or default token value (that it understands), but we cannot do that
+ * until we have actually talked to an instance of the FSMonitor process
+ * (but the protocol requires that we send a token first...).
+ *
+ * For simplicity, just initialize like we have a V1 process and require
+ * that V2 processes adapt.
+ */
+static void initialize_fsmonitor_last_update(struct index_state *istate)
+{
+	struct strbuf last_update = STRBUF_INIT;
+
+	strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
+	istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
+}
+
 void add_fsmonitor(struct index_state *istate)
 {
 	unsigned int i;
-	struct strbuf last_update = STRBUF_INIT;
 
 	if (!istate->fsmonitor_last_update) {
 		trace_printf_key(&trace_fsmonitor, "add fsmonitor");
 		istate->cache_changed |= FSMONITOR_CHANGED;
-		strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
-		istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
+		initialize_fsmonitor_last_update(istate);
 
 		/* reset the fsmonitor state */
 		for (i = 0; i < istate->cache_nr; i++)
-- 
gitgitgadget
Previous: Jeff Hostetler via GitGitGadgetNext: Jeff Hostetler via GitGitGadget
Message 23 of 30 in “FSMonitor Preliminary Commits”
  1. 00/11 FSMonitor Preliminary CommitsJeff Hostetler via GitGitGadget, Feb 1, 2021
  2. 01/11 p7519: use xargs -0 rather than -d in testJeff Hostetler via GitGitGadget, Feb 1, 2021
  3. Junio C HamanoFeb 1, 2021
  4. Jeff HostetlerFeb 2, 2021
  5. Junio C HamanoFeb 2, 2021
  6. 02/11 p7519: fix watchman watch-list test on WindowsJeff Hostetler via GitGitGadget, Feb 1, 2021
  7. 09/11 fsmonitor: log FSMN token when reading and writing the indexJeff Hostetler via GitGitGadget, Feb 1, 2021
  8. 11/11 fsmonitor: refactor initialization of fsmonitor_last_update tokenJeff Hostetler via GitGitGadget, Feb 1, 2021
  9. 03/11 p7519: move watchman cleanup earlier in the testJeff Hostetler via GitGitGadget, Feb 1, 2021
  10. 04/11 p7519: add trace logging during perf testJeff Hostetler via GitGitGadget, Feb 1, 2021
  11. 05/11 preload-index: log the number of lstat calls to trace2Jeff Hostetler via GitGitGadget, Feb 1, 2021
  12. 07/11 read-cache: log the number of scanned files to trace2Jeff Hostetler via GitGitGadget, Feb 1, 2021
  13. 10/11 fsmonitor: allow all entries for a folder to be invalidatedKevin Willford via GitGitGadget, Feb 1, 2021
  14. 08/11 fsmonitor: log invocation of FSMonitor hook to trace2Jeff Hostetler via GitGitGadget, Feb 1, 2021
  15. 06/11 read-cache: log the number of lstat calls to trace2Jeff Hostetler via GitGitGadget, Feb 1, 2021
  16. 00/11 FSMonitor Preliminary CommitsJeff Hostetler via GitGitGadget, Feb 3, 2021
  17. 03/11 p7519: move watchman cleanup earlier in the testJeff Hostetler via GitGitGadget, Feb 3, 2021
  18. 04/11 p7519: add trace logging during perf testJeff Hostetler via GitGitGadget, Feb 3, 2021
  19. 01/11 p7519: do not rely on "xargs -d" in testJeff Hostetler via GitGitGadget, Feb 3, 2021
  20. 02/11 p7519: fix watchman watch-list test on WindowsJeff Hostetler via GitGitGadget, Feb 3, 2021
  21. 06/11 read-cache: log the number of lstat calls to trace2Jeff Hostetler via GitGitGadget, Feb 3, 2021
  22. 07/11 read-cache: log the number of scanned files to trace2Jeff Hostetler via GitGitGadget, Feb 3, 2021
  23. 11/11 fsmonitor: refactor initialization of fsmonitor_last_update tokenJeff Hostetler via GitGitGadget, Feb 3, 2021
  24. 09/11 fsmonitor: log FSMN token when reading and writing the indexJeff Hostetler via GitGitGadget, Feb 3, 2021
  25. 10/11 fsmonitor: allow all entries for a folder to be invalidatedKevin Willford via GitGitGadget, Feb 3, 2021
  26. 08/11 fsmonitor: log invocation of FSMonitor hook to trace2Jeff Hostetler via GitGitGadget, Feb 3, 2021
  27. 05/11 preload-index: log the number of lstat calls to trace2Jeff Hostetler via GitGitGadget, Feb 3, 2021
  28. Jeff HostetlerFeb 16, 2021
  29. Junio C HamanoFeb 17, 2021
  30. Taylor BlauFeb 3, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.