Skip to content

Occasional memory leak in mpsc channels #121582

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
RalfJung opened this issue Feb 25, 2024 · 2 comments · Fixed by #121646
Closed

Occasional memory leak in mpsc channels #121582

RalfJung opened this issue Feb 25, 2024 · 2 comments · Fixed by #121646
Labels
I-memleak Issue: Runtime memory leak without `mem::forget`. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue

Comments

@RalfJung
Copy link
Member

RalfJung commented Feb 25, 2024

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

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 25, 2024
@RalfJung
Copy link
Member Author

RalfJung commented Feb 25, 2024

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.

@ibraheemdev
Copy link
Member

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

@jieyouxu jieyouxu added I-memleak Issue: Runtime memory leak without `mem::forget`. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 26, 2024
@bors bors closed this as completed in fc3800f Feb 26, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
I-memleak Issue: Runtime memory leak without `mem::forget`. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants