From 9c6a0766be95fc197f4f30764a794eb4d50b649a Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Thu, 29 Feb 2024 00:56:31 -0500 Subject: [PATCH] document potential memory leak in unbounded channel --- library/std/src/sync/mpmc/list.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/std/src/sync/mpmc/list.rs b/library/std/src/sync/mpmc/list.rs index a1b275112a1..b6bae7dc50c 100644 --- a/library/std/src/sync/mpmc/list.rs +++ b/library/std/src/sync/mpmc/list.rs @@ -547,6 +547,9 @@ impl Channel { } let mut head = self.head.index.load(Ordering::Acquire); + // the channel may be uninitialized, so we have to swap to avoid overwriting any sender's attempts + // to initalize the first block before noticing that the receivers disconnected. late allocations + // will be deallocated by the sender in Drop 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