Skip to content

Do not invoke accept() on an already-established connection #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions varlink/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ impl Listener {
}
}

pub const fn is_already_accepted(&self) -> bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I know the rest of the code is not consistent about this, having at least a minimal documentation string I think is a good idea for public APIs.

match *self {
Self::TCP(_, value) => value,
Self::UNIX(_, value) => value,
}
}

#[cfg(windows)]
pub fn accept(&self, timeout: u64) -> Result<Box<dyn Stream>> {
use winapi::um::winsock2::WSAEINTR as EINTR;
Expand Down Expand Up @@ -194,13 +201,21 @@ impl Listener {
}

match self {
&Listener::TCP(Some(ref l), _) => {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
&Listener::TCP(Some(ref l), accepted) => {
if accepted {
unsafe { Ok(Box::new(TcpStream::from_raw_fd(l.as_raw_fd()))) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without digging into this code a lot more, there's a reason those functions are unsafe and that reason is exactly what looks like a bug here: The previous listener still thinks it owns the file descriptor.

We can avoid this by deconstructing into an OwnedFd like Box::new(TcpStream::from(l.into::<OwnedFd>())) or so.

But I think we need to back up on this, I'll comment on the original ticket.

} else {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
}
}
Listener::UNIX(Some(ref l), _) => {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
Listener::UNIX(Some(ref l), accepted) => {
if *accepted {
unsafe { Ok(Box::new(UnixStream::from_raw_fd(l.as_raw_fd()))) }
} else {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
}
}
_ => Err(context!(ErrorKind::ConnectionClosed)),
}
Expand Down Expand Up @@ -250,13 +265,21 @@ impl Listener {
}
}
match self {
&Listener::TCP(Some(ref l), _) => {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
&Listener::TCP(Some(ref l), accepted) => {
if accepted {
unsafe { Ok(Box::new(TcpStream::from_raw_fd(l.as_raw_fd()))) }
} else {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
}
}
Listener::UNIX(Some(ref l), _) => {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
Listener::UNIX(Some(ref l), accepted) => {
if *accepted {
unsafe { Ok(Box::new(UnixStream::from_raw_fd(l.as_raw_fd()))) }
} else {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
}
}
_ => Err(context!(ErrorKind::ConnectionClosed)),
}
Expand Down Expand Up @@ -607,5 +630,9 @@ pub fn listen<S: ?Sized + AsRef<str>, H: crate::ConnectionHandler + Send + Sync
}
}
});

if listener.is_already_accepted() {
return Ok(());
}
}
}