Skip to content

Commit

Permalink
fix race between block initialization and receiver disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Feb 25, 2024
1 parent a95d1a5 commit d23c05d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion crossbeam-channel/src/flavors/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ impl<T> Channel<T> {
block = self.head.block.load(Ordering::Acquire);
}
}

unsafe {
// Drop all messages between head and tail and deallocate the heap-allocated blocks.
while head >> SHIFT != tail >> SHIFT {
Expand All @@ -624,8 +625,16 @@ impl<T> Channel<T> {
drop(Box::from_raw(block));
}
}

head &= !MARK_BIT;
self.head.block.store(ptr::null_mut(), Ordering::Release);
let new_block = self.head.block.swap(ptr::null_mut(), Ordering::AcqRel);

// If the block was just initalized before the sender saw the MARK_BIT,
// there are no messages but we have to deallocate the block
if block.is_null() && tail >> SHIFT == 0 && !new_block.is_null() {
unsafe { drop(Box::from_raw(new_block)) };
}

self.head.index.store(head, Ordering::Release);
}

Expand Down

0 comments on commit d23c05d

Please sign in to comment.