Skip to content

refactor: fix memory leak in git.py by using cached_property#5807

Merged
greysonlalonde merged 6 commits into
crewAIInc:mainfrom
irfaan101:fix/lib-cli-src-crewai_cli-git.py
May 18, 2026
Merged

refactor: fix memory leak in git.py by using cached_property#5807
greysonlalonde merged 6 commits into
crewAIInc:mainfrom
irfaan101:fix/lib-cli-src-crewai_cli-git.py

Conversation

@irfaan101
Copy link
Copy Markdown
Contributor

@irfaan101 irfaan101 commented May 14, 2026

This PR addresses a legacy TODO in lib/cli/src/crewai_cli/git.py where @lru_cache on an instance method was identified as a potential memory leak source. I have replaced it with @cached_property, which ensures the cache is tied to the instance lifecycle and properly cleared during garbage collection

Summary by CodeRabbit

  • Refactor
    • Repository detection simplified and made more reliable: repository status is now accessed via a cached property instead of repeated checks, reducing redundant work. This improves CLI responsiveness, lowers overhead during repository-state queries, and yields more consistent behavior across commands.

Review Change Stack

This PR addresses a legacy TODO in lib/cli/src/crewai_cli/git.py where @lru_cache on an instance method was identified as a potential memory leak source. I have replaced it with @cached_property, which ensures the cache is tied to the instance lifecycle and properly cleared during garbage collection
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Repository.init now reads the cached attribute is_git_repo instead of calling it. Repository.is_git_repo was changed from an @lru_cache(maxsize=None)-decorated method to an @cached_property that returns a bool and documents per-instance caching.

Changes

Caching Refactor

Layer / File(s) Summary
Import and init usage
lib/cli/src/crewai_cli/git.py
Replaced lru_cache import with cached_property. Updated Repository.__init__ to validate repository status via attribute access self.is_git_repo instead of calling self.is_git_repo().
Cached-property implementation
lib/cli/src/crewai_cli/git.py
Converted Repository.is_git_repo from an @lru_cache(maxsize=None) method to an @cached_property; updated docstring to describe cached-property semantics and boolean return.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I’m a rabbit in code with a nimble hop,
I swapped a call for a cozy prop,
Cached per-instance, snug and bright,
Few lines changed — the logic’s light.
🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: replacing lru_cache with cached_property to address a memory leak issue in git.py.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

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 `@lib/cli/src/crewai_cli/git.py`:
- Around line 43-44: The code converted is_git_repo to a `@cached_property` but
call sites still use method syntax causing "bool object not callable"; find
usages of self.is_git_repo (e.g., the call currently written as
self.is_git_repo()) and change them to property access self.is_git_repo (remove
the parentheses), ensuring imports/use of cached_property remain intact and run
tests to confirm the TypeError is resolved.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 72ecd56e-de1e-473c-b568-1e0f072cb98c

📥 Commits

Reviewing files that changed from the base of the PR and between c36827b and b6d07a5.

📒 Files selected for processing (1)
  • lib/cli/src/crewai_cli/git.py

Comment thread lib/cli/src/crewai_cli/git.py
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
lib/cli/src/crewai_cli/git.py (1)

12-12: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Use property access for is_git_repo.

Line 12 still calls self.is_git_repo(), but after Lines 43-44 this resolves to a cached bool. Object construction will raise TypeError: 'bool' object is not callable.

Suggested fix
-        if not self.is_git_repo():
+        if not self.is_git_repo:
             raise ValueError(f"{self.path} is not a Git repository.")
#!/bin/bash
set -euo pipefail

# Verify the property declaration and any remaining callable-style access in this file.
rg -n -C2 '@cached_property|def is_git_repo|is_git_repo\s*\(' lib/cli/src/crewai_cli/git.py

Also applies to: 43-44

🤖 Prompt for 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.

In `@lib/cli/src/crewai_cli/git.py` at line 12, The code currently calls
self.is_git_repo() but is_git_repo has been converted to a cached property,
causing a TypeError; update all callable-style uses (e.g., the check at the top
and the occurrences referenced around lines 43-44) to use property access
self.is_git_repo instead of self.is_git_repo(), and verify there are no
remaining callable invocations of is_git_repo (search for "is_git_repo(") after
the change; keep the cached_property decorator on is_git_repo unchanged.
🤖 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 `@lib/cli/src/crewai_cli/git.py`:
- Around line 9-10: The guard currently checks the staticmethod object instead
of invoking it; replace the attribute access with a call to is_git_installed
(e.g., use self.is_git_installed() or GitClassName.is_git_installed()) so the
boolean result is evaluated and the ValueError is raised when Git is not
present; update the check in the constructor or method that contains the line
with `if not self.is_git_installed` to call the function.

---

Duplicate comments:
In `@lib/cli/src/crewai_cli/git.py`:
- Line 12: The code currently calls self.is_git_repo() but is_git_repo has been
converted to a cached property, causing a TypeError; update all callable-style
uses (e.g., the check at the top and the occurrences referenced around lines
43-44) to use property access self.is_git_repo instead of self.is_git_repo(),
and verify there are no remaining callable invocations of is_git_repo (search
for "is_git_repo(") after the change; keep the cached_property decorator on
is_git_repo unchanged.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: deace629-2d2c-424f-82a0-0804607fa018

📥 Commits

Reviewing files that changed from the base of the PR and between b6d07a5 and bb4a79b.

📒 Files selected for processing (1)
  • lib/cli/src/crewai_cli/git.py

Comment thread lib/cli/src/crewai_cli/git.py Outdated
Comment thread lib/cli/src/crewai_cli/git.py Outdated
Removed unnecessary notes from the is_git_repo method docstring.
Copy link
Copy Markdown
Contributor Author

@irfaan101 irfaan101 left a comment

Choose a reason for hiding this comment

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

Updated the PR from my end.

@greysonlalonde greysonlalonde merged commit e8aa870 into crewAIInc:main May 18, 2026
51 of 84 checks passed
@irfaan101 irfaan101 deleted the fix/lib-cli-src-crewai_cli-git.py branch May 19, 2026 05:14
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.

2 participants