From c9af2596793258442d84a6a6c9bde978b2fe8b63 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 b74c8b33f..e90f62fb6 100644 --- a/crossbeam-channel/src/flavors/list.rs +++ b/crossbeam-channel/src/flavors/list.rs @@ -585,7 +585,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 { @@ -598,6 +598,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 { @@ -625,7 +626,6 @@ impl Channel { } } head &= !MARK_BIT; - self.head.block.store(ptr::null_mut(), Ordering::Release); self.head.index.store(head, Ordering::Release); }