Skip to content

Segmentation fault when using spmc:::channel #63256

Closed
@ZiCog

Description

@ZiCog

After a few days studying Rust I have come to a road block with segmentation faults and other errors when running a simple program using an mpsc channel.

use std::thread;
use std::fs::File;
use std::io::{BufReader,BufRead};

pub fn main() {
    let (tx, rx) = spmc::channel();

    let mut handles = Vec::new();
    for n in 0..3 {
        let rx = rx.clone();
        handles.push(thread::spawn(move || {
            let mut word_count = 0;
            loop {
                let received = rx.recv();
                match received {
                    Ok(_word) => {
                        word_count = word_count + 1;
                    },
                    Err(_e) => {
                        println!("Reader {}: {}, ", n, word_count);
                        break;
                    }
                }
            }
        }));
    }

    handles.push(thread::spawn(move || {
        let file = File::open("/usr/share/dict/british-english-insane").unwrap();
        for line in BufReader::new(file).lines() {
            let word = line.unwrap();
            tx.send(word).unwrap();
        }
    }));

    for handle in handles {
        handle.join().unwrap();
    }

    println!("Done.");
}

This code should simply read all the lines of a Debian dictionary file and forward them to four threads. The expected result is like:

$ ./target/debug/threads-rust
Reader 2: 221485,
Reader 1: 217272,
Reader 0: 215518,
Done.

But mostly I get seg faults and other errors like so:

$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
    Running `target/debug/threads-rust`
munmap_chunk(): invalid pointer
Aborted (core dumped)
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
    Running `target/debug/threads-rust`
Segmentation fault (core dumped)
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
    Running `target/debug/threads-rust`
Reader 0: 654276,
Done.
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
    Running `target/debug/threads-rust`
munmap_chunk(): invalid pointer
Aborted (core dumped)
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
    Running `target/debug/threads-rust`
free(): invalid size
Aborted (core dumped)
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
    Running `target/debug/threads-rust`
double free or corruption (out)
Aborted (core dumped)

I get similar errors on a Raspberry Pi 3.

$ uname -a
Linux monster 4.4.0-17134-Microsoft #706-Microsoft Mon Apr 01 18:13:00 PST 2019 x86_64 GNU/Linux
$ cargo --version
cargo 1.36.0 (c4fcfb725 2019-05-15)
$ rustc --version --verbose
rustc 1.36.0 (a53f9df32 2019-07-03)
binary: rustc
commit-hash: a53f9df32fbb0b5f4382caaad8f1a46f36ea887c
commit-date: 2019-07-03
host: x86_64-unknown-linux-gnu
release: 1.36.0
LLVM version: 8.0    $ cargo  build
Compiling threads-rust v0.1.0 (/mnt/c/Users/zicog/conveqs/threads-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 1.72s

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-libs-apiRelevant to the library API 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