Skip to content

Fix stale snapshot transfer recovery comment on failure/cancellation - #9237

Merged
generall merged 4 commits into
devfrom
fix-stale-snapshot-transfer-comment
May 30, 2026
Merged

generall merged 4 commits into
devfrom
fix-stale-snapshot-transfer-comment

Conversation

@generall

Copy link
Copy Markdown
Member

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:

let recovery_comment = self.active_recoveries.lock().get(&target_shard)
    .and_then(|p| p.lock().format_comment());
let comment = recovery_comment.or_else(|| { /* sender task status */ });

Recovery progress lives in active_recoveries, keyed by shard id. Entries were added by start_shard_recovery and removed only by finish_shard_recovery, which in recover_shard_snapshot (src/common/snapshots.rs) was called on the success path only. The acknowledged contract — "Once recovery tracking starts, finish_shard_recovery must run on all paths" — was violated by two early-return paths:

  1. clear_local_shard_for_snapshot_recovery(shard_id).await?
  2. cancel_on_token(cancel.clone(), download_task).await?? (download error/checksum mismatch, or cancellation — the future is spawned via spawn_cancel_on_drop)

On any of those, the RecoveryProgress entry leaked. Because stage_elapsed_secs is computed live from a fixed Instant, 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_recovery pair with an RAII ShardRecoveryGuard returned by start_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 with Arc::ptr_eq), so it never clobbers a newer recovery that has already replaced it.

Test plan

  • cargo check -p collection and cargo check --bin qdrant pass
  • cargo clippy -p collection --lib clean

🤖 Generated with Claude Code

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]>

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

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 lift

Pass the same progress handle through the restore path.

restore_shard_snapshot() still switches to Unpacking/Restoring by re-reading active_recoveries from shard_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 though Drop correctly avoids deleting it. Thread recovery_guard.progress() (or the guard itself) into the restore path and update that exact Arc instead 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 win

Mark 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

📥 Commits

Reviewing files that changed from the base of the PR and between b0fe45d and ffa045f.

📒 Files selected for processing (2)
  • lib/collection/src/shards/shard_holder/mod.rs
  • src/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]>

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between ffa045f and b4e8866.

📒 Files selected for processing (2)
  • lib/collection/src/shards/shard_holder/mod.rs
  • lib/collection/src/shards/shard_holder/recovery_guard.rs

generall and others added 2 commits May 30, 2026 01:02
`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]>
@generall
generall merged commit e54b321 into dev May 30, 2026
15 checks passed
@generall
generall deleted the fix-stale-snapshot-transfer-comment branch May 30, 2026 14:34
timvisee pushed a commit that referenced this pull request Jun 3, 2026
…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]>
@qdrant qdrant deleted a comment from coderabbitai Bot Jun 3, 2026
@timvisee timvisee mentioned this pull request Jun 3, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Jul 10, 2026
6 tasks done
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.

2 participants