Skip to content

Memory leak in LocalWaker #122180

Closed
Closed
@RalfJung

Description

@RalfJung

The following code leaks memory:

#![feature(local_waker)]
use std::rc::Rc;
use std::task::LocalWake;
use std::task::LocalWaker;

fn main() {
    struct NoopWaker;

    impl LocalWake for NoopWaker {
        fn wake(self: Rc<Self>) {}
    }

    let _waker = LocalWaker::from(Rc::new(NoopWaker));
}

Miri error:

error: memory leaked: alloc1518 (Rust heap, size: 16, align: 8), allocated here:
   --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:100:9
    |
100 |         __rust_alloc(layout.size(), layout.align())
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: BACKTRACE:
    = note: inside `std::alloc::alloc` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:100:9: 100:52
    = note: inside `std::alloc::Global::alloc_impl` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:183:73: 183:86
    = note: inside `<std::alloc::Global as std::alloc::Allocator>::allocate` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:243:9: 243:39
    = note: inside `alloc::alloc::exchange_malloc` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:332:11: 332:34
    = note: inside `std::boxed::Box::<std::rc::RcBox<main::NoopWaker>>::new` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:218:9: 218:20
    = note: inside `std::rc::Rc::<main::NoopWaker>::new` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/rc.rs:398:27: 398:94
note: inside `main`
   --> src/main.rs:13:35
    |
13  |     let _waker = LocalWaker::from(Rc::new(NoopWaker));
    |                                   ^^^^^^^^^^^^^^^^^^

The equivalent code with Waker is fine, so I think this is a bug.

Cc @dtolnay @tvallotton

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-memleakIssue: Runtime memory leak without `mem::forget`.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions