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

[PATCH v4 09/10] refs: fix recursing `get_main_ref_store()` with "onbranch" config

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 19, 2026, 11:27 UTC
Message-ID
<20260619-b4-pks-refs-avoid-chdir-notify-reparent-v4-9-a6472be7acc4@pks.im>
In-Reply-To
<20260619-b4-pks-refs-avoid-chdir-notify-reparent-v4-0-a6472be7acc4@pks.im>

When we have an "onbranch" condition we need to ask the reference database whether HEAD currently points at the configured branch. This unfortunately creates a chicken-and-egg problem:

  - The reference database needs to read the configuration so that it
    can configure itself.
  - The configuration needs to construct a reference database to fully
    parse all of its conditionals.

The way we handle this is by simply excluding "onbranch" conditionals when we haven't yet configured the reference database.

The mechanism for this is broken though: to verify whether or not we have configured the reference database we check whether its format is set to `REF_STORAGE_UNKNOWN` in `include_by_branch()`. But typically, the format _is_ already known at that time because we set it up during repository discovery in "setup.c".

The consequence is that we recurse:
  1. We call `get_main_ref_store()`.
  2. We don't yet have a reference store, so we call `ref_store_init()`.
  3. We parse the configuration required for the reference store.
  4. We eventually end up in `include_by_branch()`.
  5. We have already configured the reference storage format, so we end
     up calling `get_main_ref_store()` again.

We still haven't finished (1) though, so `get_main_ref_store()` will now call `ref_store_init()` a second time. The end result is that we have constructed the same reference store twice.

Of course, as both reference stores would be assigned to `refs_private`, we leak one of those two instances. This never surfaced as an actual leak though because the pointer is kept alive by the "chdir_notify" subsystem.

The mechanism to use the configured reference format is quite fragile in the first place. Introduce a new mechanism that allows us to explicitly skip evaluation of "onbranch" conditions and use it to fix the issue. Add a sanity check in `get_main_ref_store()` to make sure we aren't recursing, which would have failed before the fix.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 config.c                | 4 +++-
 config.h                | 1 +
 refs.c                  | 7 +++++++
 refs/files-backend.c    | 8 +++++++-
 refs/reftable-backend.c | 8 +++++++-
 5 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/config.c b/config.c
