fix(cli): memory leak in Repository.is_git_repo() due to @lru_cache#4667
Open
giulio-leone wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix(cli): memory leak in Repository.is_git_repo() due to @lru_cache#4667giulio-leone wants to merge 1 commit intocrewAIInc:mainfrom
giulio-leone wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a memory leak in the CLI Repository.is_git_repo() check by removing @lru_cache from an instance method and replacing it with per-instance caching, ensuring Repository objects can be garbage collected (per #4210).
Changes:
- Replaced
@lru_cache(maxsize=None)onRepository.is_git_repo()with an instance attribute cache (_is_git_repo_cache). - Updated
is_git_repo()logic to consult/populate the instance cache. - Added tests to verify caching behavior and that
Repositoryinstances are garbage-collectable.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lib/crewai/src/crewai/cli/git.py |
Removes global method caching and introduces instance-level cache to prevent retaining self globally. |
lib/crewai/tests/cli/test_git.py |
Adds tests covering caching behavior and the regression for the GC/leak scenario. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6670fcc to
bcede5c
Compare
bcede5c to
e10cd6e
Compare
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.
Summary
Fixes #4210
@lru_cache(maxsize=None)on the instance methodRepository.is_git_repo()holds a strong reference toselfin the global cache, preventing garbage collection ofRepositoryinstances.Changes
lib/crewai/src/crewai/cli/git.py: Replaced@lru_cache(maxsize=None)with a manual instance-level_is_git_repo_cacheattribute initialized in__init__. The cache is checked at the start ofis_git_repo()and populated on first call.lib/crewai/tests/cli/test_git.py: Added two tests:test_is_git_repo_caches_result— verifies the cached value is reused.test_is_git_repo_no_global_cache_leak— verifiesRepositoryinstances are garbage collected (usesweakref+gc.collect()).Why this approach
Using an instance attribute instead of
@lru_cache:self)Note
Low Risk
Low risk: small, localized change to caching of a git-check helper plus added unit tests; behavior should remain the same aside from avoiding retained
Repositoryinstances.Overview
Fixes a memory leak in
Repository.is_git_repo()by removing the@lru_cachedecorator and replacing it with an instance-level_is_git_repo_cache, preserving “check once per instance” behavior without globally retainingself.Adds tests that (1) assert repeated
is_git_repo()calls don’t spawn extragit rev-parsesubprocesses and (2) verifyRepositoryinstances are garbage-collectable viaweakref/gc.Written by Cursor Bugbot for commit e10cd6e. This will update automatically on new commits. Configure here.