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

Re: [PATCH v2 6/6] reftable/stack: handle outdated stacks when compacting

From
Justin Tobler <jltobler@gmail.com>
Date
Aug 11, 2025, 20:04 UTC
Message-ID
<3vkwdsqcas22um2djaic43ntapedwz4wqnrrnhvhngqdul2vmj@2rwfqngzyuup>
In-Reply-To
<20250804-pks-reftable-fixes-for-libgit2-v2-6-fef06209a984@pks.im>
On 25/08/04 11:40AM, Patrick Steinhardt wrote:
Show 5 quoted lines
> When we compact the reftable stack we first acquire the lock for the
> "tables.list" file and then reload the stack to check that it is still
> up-to-date. This is done by calling `stack_uptodate()`, which knows to
> return zero in case the stack is up-to-date, a positive value if it is
> not and a negative error code on unexpected conditions.

So `stack_uptodate()` returns a negative value for error cases and a positive value if the stack is out of date. `REFTABLE_OUTDATED_ERROR` is really also an error, but it is special cased to differentiate it from the others.

Show 75 quoted lines
> We don't do proper error checking though, but instead we only check
> whether the returned error code is non-zero. If so, we simply bubble it
> up the calling stack, which means that callers may see an unexpected
> positive value.
> 
> Fix this issue by translating to `REFTABLE_OUTDATED_ERROR` instead.
> Handle this situation in `reftable_addition_commit()`, where we perform
> a best-effort auto-compaction.
> 
> All other callsites of `stack_uptodate()` know to handle a positive
> return value and thus don't need to be fixed.
> 
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  reftable/stack.c | 32 ++++++++++++++++++++++++++------
>  1 file changed, 26 insertions(+), 6 deletions(-)
> 
> diff --git a/reftable/stack.c b/reftable/stack.c
> index f77d7f58e8..effa2fc8cb 100644
> --- a/reftable/stack.c
> +++ b/reftable/stack.c
> @@ -579,9 +579,11 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir,
>  	return err;
>  }
>  
> -/* -1 = error
> - 0 = up to date
> - 1 = changed. */
> +/*
> + * Check whether the given stack is up-to-date with what we have in memory.
> + * Returns 0 if so, 1 if the stack is out-of-date or a negative error code
> + * otherwise.
> + */
>  static int stack_uptodate(struct reftable_stack *st)
>  {
>  	char **names = NULL;
> @@ -849,10 +851,13 @@ int reftable_addition_commit(struct reftable_addition *add)
>  		 * control. It is possible that a concurrent writer is already
>  		 * trying to compact parts of the stack, which would lead to a
>  		 * `REFTABLE_LOCK_ERROR` because parts of the stack are locked
> -		 * already. This is a benign error though, so we ignore it.
> +		 * already. Similarly, the stack may have been rewritten by a
> +		 * concurrent writer, which causes `REFTABLE_OUTDATED_ERROR`.
> +		 * Both of these errors are benign, so we simply ignore them.
>  		 */
>  		err = reftable_stack_auto_compact(add->stack);
> -		if (err < 0 && err != REFTABLE_LOCK_ERROR)
> +		if (err < 0 && err != REFTABLE_LOCK_ERROR &&
> +		    err != REFTABLE_OUTDATED_ERROR)
>  			goto done;
>  		err = 0;
>  	}
> @@ -1215,9 +1220,24 @@ static int stack_compact_range(struct reftable_stack *st,
>  		goto done;
>  	}
>  
> +	/*
> +	 * Check whether the stack is up-to-date. We unfortunately cannot
> +	 * handle the situation gracefully in case it's _not_ up-to-date
> +	 * because the range of tables that the user has requested us to
> +	 * compact may have been changed. So instead we abort.
> +	 *
> +	 * We could in theory improve the situation by having the caller not
> +	 * pass in a range, but instead the list of tables to compact. If so,
> +	 * we could check that relevant tables still exist. But for now it's
> +	 * good enough to just abort.
> +	 */
>  	err = stack_uptodate(st);
> -	if (err)
> +	if (err < 0)
>  		goto done;
> +	if (err > 0) {
> +		err = REFTABLE_OUTDATED_ERROR;
> +		goto done;
> +	}

I was thinking that maybe `stack_uptodate()` could maybe handle returning the `REFTABLE_OUTDATED_ERROR` directly so we could avoid having to map the error here. This could require callers to check for `err == REFTABLE_OUTDATED_ERROR` instead of `err > 0`. Probably not a big deal either way though.

Otherwise this looks good to me :)
-Justin
Show 8 quoted lines
>  
>  	/*
>  	 * Lock all tables in the user-provided range. This is the slice of our
> 
> -- 
> 2.50.1.723.g3e08bea96f.dirty
> 
> 
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 24 of 52 in “reftable: a couple of improvements for libgit2”
  1. 0/5 reftable: a couple of improvements for libgit2Patrick Steinhardt, Aug 1, 2025
  2. 1/5 reftable/writer: fix type used for number of recordsPatrick Steinhardt, Aug 1, 2025
  3. 2/5 reftable/writer: drop Git-specific `QSORT()` macroPatrick Steinhardt, Aug 1, 2025
  4. 3/5 reftable/stack: fix compiler warning due to missing bracesPatrick Steinhardt, Aug 1, 2025
  5. Eric SunshineAug 1, 2025
  6. Patrick SteinhardtAug 4, 2025
  7. Junio C HamanoAug 4, 2025
  8. Patrick SteinhardtAug 5, 2025
  9. Carlo ArenasAug 12, 2025
  10. Patrick SteinhardtAug 12, 2025
  11. Junio C HamanoAug 12, 2025
  12. 4/5 reftable/stack: reorder code to avoid forward declarationsPatrick Steinhardt, Aug 1, 2025
  13. 5/5 reftable/stack: allow passing flags to `reftable_stack_add()`Patrick Steinhardt, Aug 1, 2025
  14. 0/6 reftable: a couple of improvements for libgit2Patrick Steinhardt, Aug 4, 2025
  15. 1/6 reftable/writer: fix type used for number of recordsPatrick Steinhardt, Aug 4, 2025
  16. 2/6 reftable/writer: drop Git-specific `QSORT()` macroPatrick Steinhardt, Aug 4, 2025
  17. 3/6 reftable/stack: fix compiler warning due to missing bracesPatrick Steinhardt, Aug 4, 2025
  18. 4/6 reftable/stack: reorder code to avoid forward declarationsPatrick Steinhardt, Aug 4, 2025
  19. Justin ToblerAug 11, 2025
  20. 5/6 reftable/stack: allow passing flags to `reftable_stack_add()`Patrick Steinhardt, Aug 4, 2025
  21. Justin ToblerAug 11, 2025
  22. Patrick SteinhardtAug 12, 2025
  23. 6/6 reftable/stack: handle outdated stacks when compactingPatrick Steinhardt, Aug 4, 2025
  24. Justin ToblerAug 11, 2025
  25. Patrick SteinhardtAug 12, 2025
  26. 0/8 reftable: a couple of improvements for libgit2Patrick Steinhardt, Aug 12, 2025
  27. 1/8 reftable/writer: fix type used for number of recordsPatrick Steinhardt, Aug 12, 2025
  28. 2/8 reftable/writer: drop Git-specific `QSORT()` macroPatrick Steinhardt, Aug 12, 2025
  29. 3/8 reftable/stack: reorder code to avoid forward declarationsPatrick Steinhardt, Aug 12, 2025
  30. 4/8 reftable/stack: fix compiler warning due to missing bracesPatrick Steinhardt, Aug 12, 2025
  31. Justin ToblerAug 12, 2025
  32. 5/8 reftable/stack: allow passing flags to `reftable_stack_add()`Patrick Steinhardt, Aug 12, 2025
  33. Justin ToblerAug 12, 2025
  34. Patrick SteinhardtAug 13, 2025
  35. 6/8 reftable/stack: handle outdated stacks when compactingPatrick Steinhardt, Aug 12, 2025
  36. 7/8 reftable: don't second-guess errors from flock interfacePatrick Steinhardt, Aug 12, 2025
  37. Justin ToblerAug 12, 2025
  38. 8/8 refs/reftable: always reload stacks when creating lockPatrick Steinhardt, Aug 12, 2025
  39. Justin ToblerAug 12, 2025
  40. Carlo ArenasAug 12, 2025
  41. Patrick SteinhardtAug 13, 2025
  42. Junio C HamanoAug 13, 2025
  43. 0/8 reftable: a couple of improvements for libgit2Patrick Steinhardt, Aug 13, 2025
  44. 1/8 reftable/writer: fix type used for number of recordsPatrick Steinhardt, Aug 13, 2025
  45. 2/8 reftable/writer: drop Git-specific `QSORT()` macroPatrick Steinhardt, Aug 13, 2025
  46. 3/8 reftable/stack: reorder code to avoid forward declarationsPatrick Steinhardt, Aug 13, 2025
  47. 4/8 reftable/stack: fix compiler warning due to missing bracesPatrick Steinhardt, Aug 13, 2025
  48. 5/8 reftable/stack: allow passing flags to `reftable_stack_add()`Patrick Steinhardt, Aug 13, 2025
  49. 6/8 reftable/stack: handle outdated stacks when compactingPatrick Steinhardt, Aug 13, 2025
  50. 7/8 reftable: don't second-guess errors from flock interfacePatrick Steinhardt, Aug 13, 2025
  51. 8/8 refs/reftable: always reload stacks when creating lockPatrick Steinhardt, Aug 13, 2025
  52. Justin ToblerAug 13, 2025

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.