Repository navigation
fix(dash-spv): make stop wait for the tasks the sync loop detaches - #253
xdustinface wants to merge 1 commit into
Conversation
The sync loop runs fork recovery and the stop of a failed loop in detached tasks, each holding a client clone and with it the storage and its lock file. `stop` did not wait for them, so it could return while they still held the lock. They now go into a `JoinSet` that `stop` joins after releasing the `sync_loop` lock. Closes upstream issue 1171 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
✅ Review complete in 3m 56s Planner (4s) Review (behavior · pair): 5 findings Judge: 3 kept · 0 dropped (1m 50s) Review metadataConfig:
Account limits:
Judge decisions:
Timing:
Last reviewed
|
There was a problem hiding this comment.
✅ Approved. 1 warning and 2 notes, none of which block.
Caution
Claude subscription has used 71% of its weekly limit, past the 70% mark. It resets 2026-11-17 05:00 UTC.
Once a limit is used up, reviews on that account stop until it resets.
What this does: Before, fork recovery and the cleanup after a failed sync loop ran as untracked background tasks. These could still hold storage after stop returned.
2 notes · not blocking
✨ The new test fills the internal detached_tasks set itself and never runs the paths that now use it
Note, not blocking · Testing · Moderate · dash-spv/src/client/mod.rs:142
Why: The test spawns a task straight into the private detached_tasks field. It never triggers a fork recovery or a failed loop through the client. Reverting both sync_coordinator.rs changes back to tokio::spawn (lines 153 and 195) still passes it, so the fix for the reported lock leak has no test. Only removing self.join_detached_tasks().await at lifecycle.rs:245 makes it fail.
rust-dashcore/dash-spv/src/client/mod.rs
Lines 140 to 144 in ee1b272
How to fix it
Add a test that makes the sync loop fail, or emit SyncEvent::ForkDetected, through the mock network or coordinator. Then call stop and assert that Arc::strong_count(&client.storage) == 1.
Checked in the code
On the flagged line: client.detached_tasks.lock().await.spawn(async move { ... drop(detached); }) writes internal state directly
And :195 are the changed spawn sites, and no test drives them
rust-dashcore/dash-spv/src/client/sync_coordinator.rs
Lines 152 to 156 in ee1b272
- Create an issue for this
✨ Finished detached tasks stay in the JoinSet until stop is called
Note, not blocking · Performance · Quick fix · dash-spv/src/client/sync_coordinator.rs:153
Why: A JoinSet keeps every finished task until join_next reaps it. Only stop reaps this set. A client that recovers from many forks over a long run without stopping keeps one entry for each recovery. Each entry is small, but the set only grows.
rust-dashcore/dash-spv/src/client/sync_coordinator.rs
Lines 151 to 155 in ee1b272
How to fix it
Before each spawn, clear finished entries with while tasks.try_join_next().is_some() {}.
Checked in the code
On the flagged line: Spawns a handle_fork task for every ForkDetected event
join_next is called only from join_detached_tasks, which only stop calls
rust-dashcore/dash-spv/src/client/lifecycle.rs
Lines 258 to 262 in ee1b272
- Create an issue for this
Related work: open PRs and issues that may cover the same change. None of them blocks.
What to test
The review model wrote these steps. Check what a step runs before you follow it.
Setup: Run the dash-spv tests and a client against a regtest dashd node, using eval $(python3 contrib/setup-dashd.py).
- Run
cargo test -p dash-spv stop_waits_for_detached_tasks. Expect it to pass, with the storageArcstrong count at 1 afterstop. - Start a client with
run, callstop, then open a newDiskStorageManageron the same path. Expect it to open without lock or busy errors. - Trigger a fork on regtest (invalidate a block and mine a competing branch) while the client runs. Expect the client to truncate above the fork height and resume syncing.
- Call
stopright after a fork is detected. Expectstopto return andis_runningto returnfalse, with no sync loop restarted. - Make the sync loop fail, for example by making an event monitor channel fail. Expect
on_errorto fire andis_runningto becomefalse, thenstopto return promptly. - Call
clear_storagewhile a fork recovery is in progress. Expect it to complete without hanging and the tip to reset afterwards.
About this review
- Reviewed
ee1b272, round 1 - 74 changed lines read in 3m 56s
- Found by
claude-sonnet-5-5andclaude-opus-5-5, judged byclaude-opus-5-5 - Tokens: 379k in (195k from cache) · 27k out · about $1.24 at API prices
- Runner: github-hosted · started after 4m 16s · peak memory 1.4 GiB of 15.6 GiB
- How Manki reviews
| /// stopping meanwhile can still add to it. | ||
| async fn join_detached_tasks(&self) { | ||
| loop { | ||
| let mut tasks = self.detached_tasks.lock().await; |
There was a problem hiding this comment.
join_detached_tasks holds the detached_tasks lock while joining, contrary to its doc, and can deadlock a sync loop
What goes wrong: The doc says the set is taken out of its lock while joining so a stopping sync loop can still add to it. The code keeps the MutexGuard alive across every join_next().await, so any sync loop that hits a fork or error and calls detached_tasks.lock().await.spawn(..) blocks until the whole join ends. If a pending detached task needs sync_loop, which a concurrent stop() holds while awaiting that blocked loop, none of the three can proceed and stop() never returns.
| let mut tasks = self.detached_tasks.lock().await; | |
| let mut tasks = std::mem::take(&mut *self.detached_tasks.lock().await); |
How to fix it and 18 places read · confirmed with tools
How to fix it
Move the set out of the lock before joining, so the lock is held only briefly: let mut tasks = std::mem::take(&mut *self.detached_tasks.lock().await);. Keep the outer loop so tasks added meanwhile are joined too.
Checked in the code
On the commented line: let mut tasks = self.detached_tasks.lock().await; is a guard that lives to the end of the loop iteration
while let Some(result) = tasks.join_next().awaitawaits with the guard held.while let Some(result) = tasks.join_next().awaitawaits while that guard is held
rust-dashcore/dash-spv/src/client/lifecycle.rs
Lines 258 to 263 in ee1b272
client.detached_tasks.lock().await.spawn(..) in the sync loop task and :195 detached_tasks.lock().await.spawn(..) both wait for that lock
rust-dashcore/dash-spv/src/client/sync_coordinator.rs
Lines 149 to 156 in ee1b272
stop_locked awaits the loop task while holding sync_loop, and handle_fork and stop_failed take sync_loop themselves
rust-dashcore/dash-spv/src/client/lifecycle.rs
Lines 238 to 242 in ee1b272
dash-spv/src/client/lifecycle.rs:249-250: The comment saysThe set is taken out of its lock while joining, so a sync loop stopping meanwhile can still add to itdash-spv/src/client/sync_coordinator.rs:195: The error path of the loop task runsdetached_tasks.lock().await.spawn(...)dash-spv/src/client/lifecycle.rs:288:let Some(forked) = sync_loop.take_if(|running| running.shutdown.is_cancelled()) else {dash-spv/src/client/lifecycle.rs:291:handle_forkcallsself.stop_locked(forked).await, andstop_lockedawaits the loop'staskdash-spv/src/client/lifecycle.rs:243:drop(sync_loop)runs before the join, so a concurrentruncan start a new loop whilestopis joiningdash-spv-ffi/src/client.rs:150: And :175block_on(client.inner.stop())andblock_on(client.inner.run())can be called from different threadsdash-spv/src/client/lifecycle.rs:287:let mut sync_loop = self.sync_loop.lock().await;dash-spv/src/client/lifecycle.rs:270:let mut sync_loop = self.sync_loop.lock().await;dash-spv/src/client/lifecycle.rs:271:if let Some(failed) = sync_loop.take_if(|running| running.shutdown.is_cancelled()) {dash-spv/src/client/sync_coordinator.rs:53:let monitor_shutdown = CancellationToken::new();dash-spv/src/client/sync_coordinator.rs:130:let shutdown = monitor_shutdown.clone();dash-spv/src/client/sync_coordinator.rs:201:shutdown,
Double-checked
Manki tried to disprove this by reading the code and could not. The guard taken at lifecycle.rs:253 is still held across every join_next().await at :259, which contradicts the doc at :249-250. A loop B that cancels its to…
- Create an issue for this
Replay of upstream PR 1178 at its initial state, for testing Manki reviews.
The sync loop runs fork recovery (
handle_fork) and the stop of a failed loop (stop_failed) in detached tasks. Each holds a client clone, and with it the storage and its lock file.stop()didn't wait for them, so it could return while they still held the lock (upstream platform issue 5381).JoinSeton the client.stop()joins them after releasing thesync_looplock, which they take themselves. With the loop already taken, they return without acting.Closes upstream issue 1171