vmm: Fix fd leak in socket connection handling

Previously, `into_raw_fd()` transferred fd ownership to epoll while
keeping separate clones in `reader` and `writer`, causing leaks when
the stream was closed. Now, `reader` owns the fd and epoll borrows it.
When the FD is closed in the kernel, reader will be reset to None.

Signed-off-by: Praveen K Paladugu <prapal@linux.microsoft.com>
This commit is contained in:
Praveen K Paladugu
2025-11-24 15:03:09 -06:00
committed by Rob Bradford
parent 0d884d3f50
commit ac5fc69b2e

View File

@@ -6,7 +6,7 @@
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::net::Shutdown; use std::net::Shutdown;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; use std::os::unix::io::{AsRawFd, FromRawFd};
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::panic::AssertUnwindSafe; use std::panic::AssertUnwindSafe;
use std::path::PathBuf; use std::path::PathBuf;
@@ -329,20 +329,19 @@ impl SerialManager {
listener.accept().map_err(Error::AcceptConnection)?; listener.accept().map_err(Error::AcceptConnection)?;
let writer = let writer =
unix_stream.try_clone().map_err(Error::CloneUnixStream)?; unix_stream.try_clone().map_err(Error::CloneUnixStream)?;
reader = Some(
unix_stream.try_clone().map_err(Error::CloneUnixStream)?,
);
epoll::ctl( epoll::ctl(
epoll_fd, epoll_fd,
epoll::ControlOptions::EPOLL_CTL_ADD, epoll::ControlOptions::EPOLL_CTL_ADD,
unix_stream.into_raw_fd(), unix_stream.as_raw_fd(),
epoll::Event::new( epoll::Event::new(
epoll::Events::EPOLLIN, epoll::Events::EPOLLIN,
EpollDispatch::File as u64, EpollDispatch::File as u64,
), ),
) )
.map_err(Error::Epoll)?; .map_err(Error::Epoll)?;
reader = Some(unix_stream);
serial.lock().unwrap().set_out(Some(Box::new(writer))); serial.lock().unwrap().set_out(Some(Box::new(writer)));
} }
EpollDispatch::File => { EpollDispatch::File => {