bytedance / monoio

Rust async runtime based on io-uring.
Apache License 2.0
4.03k stars 226 forks source link

Windows sockets not waking in certain conditions #274

Open EliseZeroTwo opened 4 months ago

EliseZeroTwo commented 4 months ago

Version List the versions of all monoio crates you are using. The easiest way to get this information is using cargo tree subcommand: monoio v0.2.3

Platform Windows 11 Pro 10.0.22631 Build 22631

Description Enter your issue details here. One way to structure the description:

When reading from a TCP socket using read, sometimes it just does not wake even though the socket has pending data.

I tried this code:


use std::{rc::Rc, sync::atomic::AtomicBool};
use monoio::{io::{AsyncReadRent, AsyncWriteRentExt}, net::{TcpListener, TcpStream}};

#[monoio::main]
async fn main() {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();
    println!("Listening on {addr}");

    let (outgoing_res, incoming_res) = monoio::join!(TcpStream::connect_addr(addr), listener.accept());

    let mut outgoing = outgoing_res.unwrap();
    let (mut incoming, incoming_addr) = incoming_res.unwrap();

    println!("Connected to {addr} from {incoming_addr}");

    const TEXT: [&'static str; 0x10] = [
        "Meow 1",
        "Meow 2",
        "Meow 3",
        "Meow 4",
        "Meow 5",
        "Meow 6",
        "Meow 7",
        "Meow 8",
        "Meow 9",
        "Meow 10",
        "Meow 11",
        "Meow 12",
        "Meow 13",
        "Meow 14",
        "Meow 15",
        "Meow 16",
    ];

    let server_semaphore = Rc::new(AtomicBool::new(true));
    let client_semaphore = server_semaphore.clone();

    let client = monoio::spawn(async move {
        let mut recv_buf = Vec::<u8>::with_capacity(256);
        let mut r;
        let mut iter = 0;

        for text in TEXT {
            println!("Client writing");
            let written = outgoing.write_all(text).await.0.unwrap();
            println!("Client written {written} bytes\nClient reading");
            (r, recv_buf) = outgoing.read(recv_buf).await;
            println!("Client read {} bytes: {}", r.unwrap(), std::str::from_utf8(&recv_buf[..]).unwrap());
            recv_buf.clear();

            println!("Client finished iter {iter}");
            iter += 1;
        }
        client_semaphore.store(false, std::sync::atomic::Ordering::Relaxed);
    });

    let server = monoio::spawn(async move {
        let mut recv_buf = Vec::<u8>::with_capacity(256);
        let mut r;
        let mut iter = 0;

        while server_semaphore.load(std::sync::atomic::Ordering::Relaxed) {
            println!("Server reading");
            (r, recv_buf) = incoming.read(recv_buf).await;
            println!("Server read {}: {}\nServer echoing it back", r.unwrap(), std::str::from_utf8(&recv_buf[..]).unwrap());
            (r, recv_buf) = incoming.write_all(recv_buf).await;
            println!("Server wrote {} bytes", r.unwrap());
            recv_buf.clear();

            println!("Server finished iter {iter}");
            iter += 1;
        }
    });

    monoio::join!(server, client);
}

I expected to see this happen: All of the contents of the TEXT array to be sent over the socket and echoed back

Instead, this happened: The waker was never called even though there was data in the socket.

Listening on 127.0.0.1:49915
Connected to 127.0.0.1:49915 from 127.0.0.1:49916
Client writing
Client written 6 bytes
Client reading
Server reading
Server read 6: Meow 1
Server echoing it back
Server wrote 6 bytes
Server finished iter 0
Server reading
Client read 6 bytes: Meow 1
Client finished iter 0
Client writing
Client written 6 bytes
Client reading
cafedetal commented 2 months ago

+1

I'm also seeing this issue on windows, and it's a hard block on doing any proper networking since it makes it highly unreliable.

geotro commented 2 months ago

I'm seeing this issue as well when trying to run a server I wrote using monoio on Windows

ihciah commented 2 months ago

Windows support is still at early stage. @Lzzzzzt Do you have any idea about this issue?

Lzzzzzt commented 2 months ago

Windows support is still at early stage. @Lzzzzzt Do you have any idea about this issue?

Currently, I know almost nothing about Windows Socket, maybe I will investigate it after finishing my current work.

cafedetal commented 2 months ago

Appreciate the attention to the issue, if there's anymore that can be provided to help/test just let me know.