Search code examples
rustreadlineeof

Why does reading a line of input from /dev/zero make my computer unresponsive?


When building this code:

use std::io;

fn main() {
    let mut guess = String::new();
    let res = io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");
}

and then executing:

cargo run < /dev/zero

My computer (under Ubuntu 20) becomes unresponsive.

I have read the manual page of the readline function and this behavior doesn't seem normal.

What am I missing?


Solution

  • /dev/zero is a never-ending stream of NUL bytes. read_line stops when it reads a newline symbol. So, this function will never return and will try to infinitely expand the buffer. The reason your computer becomes unresponsive is likely because it starts swapping heavily, in order to accommodate the ever-growing buffer.

    read_line() is definitely not what you want. Try reading a fixed amount of bytes instead?