diff --git a/Makefile b/Makefile index 4e967ad..6fb7214 100644 --- a/Makefile +++ b/Makefile @@ -10,4 +10,6 @@ build-release: cargo build --release install: build-release + $(MAKE) build-release + $(MAKE) sign-release cp target/release/rqbit "$(HOME)/bin/" \ No newline at end of file diff --git a/crates/librqbit/src/chunk_tracker.rs b/crates/librqbit/src/chunk_tracker.rs index 021409e..b2f5d9d 100644 --- a/crates/librqbit/src/chunk_tracker.rs +++ b/crates/librqbit/src/chunk_tracker.rs @@ -116,6 +116,13 @@ impl ChunkTracker { .unwrap() } + pub fn is_chunk_ready_to_upload(&self, chunk: &ChunkInfo) -> bool { + self.have + .get(chunk.piece_index.get() as usize) + .map(|b| *b) + .unwrap_or(false) + } + // return true if the whole piece is marked downloaded pub fn mark_chunk_downloaded( &mut self, diff --git a/crates/librqbit/src/peer_connection.rs b/crates/librqbit/src/peer_connection.rs index dadbdf0..53bf286 100644 --- a/crates/librqbit/src/peer_connection.rs +++ b/crates/librqbit/src/peer_connection.rs @@ -263,18 +263,22 @@ impl PeerConnection { } }; - let tx = self - .state - .locked - .read() - .peers - .clone_tx(peer_handle) - .ok_or_else(|| { + let tx = { + let g = self.state.locked.read(); + if !g.chunks.is_chunk_ready_to_upload(&chunk_info) { + anyhow::bail!( + "got request for a chunk that is not ready to upload. chunk {:?}", + &chunk_info + ); + } + + g.peers.clone_tx(peer_handle).ok_or_else(|| { anyhow::anyhow!( "peer {} died, dropping chunk that it requested", peer_handle ) - })?; + })? + }; // TODO: this is not super efficient as it does copying multiple times. // Theoretically, this could be done in the sending code, so that it reads straight into diff --git a/crates/librqbit/src/torrent_manager.rs b/crates/librqbit/src/torrent_manager.rs index 0bfe6f7..3c1bc78 100644 --- a/crates/librqbit/src/torrent_manager.rs +++ b/crates/librqbit/src/torrent_manager.rs @@ -12,7 +12,7 @@ use std::{ use anyhow::Context; use futures::{stream::FuturesUnordered, StreamExt}; -use log::{debug, error, info, warn}; +use log::{debug, info, warn}; use parking_lot::{Mutex, RwLock}; use reqwest::Url; use size_format::SizeFormatterBinary as SF;