Skip to content

io::stdin().read(buf) hangs when stdin is pipe on Linux #10237

Closed
@stepancheg

Description

@stepancheg

Rust stdlib hangs instead of reporting EOF when stdin is pipe.

Testing with script:

echo aaa | ./program

C program, does not hang, exits properly:

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>

int main() {
    for (;;) {
        char buf[1];
        ssize_t r = read(STDIN_FILENO, buf, 1);
        if (r == 0) {
            break;
        } else if (r < 0) {
            perror("read");
            exit(1);
        } else {
            printf("%d\n", (int) buf[0]);
        }
    }
    return 0;
}

Rust program hangs on Linux, exits properly on OSX:

use std::rt::io::stdin;
use std::rt::io::Reader;

fn main() {
    let mut r = stdin();
    loop {
        let b = r.read_byte();
        if b.is_none() {
            break;
        }
        println!("{}", b.unwrap());
    }
}

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