Skip to content

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

Open
irfaan101 wants to merge 3 commits into
crewAIInc:mainfrom
irfaan101:fix/lib-cli-src-crewai_cli-git.py
Open

refactor: fix memory leak in git.py by using cached_property#5807
irfaan101 wants to merge 3 commits into
crewAIInc:mainfrom
irfaan101:fix/lib-cli-src-crewai_cli-git.py

Conversation

@irfaan101
Copy link
Copy Markdown

@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
    • Improved Git repository detection: simplified status check (now accessed as a property rather than invoked) and improved caching for more reliable, efficient repository-state checks.

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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 01e1930e-3097-4ef9-a092-614fcc74b6f2

📥 Commits

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

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

📝 Walkthrough

Walkthrough

Repository.init now accesses the cached attribute is_git_repo instead of calling it. Repository.is_git_repo was changed from an @lru_cache(maxsize=None) method to an @cached_property with an updated docstring describing per-instance caching.

Changes

Caching Refactor

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

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Poem

I’m a rabbit in code with a nimble hop,
From method to property I made a small stop.
Cached per-instance, I snugly reside,
No global call fuss — just a calm little stride.
🐇✨

🚥 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: refactoring git.py to replace @lru_cache with @cached_property to address a memory leak, which is the primary objective of the PR.
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

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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

Notes:
- TODO: This method is cached to avoid redundant checks, but using lru_cache on methods can lead to memory leaks
Notes: This method uses cached_property to avoid redundant checks while being memory-safe.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you remove this Note?

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