Skip to content

Commit

Permalink
Fix a rare deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatson committed Aug 25, 2024
1 parent adb17e4 commit c12fcd7
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions crates/librqbit/src/torrent_state/live/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ impl TorrentStateLive {

fn on_piece_completed(&self, id: ValidPieceIndex) -> anyhow::Result<()> {
let mut g = self.lock_write("on_piece_completed");
let g = &mut **g;
let chunks = g.get_chunks_mut()?;
let locked = &mut **g;
let chunks = locked.get_chunks_mut()?;

// if we have all the pieces of the file, reopen it read only
for (idx, file_info) in self
Expand All @@ -743,20 +743,22 @@ impl TorrentStateLive {
self.streams
.wake_streams_on_piece_completed(id, &self.torrent.lengths);

g.unflushed_bitv_bytes += self.torrent.lengths.piece_length(id) as u64;
if g.unflushed_bitv_bytes >= FLUSH_BITV_EVERY_BYTES {
g.try_flush_bitv()
locked.unflushed_bitv_bytes += self.torrent.lengths.piece_length(id) as u64;
if locked.unflushed_bitv_bytes >= FLUSH_BITV_EVERY_BYTES {
locked.try_flush_bitv()
}

let chunks = g.get_chunks()?;
let chunks = locked.get_chunks()?;
if chunks.is_finished() {
if chunks.get_selected_pieces()[id.get_usize()] {
g.try_flush_bitv();
locked.try_flush_bitv();
info!("torrent finished downloading");
}
self.finished_notify.notify_waiters();

if !self.has_active_streams_unfinished_files(g) {
if !self.has_active_streams_unfinished_files(locked) {
// prevent deadlocks.
drop(g);
// There is not poing being connected to peers that have all the torrent, when
// we don't need anything from them, and they don't need anything from us.
self.disconnect_all_peers_that_have_full_torrent();
Expand Down

0 comments on commit c12fcd7

Please sign in to comment.