rust-lang / rust-lang/rust

UdpSocket `recv()` fails after `send()` to a closed port

Open
#128,072 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-discussion T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Trying to recv() on a UdpSocket after a send() to a closed UDP socket fails

I tried this code:

use std::net::UdpSocket;

fn main() {
    let client = UdpSocket::bind("127.0.0.1:0").expect("failed to bind");
    let server = UdpSocket::bind("127.0.0.1:8080").expect("failed to bind");

    client.connect("127.0.0.1:1234").expect("connect function failed"); // no server at this address
    client.send(&[0; 10]).expect("couldn't send data"); // no error at this point

    let mut buf = [0; 10];
    match client.recv(&mut buf) {
        Ok(received) => println!("received {received} bytes {:?}", &buf[..received]),
        Err(e) => println!("recv function failed: {e:?}"), // we get an error here
    }
}

I expected to see this happen:
We should be able to recv() without any error

Instead, this happened:
recv() fails

Golang currently handles this using SIO_UDP_CONNRESET here: https://github.com/golang/go/blob/e8c5bed7ea43e1a533c322e6b928ed06327721db/src/internal/poll/fd_windows.go#L340-L351

Meta

rustc --version --verbose:

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-pc-windows-msvc
release: 1.79.0
LLVM version: 18.1.7
Backtrace

recv function failed: Os { code: 10054, kind: ConnectionReset, message: "An existing connection was forcibly closed by the remote host." }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the UdpSocket::send and UdpSocket::recv calls in the Windows reproduction, then compare the behavior with the SIO_UDP_CONNRESET handling in the linked Go implementation. Done means the closed-port scenario has an intentional recv result on Windows and coverage verifies that behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.