Skip to content

Commit e0b878a

Browse files
Rollup merge of #158074 - valentynkt:docs/accept-transient-errors, r=Darksonn
Document transient connection errors from TcpListener::accept `TcpListener::accept` can return an error that belongs to a single incoming connection, not to the listener, for example a connection aborted by the peer before it could be accepted (`ConnectionAborted`). The listener stays usable, so a server looping over connections usually wants to log the error and keep accepting rather than treat it as fatal. This was not documented, and the `incoming` example treated every error as a failed connection. This implements the libs-team decision in #142557: document these transient errors instead of changing `accept` to retry them, since retrying would hide errors that some callers want to observe. Changes: - Add an `# Errors` section to `accept` describing this behavior, without listing specific error codes since some may be more permanent than others. - Note that `Interrupted` errors are retried internally on Unix. - Add the same pointer to `incoming` and `into_incoming`, which are `accept` in a loop. Addresses #142557. r? rust-lang/libs
2 parents d3ab997 + 1447519 commit e0b878a

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

library/std/src/net/tcp.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,29 @@ impl TcpListener {
876876
/// is established. When established, the corresponding [`TcpStream`] and the
877877
/// remote peer's address will be returned.
878878
///
879+
/// # Errors
880+
///
881+
/// Some errors this function returns do not indicate a problem with the
882+
/// listener itself, and a program serving a long-lived listener will
883+
/// usually want to handle them and keep accepting connections rather than
884+
/// treat them as fatal. These include, but are not limited to:
885+
///
886+
/// - An error specific to a single incoming connection that failed before
887+
/// it could be accepted, such as one aborted by the peer
888+
/// ([`ConnectionAborted`]). A later call may succeed immediately.
889+
/// - An error from reaching the per-process or system-wide open file
890+
/// descriptor limit. The call can be retried once other file descriptors
891+
/// have been closed, typically after a short delay.
892+
/// - An error from failing to allocate memory while accepting a connection
893+
/// ([`OutOfMemory`]).
894+
///
895+
/// Which errors can occur is platform-specific. On Unix, [`Interrupted`]
896+
/// errors are retried internally rather than being returned.
897+
///
898+
/// [`ConnectionAborted`]: io::ErrorKind::ConnectionAborted
899+
/// [`OutOfMemory`]: io::ErrorKind::OutOfMemory
900+
/// [`Interrupted`]: io::ErrorKind::Interrupted
901+
///
879902
/// # Examples
880903
///
881904
/// ```no_run
@@ -902,6 +925,11 @@ impl TcpListener {
902925
/// the peer's [`SocketAddr`] structure. Iterating over it is equivalent to
903926
/// calling [`TcpListener::accept`] in a loop.
904927
///
928+
/// # Errors
929+
///
930+
/// Each connection yielded by the iterator can fail for the same reasons as
931+
/// [`TcpListener::accept`]; see its documentation for details.
932+
///
905933
/// # Examples
906934
///
907935
/// ```no_run
@@ -937,6 +965,11 @@ impl TcpListener {
937965
/// the peer's [`SocketAddr`] structure. Iterating over it is equivalent to
938966
/// calling [`TcpListener::accept`] in a loop.
939967
///
968+
/// # Errors
969+
///
970+
/// Each connection yielded by the iterator can fail for the same reasons as
971+
/// [`TcpListener::accept`]; see its documentation for details.
972+
///
940973
/// # Examples
941974
///
942975
/// ```no_run

0 commit comments

Comments
 (0)