fix: properly clean up pygit2 Repository objects to prevent symlink/fd accumulation (#634)#881
Open
janisag07 wants to merge 1 commit intopermitio:masterfrom
Open
fix: properly clean up pygit2 Repository objects to prevent symlink/fd accumulation (#634)#881janisag07 wants to merge 1 commit intopermitio:masterfrom
janisag07 wants to merge 1 commit intopermitio:masterfrom
Conversation
…res (permitio#634) When the git remote is unavailable, pygit2 Repository objects cached in GitPolicyFetcher.repos keep file descriptors open. Repeated failed fetches/clones accumulate these descriptors, which show up as symbolic links in /proc and look like zombie processes. Changes: - Add _cleanup_repo_from_cache() that pops the Repository from the class-level cache and calls repo.free() to release native resources. - Call cleanup on fetch failure (the main scenario: repo already cloned but remote goes down). - Call cleanup on clone failure and remove partial clone directory. - Call cleanup when an existing repo is detected as invalid. - Add unit tests covering all cleanup paths.
✅ Deploy Preview for opal-docs canceled.
|
8 tasks
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.
Problem
When the OPAL server has GitHub policy sources configured and GitHub becomes unavailable,
repo.remotes["origin"].fetch()raisespygit2.GitError. TheRepositoryobjects remain in the class-levelGitPolicyFetcher.reposcache, holding native C-level file descriptors open. Each failed fetch attempt accumulates FDs that appear as symbolic links in/proc/{pid}/fd/, making the server look like it has spawned zombie processes.Root Cause
The existing code never calls
repo.free()on cachedpygit2.Repositoryobjects when errors occur. Simply deleting references from the dict is insufficient because Python's garbage collector timing is unpredictable — the C-level resources may not be released promptly.Fix
Added a
_cleanup_repo_from_cache()class method that:GitPolicyFetcher.reposrepo.free()to immediately release native C resourcesfree()Applied cleanup at 4 failure points:
shutil.rmtree(ignore_errors=True)shutil.rmtreeWhy this PR is different
repo.free(): Deterministic release of native C resourcesTests
7 new tests covering cache cleanup, idempotency, error handling, and all failure paths.
/claim #634