Skip to content

AsFd impl for Stdout/StdoutLock can violate locking guarantees of those types. #114140

Open
@the8472

Description

@the8472

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`C-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions