Skip to content

Command::output panics on Linux if more than 1024 file descriptors are open #40894

Closed
@matklad

Description

@matklad

Issue originally reported by @knsd.

Command::open uses select on Linux:

libc::select(max + 1, &mut read, ptr::null_mut(), ptr::null_mut(),
to read from both stderr and stdout. This won't always work, because select is limited to file descriptors less than FD_SETSIZE (1024).

Thus, if you raise ulimit -n to more than 1024, the following code panics

use std::fs::File;
use std::process::Command;

const FD_SETSIZE: usize = 1024;
const STDIO: usize = 3;
const EXE: &'static str = "/usr/bin/env";

fn main() {
    let mut files = Vec::new();

    for _ in 0..FD_SETSIZE - STDIO * 2 {
        let file = File::open(&EXE).unwrap();
        files.push(file)
    }

    let output = Command::new(EXE).output();
    println!("{:?}", output)
}
thread 'main' panicked at 'index out of bounds: the len is 16 but the index is 16', /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libcore/slice.rs:664

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-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