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

Re: [PATCH 4/9] refs: unregister reference stores from "chdir_notify"

From
Karthik Nayak <karthik.188@gmail.com>
Date
Jun 12, 2026, 09:18 UTC
Message-ID
<CAOLa=ZS_0b9o2YucgA6Se_Mq4nLo1Luow7adTLAifbkF9jpUrA@mail.gmail.com>
In-Reply-To
<20260610-b4-pks-refs-avoid-chdir-notify-reparent-v1-4-56c864b01c43@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 17 quoted lines
> When creating reference stores we register them with the "chdir_notify"
> subsystem. This is required because some of the paths we track may be
> relative paths, so we have to reparent them in case the current working
> directory changes.
>
> But while we register the reference stores, we never unregister them.
> This can have multiple outcomes:
>
>   - For a repository's main reference database we essentially keep the
>     pointer alive. We never free that database, either, and our leak
>     checker doesn't notice because it's still registered.
>
>   - For submodule and worktree reference databases we do eventually free
>     them in `repo_clear()`, so we may keep pointers to free'd memory
>     registered. We never notice though as we don't tend to chdir around
>     in the middle of the process.
>

So `ref_store_release()` is what is called to release a ref_store. So in the former's case, we never release the ref_store even if the repository is closed, wow.

Show 8 quoted lines
> We never noticed either of these symptoms, but they are obviously bad.
>
> Partially fix those issues by unregistering the reference stores when
> releasing them. The leak of the main reference database will be fixed in
> a subsequent commit.
>
> Note that this requires us to use `chdir_notify_register()` instead of
> `chdir_notify_parent()`, as there is no infrastructure to unregister the
Shouldn't this be s/chdir_notify_parent/chdir_notify_reparent ?
> latter. It ultimately doesn't matter much though: in a subsequent commit
> we'll drop this infrastructure completely. We merely require this step
> here so that we can fix the memory leaks ahead of time.

Right, we can't unregister when using `chdir_notify_reparent()` because it internally calls `chdir_notify_register()` with a private cb function, and we need to supply the callback function during un-registering. Makes sense.

