Skip to content

Segfault in safe code that spawns a thread with an infinite loop when optimized #39753

Closed
@shepmaster

Description

@shepmaster

Summary

  • macOS: Segfaults with Rust bae454e
  • Linux: Panics and Valgrind reports errors with Rust ba7cf7c

Complete original report

Originally reported on Stack Overflow.

use std::thread;

fn main() {
    thread::spawn(|| {
        let a = 2;
        loop {
            a*a;
        }
    }).join();
}
$ rustc -O inf.rs
warning: unused result which must be used, #[warn(unused_must_use)] on by default
 --> inf.rs:4:5
  |
4 |       thread::spawn(|| {
  |  _____^ starting here...
5 | |         let a = 2;
6 | |         loop {
7 | |             a*a;
8 | |         }
9 | |     }).join();
  | |______________^ ...ending here

$ ./inf
Segmentation fault: 11
$ rustc --version --verbose
rustc 1.15.1 (021bd294c 2017-02-08)
binary: rustc
commit-hash: 021bd294c039bd54aa5c4aa85bcdffb0d24bc892
commit-date: 2017-02-08
host: x86_64-apple-darwin
release: 1.15.1
LLVM version: 3.9

Adding an unwrap() does not change the behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions