Skip to content

Conversation

@AI-God-Dev
Copy link

@AI-God-Dev AI-God-Dev commented Jan 10, 2026

fix: remove lru_cache memory leak in Repository.is_git_repo

Fixes #4210

  • Replace @lru_cache decorator with instance variable caching
  • Prevents memory leaks from cached self references
  • Maintains same O(1) performance characteristics
  • Add comprehensive tests for memory leak prevention and cache verification
  • Remove unused lru_cache import

The @lru_cache decorator on instance methods causes memory leaks because
the cache holds references to self, preventing garbage collection. I found
this issue with a TODO comment acknowledging the problem but no fix.

My solution uses instance-level caching (self._is_git_repo_cache) which
provides the same performance benefit without the memory leak.

This affects anyone running long-lived processes or automation scripts that
repeatedly check git repos. The memory leak is gradual but adds up over time.

Closes #4210


Note

Fix memory leak in Repository.is_git_repo

  • Replace @lru_cache with per-instance _is_git_repo_cache to avoid retaining self and reduce subprocess calls
  • Initialize and use _is_git_repo_cache in Repository.__init__/is_git_repo; behavior remains unchanged
  • Add tests: instance GC verification, per-instance cache isolation, and cache usage checks
  • Remove unused lru_cache import

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

- Replace @lru_cache decorator with instance variable caching
- Prevents memory leaks from cached self references
- Maintains same O(1) performance characteristics
- Add tests for memory leak prevention and cache verification
- Remove unused lru_cache import

The @lru_cache decorator on instance methods causes memory leaks
because the cache holds references to self, preventing garbage
collection. This was acknowledged in the code with a TODO comment.

The fix uses instance-level caching which provides the same
performance benefit without the memory leak.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.


# Modifying one shouldn't affect the other
repo1._is_git_repo_cache = False
assert repo2._is_git_repo_cache is True
Copy link

Choose a reason for hiding this comment

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

Tests register insufficient subprocess calls for multiple instances

Medium Severity

The new tests test_cache_is_per_instance and test_repository_instances_are_garbage_collected only register subprocess commands once, but attempt to create multiple Repository instances. Each Repository.__init__ calls is_git_installed(), is_git_repo(), and fetch(), consuming 3 subprocess registrations. The first test creates 2 instances (needs 6 registrations), and the second creates 100 instances (needs 300 registrations), but both only register 3. Following the existing test patterns (e.g., test_is_synced_when_synced registers duplicate commands), each call consumes its registration. These tests will fail when pytest-subprocess finds no matching registration for the second instance.

🔬 Verification Test

Why verification test was not possible: The tests would require running pytest with the pytest-subprocess plugin installed in the project's test environment. The bug is evident from comparing the new tests against existing test patterns in the same file - test_is_synced_when_synced explicitly registers the same command twice because is_synced() calls status() twice, demonstrating that each fp.register() call is consumed once. The new tests fail to follow this established pattern when creating multiple Repository instances.

Additional Locations (1)

Fix in Cursor Fix in Web

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