fix(opal-server): memory leak — purge GitPolicyFetcher caches on scope delete + webhook task cleanup (PR2)#923
Open
Conversation
… repo handles Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Capture the deleted scope's source before scanning siblings (fixing the loop-variable shadowing bug where the clone path was computed from the last-iterated scope). Drop the cached pygit2.Repository handle via forget_repo and pop repos_last_fetched when no sibling shares the source. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…terating) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Review notes (planned with the
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR2 — Memory leak fix (GitPolicyFetcher caches + webhook task list)
Linear: PER-15156
What
Stops OPAL-server memory growth by releasing the leaked
pygit2.Repositoryhandle when a scope is deleted, and fixes the webhook task-list cleanup bug. Serial only — no concurrency introduced (the delete/fetch race is deferred to PR4).repo_locksdeliberately left untouched (lock-identity hazard, deferred).Changes
GitPolicyFetcher.forget_repo(path)(git_fetcher.py) — popsrepos[path]and callsRepository.free()when available (guarded; falls back to GC), releasing OS fds + mmapped pack indexes.ScopesService.delete_scope(scopes/service.py) — captures the deleted scope's source before scanning siblings (fixes a latent loop-variable shadowing bug that computed the clone path from the wrong scope), then removes the clone dir +forget_repo+ popsrepos_last_fetchedwhen no sibling shares the source.BasePolicyWatcherTask._on_webhook(policy/watcher/task.py) — replaces remove-while-iterating (which skipped elements, leaking finished tasks) with a list rebuild.Review-driven fix (beyond the original plan)
The clone-deletion gate originally used
url_shared, but clones are keyed strictly per-source_id(base_dir/git_sources/<source_id>). WithOPAL_SCOPES_REPO_CLONES_SHARDS > 1, same-url/different-branch scopes resolve to different clone dirs — sourl_sharedwould skip deleting a clone that no sibling actually uses, leaking the exact handle this PR targets. Both gates now usesource_id_shared(strictly stronger;url_sharedwas redundant), with a dedicatedSHARDS>1test.Tests
New unit tests (TDD):
forget_repo_test.py(3),delete_scope_cache_purge_test.py(3, incl. the shards>1 divergence case),webhook_task_cleanup_test.py(1, asserts both purge and that the new trigger task is scheduled).Verification
test_churn_releases_caches✅test_repeat_sync_does_not_grow✅Repository.freepresentopal_common/opal_clientchanges; norepo_locksmutation; no config key added🤖 Generated with Claude Code