From 9c6a0766be95fc197f4f30764a794eb4d50b649a Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Thu, 29 Feb 2024 00:56:31 -0500 Subject: [PATCH 1/2] 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 a1b275112a1ed..b6bae7dc50ce3 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 From 7c9fa952c3be1f7d0872fe9cd5f1f4a0327771b9 Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Thu, 29 Feb 2024 01:33:02 -0500 Subject: [PATCH 2/2] fix typos Co-authored-by: Ralf Jung --- library/std/src/sync/mpmc/list.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/std/src/sync/mpmc/list.rs b/library/std/src/sync/mpmc/list.rs index b6bae7dc50ce3..9e7148c716cda 100644 --- a/library/std/src/sync/mpmc/list.rs +++ b/library/std/src/sync/mpmc/list.rs @@ -547,9 +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 + // 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