linuxrustwebsocketepoll

Epoll randomly stops sending event for TcpStream


I got a tungstenite connection with a TcpStream set to non-blocking and a timerfd connected to a epoll fd, for some reason the tcpstreams fd events randomly stops(it's not because of no more data is being sent) and no debugging have helped me find the problem. Tungstenite don't report anything wrong with the connection, there's just no more events for the rfd variable.

pub fn run_realtime_session(...) {
    let mut shutdown = false;

    let efd = epoll::create(true).unwrap();

    // Set up timer
    let mut timer = timerfd::TimerFd::new_custom(timerfd::ClockId::Realtime, true, false).unwrap();
    let tfd = timer.as_raw_fd();
    let timer_event = epoll::Event::new(epoll::Events::EPOLLIN, tfd.try_into().unwrap());
    epoll::ctl(efd, epoll::ControlOptions::EPOLL_CTL_ADD, tfd, timer_event).unwrap();
    timer.set_state(
        timerfd::TimerState::Periodic {
            current: std::time::Duration::from_millis(50),
            interval: std::time::Duration::from_millis(50),
        },
        timerfd::SetTimeFlags::Default,
    );

    // Set up reader
    let rfd = reader.as_raw_fd().unwrap();
    let read_event = epoll::Event::new(
        epoll::Events::EPOLLIN | epoll::Events::EPOLLET,
        rfd.try_into().unwrap(),
    );
    epoll::ctl(efd, epoll::ControlOptions::EPOLL_CTL_ADD, rfd, read_event).unwrap();
    let mut events = [epoll::Event::new(epoll::Events::empty(), 0); 10];

    while !shutdown {
        let res = epoll::wait(efd, -1, &mut events).unwrap();
        for event in events {
            if event.data as i32 == tfd {
                timer.read();
                ...
            } else if event.data as i32 == rfd {
                match reader.read() {
                    ...
                }
            }
        }
    }
}

Solution

  • Seems to have solved it by putting reader.read() in a loop until std::io::ErrorKind::WouldBlock is thrown.