Repository navigation
Closed
Labels
A-tokioArea: The main tokio crateArea: The main tokio crateC-bugCategory: This is a bug.Category: This is a bug.M-netModule: tokio/netModule: tokio/net
Description
Version
│ │ │ ├── tokio v1.50.0
│ │ │ │ └── tokio-macros v2.6.1 (proc-macro)
│ │ │ ├── tokio-util v0.7.18
│ │ │ │ └── tokio v1.50.0 (*)
│ │ ├── tokio v1.50.0 (*)
│ │ ├── tokio v1.50.0 (*)
│ ├── tokio v1.50.0 (*)
│ │ ├── tokio v1.50.0 (*)
│ │ ├── tokio-util v0.7.18 (*)
├── tokio v1.50.0 (*)
├── tokio-stream v0.1.18
│ └── tokio v1.50.0 (*)
├── tokio-util v0.7.18 (*)
│ │ ├── tokio v1.50.0 (*)
│ ├── tokio v1.50.0 (*)
│ ├── tokio-stream v0.1.18 (*)
│ ├── tokio v1.50.0 (*)
├── tokio v1.50.0 (*)
Platform
Linux 912834ca0917 6.17.8-orbstack-00308-g8f9c941121b1 #1 SMP PREEMPT Thu Nov 20 09:34:02 UTC 2025 aarch64 GNU/Linux
Description
UdpSocket::recv silently disregards any errors surfaced by the (Linux) kernel in SO_ERROR. This can cause sockets to hang instead of returning an appropriate error. For example, when calling recv after sending to a closed port.
use std::{io, net::SocketAddr};
use tokio::net::UdpSocket;
#[tokio::main]
async fn main() -> io::Result<()> {
// Bind a server socket then immediately drop it to get a port we know is closed
let server = UdpSocket::bind("127.0.0.1:0").await?;
let target: SocketAddr = server.local_addr()?;
drop(server);
let sock = UdpSocket::bind("127.0.0.1:0").await?;
sock.connect(target).await?;
sock.send(b"ping").await?;
let mut buf = [0; 8];
let result = sock.recv(&mut buf).await; // hangs infinitely
println!("{:?}", result);
Ok(())
}The standard library UdpSocket::recv implementation works correctly because it uses the Linux recv(2) syscall which will return any error that occurred.
use std::{io, net::{SocketAddr, UdpSocket}};
fn main() -> io::Result<()> {
// Bind a server socket then immediately drop it to get a port we know is closed
let server = UdpSocket::bind("127.0.0.1:0")?;
let target: SocketAddr = server.local_addr()?;
drop(server);
let sock = UdpSocket::bind("127.0.0.1:0")?;
sock.connect(target)?;
sock.send(b"ping")?;
let mut buf = [0; 8];
let err = sock.recv(&mut buf).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::ConnectionRefused);
Ok(())
}Calling read on a Tokio TcpStream also works as expected and will return any errors pending in SO_ERROR.
Metadata
Metadata
Assignees
Labels
A-tokioArea: The main tokio crateArea: The main tokio crateC-bugCategory: This is a bug.Category: This is a bug.M-netModule: tokio/netModule: tokio/net
Activity
cxw620 commentedon May 2, 2026
UdpSocket::poll_recvseems to have this bug as well,Direction::Readonly maps toReady::READABLE | Ready::READ_CLOSED.Rust Playground
Similarly, when I implemented
poll_recvfor a wrapper type ofAsyncFd<socket2::Socket>,AsyncFd::poll_read_readyis used and testing results indicate the same issue.martin-g commentedon May 5, 2026
@cxw620 I've just added a test with 66a0657, part of #8084
cxw620 commentedon May 5, 2026
https://github.com/tokio-rs/tokio/actions/runs/25362578266/job/74365642136?pr=8084, maybe we need a timeout when testing so.