Skip to content

fix(cli): memory leak in Repository.is_git_repo() due to @lru_cache#4667

Open
giulio-leone wants to merge 1 commit intocrewAIInc:mainfrom
giulio-leone:fix/issue-4210-lru-cache-memory-leak
Open

fix(cli): memory leak in Repository.is_git_repo() due to @lru_cache#4667
giulio-leone wants to merge 1 commit intocrewAIInc:mainfrom
giulio-leone:fix/issue-4210-lru-cache-memory-leak

Conversation

@giulio-leone
Copy link
Contributor

@giulio-leone giulio-leone commented Mar 1, 2026

Summary

Fixes #4210

@lru_cache(maxsize=None) on the instance method Repository.is_git_repo() holds a strong reference to self in the global cache, preventing garbage collection of Repository instances.

Changes

  • lib/crewai/src/crewai/cli/git.py: Replaced @lru_cache(maxsize=None) with a manual instance-level _is_git_repo_cache attribute initialized in __init__. The cache is checked at the start of is_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 — verifies Repository instances are garbage collected (uses weakref + gc.collect()).

Why this approach

Using an instance attribute instead of @lru_cache:

  • Eliminates the memory leak (no global reference to self)
  • Preserves caching behavior (subprocess only called once per instance)
  • Cache lifetime is tied to instance lifetime — cleaned up automatically on GC

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 Repository instances.

Overview
Fixes a memory leak in Repository.is_git_repo() by removing the @lru_cache decorator and replacing it with an instance-level _is_git_repo_cache, preserving “check once per instance” behavior without globally retaining self.

Adds tests that (1) assert repeated is_git_repo() calls don’t spawn extra git rev-parse subprocesses and (2) verify Repository instances are garbage-collectable via weakref/gc.

Written by Cursor Bugbot for commit e10cd6e. This will update automatically on new commits. Configure here.

Copilot AI review requested due to automatic review settings March 1, 2026 20:48
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) on Repository.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 Repository instances 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.

@giulio-leone giulio-leone force-pushed the fix/issue-4210-lru-cache-memory-leak branch 3 times, most recently from 6670fcc to bcede5c Compare March 4, 2026 00:41
@giulio-leone giulio-leone force-pushed the fix/issue-4210-lru-cache-memory-leak branch from bcede5c to e10cd6e Compare March 4, 2026 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Memory leak in Repository.is_git_repo() due to @lru_cache decorator

2 participants