Rust file I/O and Linux fifo/pipe

I just discovered that if you write to a Linux fifo/pipe, you can miss data on the receiving end if you don't do a "flush" on a Writer. If I switched to a plain file--no data loss, but I had to add the "flush" call for the pipe to work reliably.

Does this seem right to everyone?

I think in general you don't have that guarantee for io::Write. For example, anything wrapped in BufWriter, even regular file, can be lazy and not get any writes until flush or drop of the writer.

It was surprising. I purposely close the files before the function ends also. At least I know now that flush makes a difference.

Drop of a file or bufwriter flushes too. I would expect drop of a pipe to flush it too.

On linux flushing std::fs::File is no-op. Dropping std::io::BufWriter tries to flush its buffer but ignore the error if occurred. Are they the exact types you're using? For example tokio::io::BufWriter discards its buffer without flush as you can't .await during drop.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.