Skip to content

MPSC Sender channel not dropping buffer when all Receivers are dropped during a thread panic unwind #107466

Closed
@philipr-za

Description

@philipr-za

Code

I tried this code:

use std::sync::mpsc;
use std::thread;
use std::time::{Duration, Instant};

struct Foo {
    data: i32,
    start: Instant,
}

impl Drop for Foo {
    fn drop(&mut self) {
        println!(
            "Dropping Foo with data {} - {:?}",
            self.data,
            self.start.elapsed()
        );
    }
}

fn main() {
    let start = Instant::now();
    let (tx1, rx1) = mpsc::sync_channel(1);

    let _ = thread::spawn(move || {
        // Move the receiver into this thread.
        let _rx = rx1;
        thread::sleep(Duration::from_secs(1));
        panic!("expected panic");
        // Under 1.66.1 sender `tx1` and the item in it's buffer are dropped after this panic causes
        // the thread to unwind and the Receiver to be dropped
        // Under 1.67.0 the sender `tx1` and the data in it's buffer are not dropped here
    });

    tx1.send(Foo { data: 1, start }).unwrap();
    println!("Sends completed - {:?}", start.elapsed());

    thread::sleep(Duration::from_secs(5));
    println!("Ending - {:?}", start.elapsed());
    // Under 1.67.0 the sender `tx1` and the data in it's buffer are only dropped when the
    // whole process ends here
}

I expected to see this happen: I expected to see the struct Foo in the Sender buffer get dropped after the Receiver is dropped during the unwind of the panicking thread.

Instead, this happened: The data in the Sender buffer is not dropped until the whole process ends.

Version it worked on

It most recently worked on: 1.66.1

Version with regression

rustc --version --verbose:

rustc 1.67.0 (fc594f156 2023-01-24)
binary: rustc
commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
commit-date: 2023-01-24
host: x86_64-unknown-linux-gnu
release: 1.67.0
LLVM version: 15.0.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.P-lowLow priorityT-libsRelevant to the library team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions