refactor: fix memory leak in git.py by using cached_property#5807
refactor: fix memory leak in git.py by using cached_property#5807irfaan101 wants to merge 3 commits into
Conversation
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
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughRepository.init now accesses the cached attribute ChangesCaching Refactor
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
lib/cli/src/crewai_cli/git.py
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (1)
lib/cli/src/crewai_cli/git.py (1)
12-12:⚠️ Potential issue | 🔴 Critical | ⚡ Quick winUse property access for
is_git_repo.Line 12 still calls
self.is_git_repo(), but after Lines 43-44 this resolves to a cachedbool. Object construction will raiseTypeError: '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.pyAlso 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
📒 Files selected for processing (1)
lib/cli/src/crewai_cli/git.py
|
|
||
| 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. |
There was a problem hiding this comment.
Can you remove this Note?
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