Skip to content

Instantly share code, notes, and snippets.

@oxalica
Last active November 6, 2024 20:15
Show Gist options
  • Save oxalica/666d7c6146bab4b1d6b0c974561c2daf to your computer and use it in GitHub Desktop.
Save oxalica/666d7c6146bab4b1d6b0c974561c2daf to your computer and use it in GitHub Desktop.
io-uring closing direct socket does not wake the peer end
// On Linux 6.6.59, x86_64
// Cargo.toml:
// ```toml
// [package]
// name = "testt"
// version = "0.0.0"
// edition = "2021"
// [dependencies]
// io-uring = "0.7.1"
// ```
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::os::fd::AsRawFd;
use io_uring::types::Fixed;
use io_uring::{cqueue, opcode, IoUring};
fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], 12345));
let mut ring = IoUring::new(16).unwrap();
{
let listener = TcpListener::bind(addr).unwrap();
let connected = std::thread::spawn(move || TcpStream::connect(addr))
.join()
.unwrap()
.unwrap();
let (accepted, _) = listener.accept().unwrap();
// Blocking mode does not matter for io-uring ops.
// connected.set_nonblocking(true).unwrap();
// accepted.set_nonblocking(true).unwrap();
ring.submitter()
.register_files(&[connected.as_raw_fd(), accepted.as_raw_fd()])
.unwrap();
// Drop all socket fds outside the ring.
}
let print_cqe = |cqe: cqueue::Entry| println!("ud={}, ret={}", cqe.user_data(), cqe.result());
let mut buf = [0u8; 8];
unsafe {
ring.submission()
.push_multiple(&[
// Emit read on one end, and close the other end.
// The order is important. If we close before read, then read returns 0
// successfully without hang, which is the expected behavior.
opcode::Read::new(Fixed(0), buf.as_mut_ptr(), 8)
.build()
.user_data(1),
opcode::Close::new(Fixed(1)).build().user_data(2),
])
.unwrap();
}
// Close op returns successfully.
ring.submit_and_wait(1).unwrap();
print_cqe(ring.completion().next().unwrap());
// This will hang indefinitely.
ring.submit_and_wait(1).unwrap();
print_cqe(ring.completion().next().unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment