Skip to content

Commit 6e5b89a

Browse files
committed
docs: document close-on-exec behavior in socket try_clone methods
Document that `try_clone()` sets the close-on-exec flag on duplicated sockets, addressing issue #47946. On Unix, `F_DUPFD_CLOEXEC` is used to atomically duplicate the file descriptor with the close-on-exec flag set. On Windows, the duplicated socket is created without `HANDLE_FLAG_INHERIT`. This affects: - `TcpStream::try_clone()` - `TcpListener::try_clone()` - `UdpSocket::try_clone()` - `UnixStream::try_clone()` - `UnixListener::try_clone()` - `UnixDatagram::try_clone()`
1 parent defdb83 commit 6e5b89a

5 files changed

Lines changed: 57 additions & 0 deletions

File tree

library/std/src/net/tcp.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,18 @@ impl TcpStream {
253253
/// data, and options set on one stream will be propagated to the other
254254
/// stream.
255255
///
256+
/// # Platform-specific behavior
257+
///
258+
/// On Unix, this method uses `F_DUPFD_CLOEXEC` to duplicate the file descriptor
259+
/// atomically with the close-on-exec flag set. This means the duplicated socket
260+
/// will be automatically closed when calling `exec()`, and thus will not be
261+
/// available in child processes created via [`Command`].
262+
///
263+
/// On Windows, the duplicated socket is created without the `HANDLE_FLAG_INHERIT`
264+
/// flag, so it will similarly not be inherited by child processes.
265+
///
266+
/// [`Command`]: crate::process::Command
267+
///
256268
/// # Examples
257269
///
258270
/// ```no_run
@@ -806,6 +818,18 @@ impl TcpListener {
806818
/// object references. Both handles can be used to accept incoming
807819
/// connections and options set on one listener will affect the other.
808820
///
821+
/// # Platform-specific behavior
822+
///
823+
/// On Unix, this method uses `F_DUPFD_CLOEXEC` to duplicate the file descriptor
824+
/// atomically with the close-on-exec flag set. This means the duplicated socket
825+
/// will be automatically closed when calling `exec()`, and thus will not be
826+
/// available in child processes created via [`Command`].
827+
///
828+
/// On Windows, the duplicated socket is created without the `HANDLE_FLAG_INHERIT`
829+
/// flag, so it will similarly not be inherited by child processes.
830+
///
831+
/// [`Command`]: crate::process::Command
832+
///
809833
/// # Examples
810834
///
811835
/// ```no_run

library/std/src/net/udp.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ impl UdpSocket {
259259
/// object references. Both handles will read and write the same port, and
260260
/// options set on one socket will be propagated to the other.
261261
///
262+
/// # Platform-specific behavior
263+
///
264+
/// On Unix, this method uses `F_DUPFD_CLOEXEC` to duplicate the file descriptor
265+
/// atomically with the close-on-exec flag set. This means the duplicated socket
266+
/// will be automatically closed when calling `exec()`, and thus will not be
267+
/// available in child processes created via [`Command`].
268+
///
269+
/// On Windows, the duplicated socket is created without the `HANDLE_FLAG_INHERIT`
270+
/// flag, so it will similarly not be inherited by child processes.
271+
///
272+
/// [`Command`]: crate::process::Command
273+
///
262274
/// # Examples
263275
///
264276
/// ```no_run

library/std/src/os/unix/net/datagram.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ impl UnixDatagram {
261261
/// object references. Both handles can be used to accept incoming
262262
/// connections and options set on one side will affect the other.
263263
///
264+
/// This method uses `F_DUPFD_CLOEXEC` to duplicate the file descriptor
265+
/// atomically with the close-on-exec flag set. This means the duplicated socket
266+
/// will be automatically closed when calling `exec()`, and thus will not be
267+
/// available in child processes created via [`Command`].
268+
///
269+
/// [`Command`]: crate::process::Command
270+
///
264271
/// # Examples
265272
///
266273
/// ```no_run

library/std/src/os/unix/net/listener.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ impl UnixListener {
188188
/// object references. Both handles can be used to accept incoming
189189
/// connections and options set on one listener will affect the other.
190190
///
191+
/// This method uses `F_DUPFD_CLOEXEC` to duplicate the file descriptor
192+
/// atomically with the close-on-exec flag set. This means the duplicated socket
193+
/// will be automatically closed when calling `exec()`, and thus will not be
194+
/// available in child processes created via [`Command`].
195+
///
196+
/// [`Command`]: crate::process::Command
197+
///
191198
/// # Examples
192199
///
193200
/// ```no_run

library/std/src/os/unix/net/stream.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ impl UnixStream {
185185
/// data, and options set on one stream will be propagated to the other
186186
/// stream.
187187
///
188+
/// This method uses `F_DUPFD_CLOEXEC` to duplicate the file descriptor
189+
/// atomically with the close-on-exec flag set. This means the duplicated socket
190+
/// will be automatically closed when calling `exec()`, and thus will not be
191+
/// available in child processes created via [`Command`].
192+
///
193+
/// [`Command`]: crate::process::Command
194+
///
188195
/// # Examples
189196
///
190197
/// ```no_run

0 commit comments

Comments
 (0)