Description
The stdout/err APIs guarantee that they're globally synchronized:
Each handle returned is a reference to a shared global buffer whose access is synchronized via a mutex. If you need more explicit control over locking, see the Stdout::lock method.
This can be relied on to write whole lines at a time, e.g. for json-lines style logging where multiple threads shouldn't interleave their output. It can be ok if some random other library or thread outputs non-JSON output, but it's important that it's not emitted in the middle of a line that's currently being emitted.
The combination of impl AsFd for Stdout
, try_clone_to_owned()
and impl From<OwnedFd> for File
can then be used to break that guarantee. AsRawFd
is somewhat less of an issue because any use of the created RawFd
should be unsafe.
Possible solutions:
- weaken the guarantees
- deprecate or remove the impls
- add "don't do that" warnings
Related: rust-lang/libs-team#148 (comment) and following.