Skip to content

fix: cleanup dangling forks in chain follower#7179

Open
hanabi1224 wants to merge 2 commits into
mainfrom
hm/cleanup-dangling-forks
Open

fix: cleanup dangling forks in chain follower#7179
hanabi1224 wants to merge 2 commits into
mainfrom
hm/cleanup-dangling-forks

Conversation

@hanabi1224

@hanabi1224 hanabi1224 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Summary of changes

Changes introduced in this pull request:

Reference issue to close (if applicable)

Closes #7177

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

Outside contributions

  • I have read and agree to the CONTRIBUTING document.
  • I have read and agree to the AI Policy document. I understand that failure to comply with the guidelines will lead to rejection of the pull request.

Summary by CodeRabbit

  • Bug Fixes
    • Improved chain synchronization by automatically pruning orphaned fork data periodically.
    • Enhanced task cleanup and memory optimization to reduce resource consumption during sync operations.

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

chain_follower.rs gains a SyncStateMachine::cleanup_dangling_forks() method that removes tipsets from chains whose target epoch is below the finalized epoch. The chain_follower loop calls this cleanup on a 1-minute interval. The SyncTask handler closure is also updated to always remove completed tasks from the shared set and call shrink_to_fit().

Changes

Dangling Fork Pruning in Chain Follower

Layer / File(s) Summary
cleanup_dangling_forks method and Duration import
src/chain_sync/chain_follower.rs
Adds Duration import and the SyncStateMachine::cleanup_dangling_forks public method, which iterates over current chains and removes tipsets whose last/target epoch is below the finalized epoch, emitting a log message covering the cleaned epoch range.
Loop integration: interval-based cleanup and task-set shrink
src/chain_sync/chain_follower.rs
Introduces last_fork_cleanup tracking in the main state-update loop to invoke cleanup_dangling_forks() every 1 minute. Updates the spawned SyncTask handler closure to always remove the completed task from the shared tasks set and call shrink_to_fit() after the state update and notification.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • sudo-shashank
  • LesnyRumcajs
  • akaladarshi
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: adding cleanup logic for dangling forks in the chain follower component, which is the primary focus of the code modifications.
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 hm/cleanup-dangling-forks
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch hm/cleanup-dangling-forks

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

@hanabi1224 hanabi1224 force-pushed the hm/cleanup-dangling-forks branch from 7b0e4af to d469577 Compare June 15, 2026 10:27
@hanabi1224 hanabi1224 marked this pull request as ready for review June 15, 2026 10:34
@hanabi1224 hanabi1224 requested a review from a team as a code owner June 15, 2026 10:34
@hanabi1224 hanabi1224 requested review from LesnyRumcajs and sudo-shashank and removed request for a team June 15, 2026 10:34

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/chain_sync/chain_follower.rs (1)

906-924: 💤 Low value

Consider calling shrink_to_fit() after bulk removal.

After removing potentially many tipsets from dangling forks, the tipsets HashMap may have excess capacity. For consistency with add_full_tipset (line 777) which calls shrink_to_fit(), consider adding the same call here to reclaim memory after cleanup.

♻️ Suggested change
     pub fn cleanup_dangling_forks(&mut self) {
         let finalized_epoch = self.cs.ec_calculator_finalized_epoch();
         for chain in self.chains() {
             // Cleanup dangling fork when its target epoch is finalized
             if let Some(target) = chain.last()
                 && target.epoch() < finalized_epoch
             {
                 chain.iter().for_each(|ts| {
                     self.tipsets.remove(ts.key());
                 });
                 tracing::info!(
                     "Cleaned up dangling fork from epoch {} to {}",
                     chain.first().map(|ts| ts.epoch()).unwrap_or_default(),
                     chain.last().map(|ts| ts.epoch()).unwrap_or_default(),
                 );
             }
         }
+        self.tipsets.shrink_to_fit();
     }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/chain_sync/chain_follower.rs` around lines 906 - 924, After the bulk
removal of tipsets in the cleanup_dangling_forks method (where
chain.iter().for_each removes multiple tipsets from self.tipsets), add a call to
self.tipsets.shrink_to_fit() to reclaim the excess capacity left by the HashMap
after deletion. This maintains consistency with the approach already used in the
add_full_tipset method for memory efficiency during cleanup operations.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/chain_sync/chain_follower.rs`:
- Around line 906-924: After the bulk removal of tipsets in the
cleanup_dangling_forks method (where chain.iter().for_each removes multiple
tipsets from self.tipsets), add a call to self.tipsets.shrink_to_fit() to
reclaim the excess capacity left by the HashMap after deletion. This maintains
consistency with the approach already used in the add_full_tipset method for
memory efficiency during cleanup operations.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e1699663-311d-48a7-acef-94bfae5dff97

📥 Commits

Reviewing files that changed from the base of the PR and between 194e6c7 and d469577.

📒 Files selected for processing (1)
  • src/chain_sync/chain_follower.rs
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • filecoin-project/lotus (manual)

@codecov

codecov Bot commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.34%. Comparing base (194e6c7) to head (d469577).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/chain_sync/chain_follower.rs 0.00% 24 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
src/chain_sync/chain_follower.rs 32.72% <0.00%> (-0.77%) ⬇️

... and 4 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 194e6c7...d469577. Read the comment docs.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@LesnyRumcajs

Copy link
Copy Markdown
Member

As discussed, let's let it run for a bit.

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.

Chain follower doesn't prune forks

2 participants