Repository navigation
Fix stale snapshot transfer recovery comment on failure/cancellation - #9237
Conversation
The transfer status "comment" prefers the destination-side recovery progress over the sender task status. Recovery progress was tracked in `active_recoveries` and only removed via `finish_shard_recovery`, which was called on the success path only. If `recover_shard_snapshot` returned early - clearing the local shard, downloading the snapshot, checksum mismatch, or cancellation (the future is spawned with `spawn_cancel_on_drop`) - the entry leaked. A leaked entry keeps reporting its last stage with an ever-growing elapsed time (computed live from a fixed `Instant`), so subsequent transfers for the same shard show stale timings until the next recovery overwrites the entry or the node restarts. Replace the manual start/finish pair with an RAII `ShardRecoveryGuard` that removes the progress entry on drop, covering every exit path including early returns and cancellation. The guard only removes its own entry (checked via `Arc::ptr_eq`) so it never clobbers a newer recovery. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
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)
src/common/snapshots.rs (1)
192-196:⚠️ Potential issue | 🟠 Major | 🏗️ Heavy liftPass the same progress handle through the restore path.
restore_shard_snapshot()still switches toUnpacking/Restoringby re-readingactive_recoveriesfromshard_id. If recovery A is still unwinding when recovery B replaces the map entry for the same shard, A will advance B’s comment even thoughDropcorrectly avoids deleting it. Threadrecovery_guard.progress()(or the guard itself) into the restore path and update that exactArcinstead of resolving it again from the map.Also applies to: 291-304
🤖 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/common/snapshots.rs` around lines 192 - 196, The restore path must reuse the exact progress handle from start_shard_recovery instead of re-reading active_recoveries by shard_id; thread the recovery_guard (or recovery_guard.progress()) returned by collection.shards_holder().read().await.start_shard_recovery(shard_id) into restore_shard_snapshot (and the unwind/restore branches around lines ~291-304) and update that Arc directly rather than looking it up again in the active_recoveries map, so concurrent recovery A cannot advance recovery B's progress; ensure Drop semantics remain unchanged.
🧹 Nitpick comments (1)
lib/collection/src/shards/shard_holder/mod.rs (1)
99-103: ⚡ Quick winMark the guard as
#[must_use].Ignoring
start_shard_recovery()currently compiles, but the temporary guard would be dropped at the end of the statement and silently disable tracking. Adding#[must_use]here makes that regression much harder to reintroduce.♻️ Proposed fix
+#[must_use = "hold this guard for the full recovery so the progress entry stays registered"] pub struct ShardRecoveryGuard { active_recoveries: Arc<Mutex<HashMap<ShardId, Arc<Mutex<RecoveryProgress>>>>>, shard_id: ShardId, progress: Arc<Mutex<RecoveryProgress>>,🤖 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 `@lib/collection/src/shards/shard_holder/mod.rs` around lines 99 - 103, Add the Rust attribute #[must_use] to the ShardRecoveryGuard struct declaration so the compiler warns when a temporary guard returned by start_shard_recovery() is dropped immediately; locate the struct named ShardRecoveryGuard in mod.rs and put #[must_use] above its definition to enforce the usage check.
🤖 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.
Outside diff comments:
In `@src/common/snapshots.rs`:
- Around line 192-196: The restore path must reuse the exact progress handle
from start_shard_recovery instead of re-reading active_recoveries by shard_id;
thread the recovery_guard (or recovery_guard.progress()) returned by
collection.shards_holder().read().await.start_shard_recovery(shard_id) into
restore_shard_snapshot (and the unwind/restore branches around lines ~291-304)
and update that Arc directly rather than looking it up again in the
active_recoveries map, so concurrent recovery A cannot advance recovery B's
progress; ensure Drop semantics remain unchanged.
---
Nitpick comments:
In `@lib/collection/src/shards/shard_holder/mod.rs`:
- Around line 99-103: Add the Rust attribute #[must_use] to the
ShardRecoveryGuard struct declaration so the compiler warns when a temporary
guard returned by start_shard_recovery() is dropped immediately; locate the
struct named ShardRecoveryGuard in mod.rs and put #[must_use] above its
definition to enforce the usage check.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI (base), Organization UI (inherited)
Review profile: CHILL
Plan: Pro
Run ID: 5ecfbd2f-4afe-47f4-8c1a-ee7a4627cf55
📒 Files selected for processing (2)
lib/collection/src/shards/shard_holder/mod.rssrc/common/snapshots.rs
Move ShardRecoveryGuard out of the large shard_holder/mod.rs into a dedicated recovery_guard.rs, and replace the verbose `Arc<Mutex<HashMap<ShardId, Arc<Mutex<RecoveryProgress>>>>>` type with a dedicated `ActiveRecoveries` struct that encapsulates start/comment/ set_stage/remove operations. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@lib/collection/src/shards/shard_holder/recovery_guard.rs`:
- Around line 44-49: The current set_stage/shard update looks up the progress by
shard_id from self.recoveries and mutates whatever entry is current, which
allows an older guard to accidentally modify a newer recovery; instead update
the RecoveryStage via the existing ShardRecoveryGuard's own progress instance so
the guard's pointer is used rather than re-resolving the map. Change callers to
call a method on ShardRecoveryGuard (or add a new guard.set_stage(stage)) that
locks and updates the guard's internal progress, and remove or deprecate
ActiveRecoveries::set_stage(shard_id, ...) so set_stage no longer does
self.recoveries.lock().get(&shard_id) but operates through the guard's stored
progress to avoid cross-recovery races; keep remove() pointer-guard logic as-is.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI (base), Organization UI (inherited)
Review profile: CHILL
Plan: Pro
Run ID: 2ef6c963-12df-4452-884d-d2794ffe4eef
📒 Files selected for processing (2)
lib/collection/src/shards/shard_holder/mod.rslib/collection/src/shards/shard_holder/recovery_guard.rs
`ActiveRecoveries::remove` is pointer-guarded so an unwinding recovery cannot delete a newer recovery's entry, but stage updates still resolved the progress entry by shard id. If recovery A is still unwinding after recovery B started for the same shard, A's late `set_stage` would mutate B's progress and resurrect incorrect transfer comments. Thread the guard's own progress handle (`RecoveryProgressHandle`) through `recover_shard_snapshot_impl` -> `Collection::restore_shard_snapshot` -> `ShardHolder::restore_shard_snapshot`, so the Unpacking/Restoring stages (and Downloading, via `ShardRecoveryGuard::set_stage`) update that exact recovery's progress. Direct API recoveries, which are not tracked as transfer-side recoveries, pass `None`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Cursor <[email protected]>
…9237) * Fix stale snapshot transfer recovery comment on failure/cancellation The transfer status "comment" prefers the destination-side recovery progress over the sender task status. Recovery progress was tracked in `active_recoveries` and only removed via `finish_shard_recovery`, which was called on the success path only. If `recover_shard_snapshot` returned early - clearing the local shard, downloading the snapshot, checksum mismatch, or cancellation (the future is spawned with `spawn_cancel_on_drop`) - the entry leaked. A leaked entry keeps reporting its last stage with an ever-growing elapsed time (computed live from a fixed `Instant`), so subsequent transfers for the same shard show stale timings until the next recovery overwrites the entry or the node restarts. Replace the manual start/finish pair with an RAII `ShardRecoveryGuard` that removes the progress entry on drop, covering every exit path including early returns and cancellation. The guard only removes its own entry (checked via `Arc::ptr_eq`) so it never clobbers a newer recovery. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Extract recovery tracking into dedicated recovery_guard module Move ShardRecoveryGuard out of the large shard_holder/mod.rs into a dedicated recovery_guard.rs, and replace the verbose `Arc<Mutex<HashMap<ShardId, Arc<Mutex<RecoveryProgress>>>>>` type with a dedicated `ActiveRecoveries` struct that encapsulates start/comment/ set_stage/remove operations. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Bind recovery stage updates to the guard instance, not shard id `ActiveRecoveries::remove` is pointer-guarded so an unwinding recovery cannot delete a newer recovery's entry, but stage updates still resolved the progress entry by shard id. If recovery A is still unwinding after recovery B started for the same shard, A's late `set_stage` would mutate B's progress and resurrect incorrect transfer comments. Thread the guard's own progress handle (`RecoveryProgressHandle`) through `recover_shard_snapshot_impl` -> `Collection::restore_shard_snapshot` -> `ShardHolder::restore_shard_snapshot`, so the Unpacking/Restoring stages (and Downloading, via `ShardRecoveryGuard::set_stage`) update that exact recovery's progress. Direct API recoveries, which are not tracked as transfer-side recoveries, pass `None`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Allow too_many_arguments on recover_shard_snapshot_impl Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: root <[email protected]> Co-authored-by: Cursor <[email protected]>
Problem
When transferring shard snapshots between replicas, the transfer status "comment" can report old timings for new transfers, especially after a transfer was cancelled or failed.
The comment is assembled in
ShardHolder::get_shard_transfer_info, which prefers the destination-side recovery comment over the sender's task status:Recovery progress lives in
active_recoveries, keyed by shard id. Entries were added bystart_shard_recoveryand removed only byfinish_shard_recovery, which inrecover_shard_snapshot(src/common/snapshots.rs) was called on the success path only. The acknowledged contract — "Once recovery tracking starts,finish_shard_recoverymust run on all paths" — was violated by two early-return paths:clear_local_shard_for_snapshot_recovery(shard_id).await?cancel_on_token(cancel.clone(), download_task).await??(download error/checksum mismatch, or cancellation — the future is spawned viaspawn_cancel_on_drop)On any of those, the
RecoveryProgressentry leaked. Becausestage_elapsed_secsis computed live from a fixedInstant, a leaked entry keeps reporting its last stage with an ever-growing elapsed time (e.g.Downloading (3617.42s)) for subsequent transfers of the same shard, until the next recovery overwrites it or the node restarts.Fix
Replace the manual
start_shard_recovery/finish_shard_recoverypair with an RAIIShardRecoveryGuardreturned bystart_shard_recovery. Dropping the guard removes the progress entry, which happens on every exit path — success, error, and cancellation. The guard only removes its own entry (verified withArc::ptr_eq), so it never clobbers a newer recovery that has already replaced it.Test plan
cargo check -p collectionandcargo check --bin qdrantpasscargo clippy -p collection --libclean🤖 Generated with Claude Code