refactor: move manager initialization from trait method to constructors #467
refactor: move manager initialization from trait method to constructors #467xdustinface merged 1 commit intov0.42-devfrom
Conversation
📝 WalkthroughWalkthroughThis pull request refactors manager initialization by converting synchronous constructors to asynchronous ones that perform their own setup, and removes the Changes
Sequence DiagramsequenceDiagram
participant Client
participant Managers as Managers<br/>(BlockHeaders, etc.)
participant Storage
participant Coordinator
rect rgba(200, 150, 255, 0.5)
Note over Client,Coordinator: Before (Old Flow)
Client->>Managers: new() → sync
Client->>Managers: initialize().await
Managers->>Storage: read state
Managers->>Managers: set initial progress
Client->>Coordinator: new()
Client->>Coordinator: start_sync()
end
rect rgba(150, 200, 255, 0.5)
Note over Client,Coordinator: After (New Flow)
Client->>Managers: new().await (with init)
Managers->>Storage: read state
Managers->>Managers: set initial progress
Client->>Client: initialize_genesis_block()
Client->>Coordinator: new().await
Coordinator->>Managers: aggregate progress
Client->>Coordinator: start_sync()
end
Estimated Code Review Effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
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 |
|
This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them. |
Move the storage background persist worker spawn from `DiskStorageManager::new()` to a new `StorageManager::start()` trait method, called from `DashSpvClient::start()`. This prevents a tokio task leak when clients are created and destroyed without being started in #467.
7d5adf1 to
c85abe0
Compare
d655bb3 to
27aefe4
Compare
Move the storage background persist worker spawn from `DiskStorageManager::new()` to a new `StorageManager::start()` trait method, called from `DashSpvClient::start()`. This prevents a tokio task leak when clients are created and destroyed without being started in dashpay#467.
27aefe4 to
e364976
Compare
|
This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them. |
e364976 to
89ab17e
Compare
|
This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them. |
89ab17e to
8e08c35
Compare
- Moves initialization logic out of the `SyncManager::initialize()` trait method and into each manager's constructor. - Genesis block is now stored before managers are created, so they can read the tip during construction. - The `SyncCoordinator` seeds initial progress from actual manager state instead of default and uses `WatchStream::from_changes` to avoid re-emitting all initial progresses.
8e08c35 to
640c1f8
Compare
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
dash-spv/src/client/lifecycle.rs (1)
175-203:⚠️ Potential issue | 🟠 MajorRollback coordinator startup if network connect fails.
After
SyncCoordinator::start(Line 187), aconnect()failure (Line 194) returns early withrunning == false, but spawned sync tasks remain active. Sincestop()exits early when not running, those tasks may never be shut down.🔧 Suggested fix
- // Connect to network - self.network.lock().await.connect().await?; + // Connect to network + if let Err(e) = self.network.lock().await.connect().await { + if let Err(shutdown_err) = self.sync_coordinator.lock().await.shutdown().await { + tracing::warn!( + "Failed to rollback sync coordinator after connect error: {}", + shutdown_err + ); + } + return Err(e); + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@dash-spv/src/client/lifecycle.rs` around lines 175 - 203, When network.connect() fails after starting the sync coordinator, rollback the coordinator startup by calling its shutdown/stop method (the same API used to stop coordinator tasks) before returning the error; specifically, in Client::start after self.network.lock().await.connect().await? fails, acquire the sync_coordinator lock and invoke SyncCoordinator::stop (or the appropriate shutdown method) to terminate spawned tasks, handle/log any error from that stop call, and then return SpvError::Sync(e) so running remains false; reference the start method, self.sync_coordinator, SyncCoordinator::start, and the network.connect call when making this change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@dash-spv/src/client/lifecycle.rs`:
- Around line 175-203: When network.connect() fails after starting the sync
coordinator, rollback the coordinator startup by calling its shutdown/stop
method (the same API used to stop coordinator tasks) before returning the error;
specifically, in Client::start after self.network.lock().await.connect().await?
fails, acquire the sync_coordinator lock and invoke SyncCoordinator::stop (or
the appropriate shutdown method) to terminate spawned tasks, handle/log any
error from that stop call, and then return SpvError::Sync(e) so running remains
false; reference the start method, self.sync_coordinator,
SyncCoordinator::start, and the network.connect call when making this change.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (15)
dash-spv/ARCHITECTURE.mddash-spv/src/client/lifecycle.rsdash-spv/src/sync/block_headers/manager.rsdash-spv/src/sync/block_headers/sync_manager.rsdash-spv/src/sync/blocks/manager.rsdash-spv/src/sync/blocks/sync_manager.rsdash-spv/src/sync/chainlock/manager.rsdash-spv/src/sync/chainlock/sync_manager.rsdash-spv/src/sync/filter_headers/manager.rsdash-spv/src/sync/filter_headers/sync_manager.rsdash-spv/src/sync/filters/manager.rsdash-spv/src/sync/filters/sync_manager.rsdash-spv/src/sync/masternodes/manager.rsdash-spv/src/sync/sync_coordinator.rsdash-spv/src/sync/sync_manager.rs
💤 Files with no reviewable changes (7)
- dash-spv/ARCHITECTURE.md
- dash-spv/src/sync/sync_manager.rs
- dash-spv/src/sync/chainlock/sync_manager.rs
- dash-spv/src/sync/filter_headers/sync_manager.rs
- dash-spv/src/sync/filters/sync_manager.rs
- dash-spv/src/sync/blocks/sync_manager.rs
- dash-spv/src/sync/block_headers/sync_manager.rs
SyncManager::initialize()trait method and into each manager's constructor.SyncCoordinatorseeds initial progress from actual manager state instead of default and usesWatchStream::from_changesto avoid re-emitting all initial progresses.Based on:
SyncState::Initializingvariant #465ensure_not_started()guard #466Summary by CodeRabbit