fix(worker): clean up orphaned zoekt .tmp shard files after every index attempt
Summary
- Moved
cleanupTempShardsfrom thecatchblock into afinallyblock inrepoIndexManager.tsso leftover.tmpshard files are cleaned up on every indexing attempt, not just on failure. - Previously, if a previous run was killed mid-rename (zoekt writes shards to
.tmpfirst and atomically renames them), the orphaned.tmpfiles would persist indefinitely — zoekt itself only tracks temp files from the current build, so a subsequent successful run would not remove them.
Investigation
- https://chat.betterstack.com/team/t306397/chats/48168
Test plan
- [ ] Verify that successful indexing runs no longer leave behind
.tmpshard files when orphans exist from a prior interrupted run. - [ ] Verify that failed indexing runs still trigger cleanup and propagate the original error (the
cleanupTempShardscall is best-effort and does not throw, so it cannot mask the error fromcatch).
🤖 Generated with Claude Code
Summary by CodeRabbit
-
Bug Fixes
- Temporary shard files are now consistently cleaned up after every indexing attempt, eliminating leftover files from interrupted runs.
-
Documentation
- Changelog updated with an "Unreleased" entry noting the cleanup fix.
-
Tests
- Test suite updated to cover and validate the new cleanup behavior during indexing.
@brendan-kellam your pull request is missing a changelog!
Walkthrough
The PR refactors indexing error handling to guarantee cleanup of temporary Zoekt shard files on every indexing attempt, not only on failure. The cleanupTempShards(repo) call was moved from the catch block into a finally block, tests were updated to mock the function, and a changelog entry documents the fix.
Changes
Cleanup guarantee in repo indexing
| Layer / File(s) | Summary |
|---|---|
Try-catch-finally refactor, test mock, and changelog update packages/backend/src/repoIndexManager.ts, packages/backend/src/repoIndexManager.test.ts, CHANGELOG.md |
Moved cleanupTempShards(repo) from the catch block to a finally block in indexRepository, simplified the catch logging and rethrow behavior; added cleanupTempShards to the Zoekt mock used in tests; added an Unreleased "Fixed" changelog entry noting orphaned .tmp shard cleanup. |
Estimated code review effort
🎯 2 (Simple) | ⏱️ ~8 minutes
Possibly related PRs
- sourcebot-dev/sourcebot#805: Both PRs modify
packages/backend/src/repoIndexManager.ts'sindexRepositoryflow to usecleanupTempShards(repo)for Zoekt.tmpshard cleanup (with this PR ensuring cleanup runs in afinallyafter every indexing attempt).
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title accurately and clearly describes the main change: moving temp shard cleanup to execute on every index attempt rather than only on failure. |
| Docstring Coverage | ✅ Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
| 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 docstrings
- [ ] Create stacked PR
- [ ] Commit on current branch
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Commit unit tests in branch
brendan-kellam/fix-orphaned-temp-shards
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.