Skip to content

std::thread::Builder::spawn() panics #78497

Open
@BartMassey

Description

@BartMassey

When trying to create a large number of threads, std::thread::Builder::spawn() can panic rather than returning an error.

I wrote this:

fn main() {
    let n: usize = std::env::args().nth(1).unwrap().parse().unwrap();

    let mut threads = Vec::new();
    for i in 0..n {
        let b = std::thread::Builder::new();
        let t = b.spawn(|| {
            std::thread::sleep(std::time::Duration::from_secs(3));
        }).unwrap_or_else(|e| {
            println!("{}: {}", i, e);
            std::process::exit(1);
        });
        threads.push(t);
    }
    println!("spawned");
    for t in threads {
        t.join().unwrap();
    }
    println!("joined");
}

When run as cargo run --release 10000 on my box this works as I expected: prints "spawned" in a half-second, "joined" three seconds after that. When run as cargo run --release 100000 I get

thread '<unnamed>' panicked at 'failed to set up alternative stack guard page', 16341: Resource temporarily unavailable (os error 11)
library/std/src/sys/unix/stack_overflow.rs:156:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5

From the documentation for Builder::spawn() I expected to get an Err back instead of the panic.

Meta

rustc --version --verbose:

rustc 1.47.0 (18bf6b4f0 2020-10-07)
binary: rustc
commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39
commit-date: 2020-10-07
host: x86_64-unknown-linux-gnu
release: 1.47.0
LLVM version: 11.0

My environment: current Debian Linux, 32GB memory.

Fails the same way on nightly 2020-10-25. No stack backtrace is available.

Metadata

Metadata

Assignees

Labels

A-threadArea: `std::thread`C-bugCategory: This is a bug.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