fix: return libgit error rather than asserting in Repository::submodules#1220
Merged
weihanglo merged 1 commit intorust-lang:masterfrom Mar 9, 2026
Merged
Conversation
`Repository::submodules`
weihanglo
approved these changes
Mar 9, 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.
The Problem
When calling
Repository::submodulesconcurrently with another git process on the same repository, the assertion ongit_submodule_lookup's return code can be reached (most often with a value of -3, sometimes -1), causing a crash. I've found this can be mitigated against by acquiring the repository's.git/index.lockfile before callingRepository::submodules, but the assert can still be triggered, albeit more rarely. I've created a standalone reproducer repo for the issue here: https://github.com/WillLillis/git2-submodule-race.The Solution
Return the error from the callback instead of asserting.
Considerations
The doc comment for
git_submodule_foreachstates that a nonzero return from the callback will halt iteration.I checked the source of
git_submodule_lookupand theoutpointer (raw, in the callback) is never written to in the error case, so there's no need to clean it up in the callback on error (i.e. withraw::git_submodule_free).I didn't think it would be appropriate to add as a test, since this isn't possible to test deterministically.
If there is some other reason not to return an error here, would it make sense to add a
# Panicssection to the function's doc comment indicating that it can assert?Thanks for maintaining such a great project!