Show 33 quoted lines
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  refs/files-backend.c    | 22 +++++++++++++++++++---
>  refs/packed-backend.c   | 16 +++++++++++++++-
>  refs/reftable-backend.c | 16 +++++++++++++++-
>  3 files changed, 49 insertions(+), 5 deletions(-)
>
> diff --git a/refs/files-backend.c b/refs/files-backend.c
> index a4c7858787..296981584b 100644
> --- a/refs/files-backend.c
> +++ b/refs/files-backend.c
> @@ -100,6 +100,23 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
>  	}
>  }
>
> +static void files_ref_store_reparent(const char *name UNUSED,
> +				     const char *old_cwd,
> +				     const char *new_cwd,
> +				     void *payload)
> +{
> +	struct files_ref_store *refs = payload;
> +	char *tmp;
> +
> +	tmp = reparent_relative_path(old_cwd, new_cwd, refs->base.gitdir);
> +	free(refs->base.gitdir);
> +	refs->base.gitdir = tmp;
> +
> +	tmp = reparent_relative_path(old_cwd, new_cwd, refs->gitcommondir);
> +	free(refs->gitcommondir);
> +	refs->gitcommondir = tmp;
> +}
> +
Looks similar to `void reparent_cb()` but for both the directories.
Show 107 quoted lines
>  /*
>   * Create a new submodule ref cache and add it to the internal
>   * set of caches.
> @@ -128,9 +145,7 @@ static struct ref_store *files_ref_store_init(struct repository *repo,
>
>  	repo_config_get_bool(repo, "core.prefersymlinkrefs", &refs->prefer_symlink_refs);
>
> -	chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir);
> -	chdir_notify_reparent("files-backend $GIT_COMMONDIR",
> -			      &refs->gitcommondir);
> +	chdir_notify_register(NULL, files_ref_store_reparent, refs);
>
>  	strbuf_release(&refdir);
>
> @@ -182,6 +197,7 @@ static void files_ref_store_release(struct ref_store *ref_store)
>  	free(refs->gitcommondir);
>  	ref_store_release(refs->packed_ref_store);
>  	free(refs->packed_ref_store);
> +	chdir_notify_unregister(NULL, files_ref_store_reparent, refs);
>  }
>
>  static void files_reflog_path(struct files_ref_store *refs,
> diff --git a/refs/packed-backend.c b/refs/packed-backend.c
> index 0acde48c45..499cb55dfa 100644
> --- a/refs/packed-backend.c
> +++ b/refs/packed-backend.c
> @@ -211,6 +211,19 @@ static size_t snapshot_hexsz(const struct snapshot *snapshot)
>  	return snapshot->refs->base.repo->hash_algo->hexsz;
>  }
>
> +static void packed_ref_store_reparent(const char *name UNUSED,
> +				      const char *old_cwd,
> +				      const char *new_cwd,
> +				      void *payload)
> +{
> +	struct packed_ref_store *refs = payload;
> +	char *tmp;
> +
> +	tmp = reparent_relative_path(old_cwd, new_cwd, refs->path);
> +	free(refs->path);
> +	refs->path = tmp;
> +}
> +
>  /*
>   * Since packed-refs is only stored in the common dir, don't parse the
>   * payload and rely on the files-backend to set 'gitdir' correctly.
> @@ -229,7 +242,7 @@ struct ref_store *packed_ref_store_init(struct repository *repo,
>
>  	strbuf_addf(&sb, "%s/packed-refs", gitdir);
>  	refs->path = strbuf_detach(&sb, NULL);
> -	chdir_notify_reparent("packed-refs", &refs->path);
> +	chdir_notify_register(NULL, packed_ref_store_reparent, refs);
>  	return ref_store;
>  }
>
> @@ -274,6 +287,7 @@ static void packed_ref_store_release(struct ref_store *ref_store)
>  	clear_snapshot(refs);
>  	rollback_lock_file(&refs->lock);
>  	delete_tempfile(&refs->tempfile);
> +	chdir_notify_unregister(NULL, packed_ref_store_reparent, refs);
>  	free(refs->path);
>  }
>
> diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
> index 4ae22922de..8c93070677 100644
> --- a/refs/reftable-backend.c
> +++ b/refs/reftable-backend.c
> @@ -365,6 +365,19 @@ static int reftable_be_config(const char *var, const char *value,
>  	return 0;
>  }
>
> +static void reftable_be_reparent(const char *name UNUSED,
> +				 const char *old_cwd,
> +				 const char *new_cwd,
> +				 void *payload)
> +{
> +	struct reftable_ref_store *refs = payload;
> +	char *tmp;
> +
> +	tmp = reparent_relative_path(old_cwd, new_cwd, refs->base.gitdir);
> +	free(refs->base.gitdir);
> +	refs->base.gitdir = tmp;
> +}
> +
>  static struct ref_store *reftable_be_init(struct repository *repo,
>  					  const char *payload,
>  					  const char *gitdir,
> @@ -447,7 +460,7 @@ static struct ref_store *reftable_be_init(struct repository *repo,
>  			goto done;
>  	}
>
> -	chdir_notify_reparent("reftables-backend $GIT_DIR", &refs->base.gitdir);
> +	chdir_notify_register(NULL, reftable_be_reparent, refs);
>
>  done:
>  	assert(refs->err != REFTABLE_API_ERROR);
> @@ -474,6 +487,7 @@ static void reftable_be_release(struct ref_store *ref_store)
>  		free(be);
>  	}
>  	strmap_clear(&refs->worktree_backends, 0);
> +	chdir_notify_unregister(NULL, reftable_be_reparent, refs);
>  }
>
>  static int reftable_be_create_on_disk(struct ref_store *ref_store,
>
> --
> 2.54.0.1189.g8c84645362.dirty
The changes here look good.
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 10 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.