Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

Summary

Fixes a memory leak in the Repository class where using @lru_cache on the is_git_repo() instance method prevented garbage collection of Repository instances. The lru_cache decorator holds references to self in its cache dictionary, keeping instances alive indefinitely.

The fix replaces the @lru_cache decorator with instance-level caching using a _is_git_repo_cache: bool | None attribute. This maintains O(1) performance for repeated calls while allowing proper garbage collection when instances go out of scope.

Fixes #4210

Review & Testing Checklist for Human

  • Verify the caching logic is correct: cache should be None initially, populated on first is_git_repo() call, and returned directly on subsequent calls
  • Consider running a manual memory test with multiple Repository instances in a loop to confirm GC works in practice (the tests use mocked subprocess calls)
  • Check if there are other @lru_cache usages on instance methods elsewhere in the codebase that might have the same issue

Suggested test plan: Create a simple script that instantiates Repository objects in a loop and monitors memory usage to confirm it stays stable rather than growing.

Notes

  • The existing TODO comment in the code acknowledged this issue but hadn't been addressed
  • All 13 tests pass (10 existing + 3 new tests for GC, caching behavior, and independent instance caches)
  • Lint (ruff) and type checking (mypy) pass

Link to Devin run: https://app.devin.ai/sessions/56e14e0b869c4483a02020b60148cce2
Requested by: João

…git_repo()

This fixes a memory leak where using @lru_cache on the is_git_repo() instance
method prevented garbage collection of Repository instances. The cache
dictionary held references to self, keeping instances alive indefinitely.

The fix replaces the @lru_cache decorator with instance-level caching using
a _is_git_repo_cache attribute. This maintains O(1) performance for repeated
calls while allowing proper garbage collection when instances go out of scope.

Fixes #4210

Co-Authored-By: João <[email protected]>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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

1 participant