Skip to content

UdpSocket::recv does not return socket errors #8000

Closed

Description

@marvinvogt

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.

Rust Playground

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.

Rust Playground

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.

Activity

  1. added
    C-bugCategory: This is a bug.
    A-tokioArea: The main tokio crate
    on Apr 1, 2026
  2. cxw620 commented on May 2, 2026

    @cxw620

    UdpSocket::poll_recv seems to have this bug as well, Direction::Read only maps to Ready::READABLE | Ready::READ_CLOSED.

    Rust Playground

    Similarly, when I implemented poll_recv for a wrapper type of AsyncFd<socket2::Socket>, AsyncFd::poll_read_ready is used and testing results indicate the same issue.

  3. martin-g commented on May 5, 2026

    @martin-g
    Member

    @cxw620 I've just added a test with 66a0657, part of #8084

  4. cxw620 commented on May 5, 2026

    @cxw620

    @cxw620 I've just added a test with 66a0657, part of #8084

    https://github.com/tokio-rs/tokio/actions/runs/25362578266/job/74365642136?pr=8084, maybe we need a timeout when testing so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateC-bugCategory: This is a bug.M-netModule: tokio/net

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions