Skip to content

Occasional memory leak in mpsc channels #121582

Closed
@RalfJung

Description

@RalfJung

Running this test:

use std::sync::mpsc::channel;
use std::thread;

fn main() {
    let (tx, rx) = channel::<i32>();
    let _t = thread::spawn(move || {
        drop(rx);
    });
    let _ = thread::spawn(move || {
        tx.send(1).unwrap();
    })
    .join();
}

with many different seeds in Miri eventually shows:

error: memory leaked: alloc2567 (Rust heap, size: 504, align: 8), allocated here:
  --> /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/alloc/src/alloc.rs:98:9
   |
98 |         __rust_alloc(layout.size(), layout.align())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: inside `std::alloc::alloc` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/alloc/src/alloc.rs:98:9: 98:52
   = note: inside `std::alloc::Global::alloc_impl` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/alloc/src/alloc.rs:181:73: 181:86
   = note: inside `<std::alloc::Global as std::alloc::Allocator>::allocate` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/alloc/src/alloc.rs:241:9: 241:39
   = note: inside `alloc::alloc::exchange_malloc` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/alloc/src/alloc.rs:330:11: 330:34
   = note: inside `std::boxed::Box::<std::sync::mpmc::list::Block<i32>>::new` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/alloc/src/boxed.rs:218:9: 218:20
   = note: inside `std::sync::mpmc::list::Channel::<i32>::start_send` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sync/mpmc/list.rs:209:41: 209:68
   = note: inside `std::sync::mpmc::list::Channel::<i32>::send` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sync/mpmc/list.rs:402:17: 402:39
   = note: inside `std::sync::mpmc::Sender::<i32>::send` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sync/mpmc/mod.rs:128:41: 128:61
   = note: inside `std::sync::mpsc::Sender::<i32>::send` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sync/mpsc/mod.rs:613:9: 613:27
note: inside closure
  --> leak.rs:10:9
   |
10 |         tx.send(1).unwrap();
   |         ^^^^^^^^^^

This indicates that the allocation made here does not get freed properly:

let new = Box::into_raw(Box::new(Block::<T>::new()));

Specifically when running this with Miri 3fe10973bb6e9a01b280686534d0242da07f3ede, seed 139 causes the issue.

I don't think there is any way for this to be a false positive.

Cc @ibraheemdev @taiki-e @Amanieu

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-memleakIssue: Runtime memory leak without `mem::forget`.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions