-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Does I/O safety forbid duping arbitrary file descriptors? #114167
Copy link
Copy link
Closed
Labels
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`Area: `std::io`, `std::fs`, `std::net` and `std::path`T-langRelevant to the language teamRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`Area: `std::io`, `std::fs`, `std::net` and `std::path`T-langRelevant to the language teamRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
I/O safety is summarized as
I would argue then that if a function is written
foo(fd: OwnedFd), then it should be guaranteed that it is the only one with read/write access to the underlying resource managed by this FD. (Of course if it is a file, others might be able to open the file again, but not all FDs are for files that have a name in the file system.) And this guarantee almost holds, except forOwnedFd::try_cloneandBorrowedFd::try_clone_to_owned. These will take a mere reference and return a fully ownedOwnedFd(viadup), so ourfoofunction has been foo'led and it is not actually exclusively reading/writing the underlying resource.I am somewhat surprised to discover this now; these functions were not mentioned in the original RFC. They fundamentally change what it means to hold an
OwnedFd, and I think not for the better. They are incompatible with the mental model I used when helping design and defend I/O safety.So, what shall we do about this?
OwnedFdcarefully. They are very weak (basically just "nobody will close this FD while you have it").Also, we should clarify what this means for I/O safety overall. Would it be sound to do
dup(rand())and return the result as anOwnedFd? I think the answer should be "absolutely not"; if I create an FD and don't share it with anyone I should be guaranteed that no duplicates exist. This would at least make it possible for a user crate to defineNonDupOwnedFd/NonDupBorrowedFdtypes that guarantee an absence of duplicates.Cc @sunfishcode