Fix libgit2 ll_find_delta leaking worker threads#568
Merged
Conversation
If `git_thread_create` failed in `ll_find_deltas` (e.g. with EAGAIN due to transient resource exhaustion), libgit2 returned an error while the already-created delta-search worker threads were still running. `GitRepoImpl::flush()` would then throw and free the packbuilder, causing the orphaned workers to crash on freed memory (DETERMINATE-NIX-66: SIGSEGV in `git_odb_read` → `git_cache_get_raw` → `pthread_rwlock_rdlock` during `nix flake show`). Patch libgit2 to proceed with however many threads were successfully created, letting the work-stealing loop redistribute the thread-less partitions' work (stolen in their entirety, so every object is still searched for deltas), and to fall back to the single-threaded delta search if no threads could be created at all. Also remove a use-after-free of the `thread_params` array on the (unlikely) mutex lock failure path. Assisted-by: Claude Fable 5 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe packaging configuration now overrides libgit2 to include debug information and a patch that handles worker thread creation failures during threaded delta search, including partial-thread and single-thread fallback paths. Changeslibgit2 thread recovery
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packaging/dependencies.nix`:
- Around line 175-180: Update the patches assignment to parenthesize the
fallback expression, using (old.patches or [ ]) before appending the libgit2
patch so the patch is included whether or not old.patches already exists.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8bd14cfe-c792-4f27-a32b-fbd84a1fd242
📒 Files selected for processing (2)
packaging/dependencies.nixpackaging/patches/libgit2-packbuilder-dont-fail-on-thread-create-error.patch
cole-h
approved these changes
Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Several sentry crash reports show Nix crashing in a libgit2 pack worker thread, e.g.
while the calling
GitFileSystemObjectSinkImpl::flush()has exited or is exiting. The libgit2 addresses resolve tothreaded_find_deltas,find_deltas,git_odb_read,git_cache_get_raw.The most likely cause is that some but not all worker thread creations in libgit2's
ll_find_deltasfail (we have seen other instances of thread creation failing, so that tracks).ll_find_deltathen returns with an error while leaking the already created worker threads, which keep running on state that is no longer valid.The patch to libgit2 should make it proceed with the threads that is has, i.e. it should never leak worker threads.
Also, enable debug symbols for libgit2.
Context
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.