mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
net: deprecate {TcpStream,TcpSocket}::set_linger (#7752)
This commit is contained in:
@@ -419,6 +419,17 @@ impl TcpSocket {
|
|||||||
///
|
///
|
||||||
/// If `SO_LINGER` is not specified, and the socket is closed, the system handles the call in a
|
/// If `SO_LINGER` is not specified, and the socket is closed, the system handles the call in a
|
||||||
/// way that allows the process to continue as quickly as possible.
|
/// way that allows the process to continue as quickly as possible.
|
||||||
|
///
|
||||||
|
/// This option is deprecated because setting `SO_LINGER` on a socket used with Tokio is always
|
||||||
|
/// incorrect as it leads to blocking the thread when the socket is closed. For more details,
|
||||||
|
/// please see:
|
||||||
|
///
|
||||||
|
/// > Volumes of communications have been devoted to the intricacies of `SO_LINGER` versus
|
||||||
|
/// > non-blocking (`O_NONBLOCK`) sockets. From what I can tell, the final word is: don't do
|
||||||
|
/// > it. Rely on the `shutdown()`-followed-by-`read()`-eof technique instead.
|
||||||
|
/// >
|
||||||
|
/// > From [The ultimate `SO_LINGER` page, or: why is my tcp not reliable](https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable)
|
||||||
|
#[deprecated = "`SO_LINGER` causes the socket to block the thread on drop"]
|
||||||
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
|
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
|
||||||
self.inner.set_linger(dur)
|
self.inner.set_linger(dur)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1278,9 +1278,20 @@ impl TcpStream {
|
|||||||
/// If `SO_LINGER` is not specified, and the stream is closed, the system handles the call in a
|
/// If `SO_LINGER` is not specified, and the stream is closed, the system handles the call in a
|
||||||
/// way that allows the process to continue as quickly as possible.
|
/// way that allows the process to continue as quickly as possible.
|
||||||
///
|
///
|
||||||
|
/// This option is deprecated because setting `SO_LINGER` on a socket used with Tokio is
|
||||||
|
/// always incorrect as it leads to blocking the thread when the socket is closed. For more
|
||||||
|
/// details, please see:
|
||||||
|
///
|
||||||
|
/// > Volumes of communications have been devoted to the intricacies of `SO_LINGER` versus
|
||||||
|
/// > non-blocking (`O_NONBLOCK`) sockets. From what I can tell, the final word is: don't
|
||||||
|
/// > do it. Rely on the `shutdown()`-followed-by-`read()`-eof technique instead.
|
||||||
|
/// >
|
||||||
|
/// > From [The ultimate `SO_LINGER` page, or: why is my tcp not reliable](https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable)
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
|
/// # #![allow(deprecated)]
|
||||||
/// use tokio::net::TcpStream;
|
/// use tokio::net::TcpStream;
|
||||||
///
|
///
|
||||||
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
|
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
@@ -1290,6 +1301,7 @@ impl TcpStream {
|
|||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[deprecated = "`SO_LINGER` causes the socket to block the thread on drop"]
|
||||||
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
|
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
|
||||||
socket2::SockRef::from(self).set_linger(dur)
|
socket2::SockRef::from(self).set_linger(dur)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -181,6 +181,7 @@ mod linux {
|
|||||||
use std::{net, thread};
|
use std::{net, thread};
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
#[expect(deprecated)] // set_linger is deprecated
|
||||||
fn poll_hup() {
|
fn poll_hup() {
|
||||||
let addr = assert_ok!("127.0.0.1:0".parse());
|
let addr = assert_ok!("127.0.0.1:0".parse());
|
||||||
let mut srv = assert_ok!(TcpListener::bind(&addr));
|
let mut srv = assert_ok!(TcpListener::bind(&addr));
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ async fn shutdown() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
#[expect(deprecated)] // set_linger is deprecated
|
||||||
async fn shutdown_after_tcp_reset() {
|
async fn shutdown_after_tcp_reset() {
|
||||||
let srv = assert_ok!(TcpListener::bind("127.0.0.1:0").await);
|
let srv = assert_ok!(TcpListener::bind("127.0.0.1:0").await);
|
||||||
let addr = assert_ok!(srv.local_addr());
|
let addr = assert_ok!(srv.local_addr());
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ async fn bind_before_connect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
#[expect(deprecated)] // set_linger is deprecated
|
||||||
async fn basic_linger() {
|
async fn basic_linger() {
|
||||||
// Create server
|
// Create server
|
||||||
let addr = assert_ok!("127.0.0.1:0".parse());
|
let addr = assert_ok!("127.0.0.1:0".parse());
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ use std::time::Duration;
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
#[cfg_attr(miri, ignore)] // No `socket` on miri.
|
#[cfg_attr(miri, ignore)] // No `socket` on miri.
|
||||||
|
#[expect(deprecated)] // set_linger is deprecated
|
||||||
async fn set_linger() {
|
async fn set_linger() {
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user