UdpSocket `recv()` fails after `send()` to a closed port
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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