Skip to content

Occasional memory leak in mpsc channels #121582

Closed

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

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 25, 2024
  2. RalfJung commented on Feb 25, 2024

    @RalfJung
    MemberAuthor

    I think this is not recent, it's just hard to trigger. I'll try reproducing on some older versions of rustc+Miri:

    • 1244dca7fd35293b5c88d51efea781cb566d8cfe (Jan 24th): seed 1870
    • cad4f40616ccf4ada87506ec5437b2ff41af2671 (Nov 22nd, 2023): seed 1020

    We can't go back much further since it seems we're not keeping the artifacts long enough for that?

    So probably this existed ever since #93563 landed.

  3. ibraheemdev commented on Feb 25, 2024

    @ibraheemdev
    Member

    Reproduced this with crossbeam-channel 0.5.11 and miri 381d699`.

  4. added
    I-memleakIssue: Runtime memory leak without `mem::forget`.
    S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 26, 2024
  5. added a commit that references this issue on Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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