rust-lang / rust-lang/rust

`coroutine_clone` causes memory leaks, panics, and UB

Open
#152,463 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coroutines C-bug F-coroutine_clone F-coroutines I-unsound requires-nightly
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I was noticing some memory leaks when I cloned coroutines and managed to turn it into a panic:

#![feature(coroutines, coroutine_trait, coroutine_clone)]
use std::{ops::Coroutine, pin::Pin};

#[derive(Clone)]
enum Foo {
    Bar(Vec<u8>),
}

fn create_coro() -> impl Coroutine<()> + Clone {
    #[coroutine]
    || match Foo::Bar(vec![0_u8]) {
        Foo::Bar(var) => {
            drop(var);
            yield;
        }
    }
}

fn main() {
    let mut coro = create_coro();
    Pin::new(&mut coro).resume(());
    let _ = coro.clone();
}

This code looks reasonable to me but running it returns:

thread 'main' (32000) panicked at C:\Users\zinfo\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\ptr\mod.rs:527:5:
unsafe precondition(s) violated: ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null and the specified memory ranges do not overlap

This indicates a bug in the program. This Undefined Behavior check is optional, and cannot be relied on for safety.
Meta

rustc --version --verbose:

rustc 1.95.0-nightly (18d13b533 2026-02-09)
binary: rustc
commit-hash: 18d13b5332916ffca8eadb9106d54b5b434e9978
commit-date: 2026-02-09
host: x86_64-pc-windows-msvc
release: 1.95.0-nightly
LLVM version: 22.1.0
Backtrace

stack backtrace:
   0: std::panicking::panic_handler
             at /rustc/18d13b5332916ffca8eadb9106d54b5b434e9978/library\std\src\panicking.rs:689
   1: core::panicking::panic_nounwind_fmt
             at /rustc/18d13b5332916ffca8eadb9106d54b5b434e9978/library\core\src\intrinsics\mod.rs:2434
   2: core::ptr::copy_nonoverlapping::precondition_check
             at C:\Users\zinfo\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ub_checks.rs:73
   3: alloc::slice::impl$0::to_vec_in::impl$1::to_vec<u8,alloc::alloc::Global>
             at C:\Users\zinfo\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\alloc\src\slice.rs:454
   4: alloc::vec::impl$12::clone<u8,alloc::alloc::Global>
             at C:\Users\zinfo\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\alloc\src\vec\mod.rs:3684
   5: coroutine_tests::impl$0::clone
             at .\src\main.rs:6
   6: core::clone::Clone::clone<enum2$<coroutine_tests::create_coro::coroutine_env$0> >
             at C:\Users\zinfo\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\clone.rs:236
   7: coroutine_tests::main
             at .\src\main.rs:22
   8: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at C:\Users\zinfo\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:250

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example with the reported nightly toolchain and inspect the coroutine_clone behavior reached from core::clone::Clone::clone. Trace the interaction with alloc::vec::Vec::clone, alloc::slice::to_vec, and core::ptr::copy_nonoverlapping, using the supplied backtrace and src/main.rs example as entry points. Done means cloning the resumed coroutine no longer leaks, panics, or invokes undefined behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.