How can I open stdin nonblocking, while leaving stdout blocking?


I’m writing a text editor that uses a raw TTY for user input. I’m also using tokio to process this input in an asynchronous manner. However, I’ve noticed that setting stdin nonblocking also sets stdout to be nonblocking.

How can I set only stdin to be nonblocking in Rust in a semi-portable way (MacOS and Linux)? It seems like I need to open a separate file descriptor for stdin and stdout, and then fcntl O_NONBLOCK on the new descriptor, but I’m not sure how best to do that.


You could try opening /dev/tty, which will be the process’ controlling terminal. I know that works on Linux, and it appears to be present on OSX as well. (I just tried echo "HELLO" >/dev/tty on an OSX ssh session and it worked.)