-
Notifications
You must be signed in to change notification settings - Fork 19
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,13 @@ impl Listener { | |
} | ||
} | ||
|
||
pub const fn is_already_accepted(&self) -> bool { | ||
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; | ||
|
@@ -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()))) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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)), | ||
} | ||
|
@@ -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)), | ||
} | ||
|
@@ -607,5 +630,9 @@ pub fn listen<S: ?Sized + AsRef<str>, H: crate::ConnectionHandler + Send + Sync | |
} | ||
} | ||
}); | ||
|
||
if listener.is_already_accepted() { | ||
return Ok(()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.