-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BufWriter documentation should warn about ignored errors on drop #37045
Comments
Duplicate of #32677 |
Ah, didn't notice that one. Well, if you want a documentation change until that one gets sorted out, let me know. |
I'm going to close this in favor of #32677. |
Mark-Simulacrum
added a commit
to Mark-Simulacrum/rust
that referenced
this issue
Jul 12, 2017
…bnik Add warning to BufWriter documentation When using `BufWriter`, it is very easy to unintentionally ignore errors, because errors which occur when flushing buffered data when the `BufWriter` is dropped are ignored. This has been noted in a couple places: rust-lang#32677, rust-lang#37045. There has been some discussion about how to fix this problem in rust-lang#32677, but no solution seems likely to land in the near future. For now, anyone who wishes to have robust error handling must remember to manually call `flush()` on a `BufWriter` before it is dropped. Until a permanent fix is in place, it seems worthwhile to add a warning to that effect to the documentation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BufWriter
callsflush_buf()
in its destructor, which writes out the contents of its internal buffer to the underlying writer. The destructor ignores any errors that happen when flushing the buffer. This makes it easy to accidentally forget to handle such errors:The code above outputs
It's tempting (at least to me) to think of
BufWriter
as preserving the semantics of the underlying writer, while offering possible performance improvements. It doesn't quite though, and it's unfortunately easy to forget to callflush()
, and to ignore errors when usingBufWriter
, as in functionf2()
above.It seems like a warning in the
BufWriter
documentation could be helpful here. Right now it saysPerhaps it could be changed to something like
If a change along these lines is of interest, I'll make a pull request.
The text was updated successfully, but these errors were encountered: