sourcebot icon indicating copy to clipboard operation
sourcebot copied to clipboard

fix(worker): clean up orphaned zoekt .tmp shard files after every index attempt

Open brendan-kellam opened this issue 6 months ago • 2 comments

Summary

  • Moved cleanupTempShards from the catch block into a finally block in repoIndexManager.ts so leftover .tmp shard 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 .tmp first and atomically renames them), the orphaned .tmp files 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 .tmp shard files when orphans exist from a prior interrupted run.
  • [ ] Verify that failed indexing runs still trigger cleanup and propagate the original error (the cleanupTempShards call is best-effort and does not throw, so it cannot mask the error from catch).

🤖 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.

Review Change Stack

brendan-kellam avatar May 20 '26 17:05 brendan-kellam

@brendan-kellam your pull request is missing a changelog!

github-actions[bot] avatar May 20 '26 17:05 github-actions[bot]

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's indexRepository flow to use cleanupTempShards(repo) for Zoekt .tmp shard cleanup (with this PR ensuring cleanup runs in a finally after 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 20 '26 17:05 coderabbitai[bot]