Skip to content

Commit ed69db3

Browse files
authored
Rollup merge of rust-lang#124412 - RalfJung:io-safety, r=Amanieu
io safety: update Unix explanation to use `Arc` Fixes rust-lang#124384 Cc ```@jsgf```
2 parents 29b5199 + c60c646 commit ed69db3

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

Diff for: std/src/io/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266
//! its file descriptors with no operations being performed by any other part of the program.
267267
//!
268268
//! Note that exclusive ownership of a file descriptor does *not* imply exclusive ownership of the
269-
//! underlying kernel object that the file descriptor references (also called "file description" on
269+
//! underlying kernel object that the file descriptor references (also called "open file description" on
270270
//! some operating systems). File descriptors basically work like [`Arc`]: when you receive an owned
271271
//! file descriptor, you cannot know whether there are any other file descriptors that reference the
272272
//! same kernel object. However, when you create a new kernel object, you know that you are holding

Diff for: std/src/os/unix/io/mod.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
//! | Type | Analogous to |
1313
//! | ------------------ | ------------ |
1414
//! | [`RawFd`] | `*const _` |
15-
//! | [`BorrowedFd<'a>`] | `&'a _` |
16-
//! | [`OwnedFd`] | `Box<_>` |
15+
//! | [`BorrowedFd<'a>`] | `&'a Arc<_>` |
16+
//! | [`OwnedFd`] | `Arc<_>` |
1717
//!
1818
//! Like raw pointers, `RawFd` values are primitive values. And in new code,
1919
//! they should be considered unsafe to do I/O on (analogous to dereferencing
@@ -23,22 +23,31 @@
2323
//! either by adding `unsafe` to APIs that dereference `RawFd` values, or by
2424
//! using to `BorrowedFd` or `OwnedFd` instead.
2525
//!
26+
//! The use of `Arc` for borrowed/owned file descriptors may be surprising. Unix file descriptors
27+
//! are mere references to internal kernel objects called "open file descriptions", and the same
28+
//! open file description can be referenced by multiple file descriptors (e.g. if `dup` is used).
29+
//! State such as the offset within the file is shared among all file descriptors that refer to the
30+
//! same open file description, and the kernel internally does reference-counting to only close the
31+
//! underlying resource once all file descriptors referencing it are closed. That's why `Arc` (and
32+
//! not `Box`) is the closest Rust analogy to an "owned" file descriptor.
33+
//!
2634
//! Like references, `BorrowedFd` values are tied to a lifetime, to ensure
2735
//! that they don't outlive the resource they point to. These are safe to
2836
//! use. `BorrowedFd` values may be used in APIs which provide safe access to
2937
//! any system call except for:
3038
//!
3139
//! - `close`, because that would end the dynamic lifetime of the resource
32-
//! without ending the lifetime of the file descriptor.
40+
//! without ending the lifetime of the file descriptor. (Equivalently:
41+
//! an `&Arc<_>` cannot be `drop`ed.)
3342
//!
3443
//! - `dup2`/`dup3`, in the second argument, because this argument is
35-
//! closed and assigned a new resource, which may break the assumptions
44+
//! closed and assigned a new resource, which may break the assumptions of
3645
//! other code using that file descriptor.
3746
//!
38-
//! `BorrowedFd` values may be used in APIs which provide safe access to `dup`
39-
//! system calls, so types implementing `AsFd` or `From<OwnedFd>` should not
40-
//! assume they always have exclusive access to the underlying file
41-
//! description.
47+
//! `BorrowedFd` values may be used in APIs which provide safe access to `dup` system calls, so code
48+
//! working with `OwnedFd` cannot assume to have exclusive access to the underlying open file
49+
//! description. (Equivalently: `&Arc` may be used in APIs that provide safe access to `clone`, so
50+
//! code working with an `Arc` cannot assume that the reference count is 1.)
4251
//!
4352
//! `BorrowedFd` values may also be used with `mmap`, since `mmap` uses the
4453
//! provided file descriptor in a manner similar to `dup` and does not require
@@ -52,8 +61,10 @@
5261
//! take full responsibility for ensuring that safe Rust code cannot evoke
5362
//! undefined behavior through it.
5463
//!
55-
//! Like boxes, `OwnedFd` values conceptually own the resource they point to,
56-
//! and free (close) it when they are dropped.
64+
//! Like `Arc`, `OwnedFd` values conceptually own one reference to the resource they point to,
65+
//! and decrement the reference count when they are dropped (by calling `close`).
66+
//! When the reference count reaches 0, the underlying open file description will be freed
67+
//! by the kernel.
5768
//!
5869
//! See the [`io` module docs][io-safety] for a general explanation of I/O safety.
5970
//!

0 commit comments

Comments
 (0)