From 5e706a0a59011441dea6f619fab314d7e9d60b9a Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Mon, 26 Feb 2024 08:24:26 -0500 Subject: [PATCH] Fix race between block initialization and receiver disconnection (#1084) --- crossbeam-channel/src/flavors/list.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crossbeam-channel/src/flavors/list.rs b/crossbeam-channel/src/flavors/list.rs index f6a79f719..6d7ba7ebf 100644 --- a/crossbeam-channel/src/flavors/list.rs +++ b/crossbeam-channel/src/flavors/list.rs @@ -583,7 +583,7 @@ impl Channel { } let mut head = self.head.index.load(Ordering::Acquire); - let mut block = self.head.block.load(Ordering::Acquire); + let mut block = self.head.block.swap(ptr::null_mut(), Ordering::AcqRel); // If we're going to be dropping messages we need to synchronize with initialization if head >> SHIFT != tail >> SHIFT { @@ -596,6 +596,7 @@ impl Channel { 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 { @@ -623,7 +624,6 @@ impl Channel { } } head &= !MARK_BIT; - self.head.block.store(ptr::null_mut(), Ordering::Release); self.head.index.store(head, Ordering::Release); }