index a1b92fe083..223c252236 100644
--- a/config.c
+++ b/config.c
@@ -302,7 +302,9 @@ static int include_by_branch(struct config_include_data *data,
 	struct strbuf pattern = STRBUF_INIT;
 	const char *refname, *shortname;
 
-	if (!data->repo || data->repo->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
+	if (!data->repo ||
+	    data->opts->ignore_refs ||
+	    data->repo->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
 		return 0;
 
 	refname = refs_resolve_ref_unsafe(get_main_ref_store(data->repo),
diff --git a/config.h b/config.h
index bf47fb3afc..42aedde878 100644
--- a/config.h
+++ b/config.h
@@ -88,6 +88,7 @@ typedef int (*config_parser_event_fn_t)(enum config_event_t type,
 struct config_options {
 	unsigned int respect_includes : 1;
 	unsigned int ignore_repo : 1;
+	unsigned int ignore_refs : 1;
 	unsigned int ignore_worktree : 1;
 	unsigned int ignore_cmdline : 1;
 	unsigned int system_gently : 1;
diff --git a/refs.c b/refs.c
index 5b773b1c15..f242e6ca96 100644
--- a/refs.c
+++ b/refs.c
@@ -2359,15 +2359,22 @@ void ref_store_release(struct ref_store *ref_store)
 
 struct ref_store *get_main_ref_store(struct repository *r)
 {
+	static bool initializing;
+
 	if (r->refs_private)
 		return r->refs_private;
 
 	if (!r->gitdir)
 		BUG("attempting to get main_ref_store outside of repository");
+	if (initializing)
+		BUG("main reference store creation is recursing");
 
+	initializing = true;
 	r->refs_private = ref_store_init(r, r->ref_storage_format,
 					 r->gitdir, REF_STORE_ALL_CAPS);
 	r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private);
+	initializing = false;
+
 	return r->refs_private;
 }
 
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 79fb6735e1..ce29875cdd 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -141,6 +141,12 @@ static struct ref_store *files_ref_store_init(struct repository *repo,
 					      const char *gitdir,
 					      const struct ref_store_init_options *opts)
 {
+	struct config_options config_opts = {
+		.respect_includes = 1,
+		.ignore_refs = 1,
+		.commondir = repo->commondir,
+		.git_dir = repo->gitdir,
+	};
 	struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
 	struct ref_store *ref_store = (struct ref_store *)refs;
 	struct strbuf ref_common_dir = STRBUF_INIT;
@@ -158,7 +164,7 @@ static struct ref_store *files_ref_store_init(struct repository *repo,
 	refs->store_flags = opts->access_flags;
 	refs->log_all_ref_updates = LOG_REFS_UNSET;
 
-	repo_config(repo, files_ref_store_config, refs);
+	config_with_options(files_ref_store_config, refs, NULL, repo, &config_opts);
 	chdir_notify_register(NULL, files_ref_store_reparent, refs);
 
 	strbuf_release(&refdir);
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index ee92bd9c70..05d4edc6fd 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -390,6 +390,12 @@ static struct ref_store *reftable_be_init(struct repository *repo,
 					  const char *gitdir,
 					  const struct ref_store_init_options *opts)
 {
+	struct config_options config_opts = {
+		.respect_includes = 1,
+		.ignore_refs = 1,
+		.commondir = repo->commondir,
+		.git_dir = repo->gitdir,
+	};
 	struct reftable_ref_store *refs = xcalloc(1, sizeof(*refs));
 	struct strbuf ref_common_dir = STRBUF_INIT;
 	struct strbuf refdir = STRBUF_INIT;
@@ -424,7 +430,7 @@ static struct ref_store *reftable_be_init(struct repository *repo,
 	refs->write_options.lock_timeout_ms = 100;
 	refs->log_all_ref_updates = LOG_REFS_UNSET;
 
-	repo_config(repo, reftable_be_config, refs);
+	config_with_options(reftable_be_config, refs, NULL, repo, &config_opts);
 
 	/*
 	 * It is somewhat unfortunate that we have to mirror the default block
-- 
2.55.0.rc1.722.g2b3ac350e6.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 67 of 88 in “refs: stop using `chdir_notify_reparent()`”
  1. 0/9 refs: stop using `chdir_notify_reparent()`Patrick Steinhardt, Jun 10, 2026
  2. 1/9 setup: inline `check_and_apply_repository_format()`Patrick Steinhardt, Jun 10, 2026
  3. 2/9 setup: stop applying repository format twicePatrick Steinhardt, Jun 10, 2026
  4. Karthik NayakJun 12, 2026
  5. Patrick SteinhardtJun 15, 2026
  6. 3/9 setup: don't apply "GIT_REFERENCE_BACKEND" without a repositoryPatrick Steinhardt, Jun 10, 2026
  7. Junio C HamanoJun 10, 2026
  8. Patrick SteinhardtJun 12, 2026
  9. 4/9 refs: unregister reference stores from "chdir_notify"Patrick Steinhardt, Jun 10, 2026
  10. Karthik NayakJun 12, 2026
  11. Patrick SteinhardtJun 15, 2026
  12. 5/9 chdir-notify: drop unused `chdir_notify_reparent()`Patrick Steinhardt, Jun 10, 2026
  13. 6/9 repository: free main reference databasePatrick Steinhardt, Jun 10, 2026
  14. Karthik NayakJun 12, 2026
  15. 7/9 refs: fix recursing `get_main_ref_store()` with "onbranch" configPatrick Steinhardt, Jun 10, 2026
  16. 8/9 refs: drop local buffer in `refs_compute_filesystem_location()`Patrick Steinhardt, Jun 10, 2026
  17. 9/9 refs: always use absolute paths for reference storesPatrick Steinhardt, Jun 10, 2026
  18. Karthik NayakJun 12, 2026
  19. Patrick SteinhardtJun 15, 2026
  20. Jeff KingJun 11, 2026
  21. Patrick SteinhardtJun 12, 2026
  22. Jeff KingJun 13, 2026
  23. Patrick SteinhardtJun 15, 2026
  24. 0/8 refs: stop using `chdir_notify_reparent()`Patrick Steinhardt, Jun 15, 2026
  25. 1/8 setup: inline `check_and_apply_repository_format()`Patrick Steinhardt, Jun 15, 2026
  26. 2/8 setup: stop applying repository format twicePatrick Steinhardt, Jun 15, 2026
  27. Justin ToblerJun 17, 2026
  28. 3/8 setup: don't apply "GIT_REFERENCE_BACKEND" without a repositoryPatrick Steinhardt, Jun 15, 2026
  29. Justin ToblerJun 17, 2026
  30. Patrick SteinhardtJun 18, 2026
  31. 4/8 refs: unregister reference stores from "chdir_notify"Patrick Steinhardt, Jun 15, 2026
  32. Justin ToblerJun 17, 2026
  33. Justin ToblerJun 17, 2026
  34. Patrick SteinhardtJun 18, 2026
  35. 5/8 chdir-notify: drop unused `chdir_notify_reparent()`Patrick Steinhardt, Jun 15, 2026
  36. 6/8 repository: free main reference databasePatrick Steinhardt, Jun 15, 2026
  37. Justin ToblerJun 17, 2026
  38. 7/8 refs: fix recursing `get_main_ref_store()` with "onbranch" configPatrick Steinhardt, Jun 15, 2026
  39. Justin ToblerJun 17, 2026
  40. Patrick SteinhardtJun 18, 2026
  41. Justin ToblerJun 18, 2026
  42. Patrick SteinhardtJun 18, 2026
  43. Justin ToblerJun 18, 2026
  44. Jeff KingJun 18, 2026
  45. Patrick SteinhardtJun 19, 2026
  46. Jeff KingJun 21, 2026
  47. Patrick SteinhardtJun 22, 2026
  48. 8/8 refs: drop local buffer in `refs_compute_filesystem_location()`Patrick Steinhardt, Jun 15, 2026
  49. 0/8 refs: stop using `chdir_notify_reparent()`Patrick Steinhardt, Jun 18, 2026
  50. 1/8 setup: inline `check_and_apply_repository_format()`Patrick Steinhardt, Jun 18, 2026
  51. 2/8 setup: stop applying repository format twicePatrick Steinhardt, Jun 18, 2026
  52. 3/8 setup: don't apply "GIT_REFERENCE_BACKEND" without a repositoryPatrick Steinhardt, Jun 18, 2026
  53. 4/8 refs: unregister reference stores from "chdir_notify"Patrick Steinhardt, Jun 18, 2026
  54. 5/8 chdir-notify: drop unused `chdir_notify_reparent()`Patrick Steinhardt, Jun 18, 2026
  55. 6/8 repository: free main reference databasePatrick Steinhardt, Jun 18, 2026
  56. 7/8 refs: fix recursing `get_main_ref_store()` with "onbranch" configPatrick Steinhardt, Jun 18, 2026
  57. 8/8 refs: drop local buffer in `refs_compute_filesystem_location()`Patrick Steinhardt, Jun 18, 2026
  58. 00/10 refs: stop using `chdir_notify_reparent()`Patrick Steinhardt, Jun 19, 2026
  59. 01/10 setup: inline `check_and_apply_repository_format()`Patrick Steinhardt, Jun 19, 2026
  60. 02/10 setup: stop applying repository format twicePatrick Steinhardt, Jun 19, 2026
  61. 03/10 setup: don't apply "GIT_REFERENCE_BACKEND" without a repositoryPatrick Steinhardt, Jun 19, 2026
  62. 04/10 refs: unregister reference stores from "chdir_notify"Patrick Steinhardt, Jun 19, 2026
  63. 05/10 chdir-notify: drop unused `chdir_notify_reparent()`Patrick Steinhardt, Jun 19, 2026
  64. 06/10 repository: free main reference databasePatrick Steinhardt, Jun 19, 2026
  65. 07/10 refs: move parsing of "core.logAllRefUpdates" back into ref storesPatrick Steinhardt, Jun 19, 2026
  66. 08/10 refs/reftable-backend: manually parse "core.sharedRepository"Patrick Steinhardt, Jun 19, 2026
  67. 09/10 refs: fix recursing `get_main_ref_store()` with "onbranch" configPatrick Steinhardt, Jun 19, 2026
  68. 10/10 refs: drop local buffer in `refs_compute_filesystem_location()`Patrick Steinhardt, Jun 19, 2026
  69. 00/11 refs: fix "onbranch" conditionsPatrick Steinhardt, Jun 22, 2026
  70. 01/11 setup: inline `check_and_apply_repository_format()`Patrick Steinhardt, Jun 22, 2026
  71. 02/11 setup: stop applying repository format twicePatrick Steinhardt, Jun 22, 2026
  72. 03/11 setup: don't apply "GIT_REFERENCE_BACKEND" without a repositoryPatrick Steinhardt, Jun 22, 2026
  73. 04/11 refs: unregister reference stores from "chdir_notify"Patrick Steinhardt, Jun 22, 2026
  74. 05/11 chdir-notify: drop unused `chdir_notify_reparent()`Patrick Steinhardt, Jun 22, 2026
  75. 06/11 repository: free main reference databasePatrick Steinhardt, Jun 22, 2026
  76. 07/11 refs: move parsing of "core.logAllRefUpdates" back into ref storesPatrick Steinhardt, Jun 22, 2026
  77. Justin ToblerJun 24, 2026
  78. Patrick SteinhardtJun 25, 2026
  79. 08/11 refs/files: lazy-load configuration to fix chicken-and-eggPatrick Steinhardt, Jun 22, 2026
  80. Justin ToblerJun 24, 2026
  81. Patrick SteinhardtJun 25, 2026
  82. 09/11 reftable: split up write optionsPatrick Steinhardt, Jun 22, 2026
  83. Justin ToblerJun 24, 2026
  84. Patrick SteinhardtJun 25, 2026
  85. 10/11 refs/reftable: lazy-load configuration to fix chicken-and-eggPatrick Steinhardt, Jun 22, 2026
  86. Justin ToblerJun 24, 2026
  87. Patrick SteinhardtJun 25, 2026
  88. 11/11 refs: protect against chicken-and-egg recursionPatrick Steinhardt, Jun 22, 2026

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.