diff --git a/crates/librqbit/src/chunk_tracker.rs b/crates/librqbit/src/chunk_tracker.rs index 4369963..8fc8c97 100644 --- a/crates/librqbit/src/chunk_tracker.rs +++ b/crates/librqbit/src/chunk_tracker.rs @@ -157,16 +157,20 @@ impl ChunkTracker { Some(true) } - pub fn mark_piece_broken(&mut self, index: ValidPieceIndex) -> bool { + pub fn mark_piece_broken_if_not_have(&mut self, index: ValidPieceIndex) { + if self + .have + .get(index.get() as usize) + .map(|r| *r) + .unwrap_or_default() + { + return; + } debug!("remarking piece={} as broken", index); self.needed_pieces.set(index.get() as usize, true); - self.chunk_status - .get_mut(self.lengths.chunk_range(index)) - .map(|s| { - s.fill(false); - true - }) - .unwrap_or_default() + if let Some(s) = self.chunk_status.get_mut(self.lengths.chunk_range(index)) { + s.fill(false); + } } pub fn mark_piece_downloaded(&mut self, idx: ValidPieceIndex) { diff --git a/crates/librqbit/src/torrent_state/live/mod.rs b/crates/librqbit/src/torrent_state/live/mod.rs index 83c459a..550864e 100644 --- a/crates/librqbit/src/torrent_state/live/mod.rs +++ b/crates/librqbit/src/torrent_state/live/mod.rs @@ -748,7 +748,7 @@ impl TorrentStateLive { .take() .context("bug: pausing already paused torrent")?; for piece_id in g.inflight_pieces.keys().copied() { - chunk_tracker.mark_piece_broken(piece_id); + chunk_tracker.mark_piece_broken_if_not_have(piece_id); } let have_bytes = chunk_tracker.calc_have_bytes(); let needed_bytes = chunk_tracker.calc_needed_bytes(); @@ -1494,7 +1494,7 @@ impl PeerHandler { self.state .lock_write("mark_piece_broken") .get_chunks_mut()? - .mark_piece_broken(chunk_info.piece_index); + .mark_piece_broken_if_not_have(chunk_info.piece_index); } }; Ok::<_, anyhow::Error>(())