Skip to content

Commit

Permalink
fix(core): socket being closed twice on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Dec 21, 2024
1 parent eae4404 commit 673825d
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 28 deletions.
2 changes: 0 additions & 2 deletions crates/trippy-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ pub enum IoOperation {
SetReusePort,
SetHeaderIncluded,
SetUnicastHopsV6,
Close,
WSACreateEvent,
WSARecvFrom,
WSAEventSelect,
Expand Down Expand Up @@ -121,7 +120,6 @@ impl Display for IoOperation {
Self::SetReusePort => write!(f, "set reuse port"),
Self::SetHeaderIncluded => write!(f, "set header included"),
Self::SetUnicastHopsV6 => write!(f, "set unicast hops v6"),
Self::Close => write!(f, "close"),
Self::WSACreateEvent => write!(f, "WSA create event"),
Self::WSARecvFrom => write!(f, "WSA recv from"),
Self::WSAEventSelect => write!(f, "WSA event select"),
Expand Down
5 changes: 0 additions & 5 deletions crates/trippy-core/src/net/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,6 @@ mod socket {
fn icmp_error_info(&mut self) -> IoResult<IpAddr> {
Ok(IpAddr::V4(Ipv4Addr::UNSPECIFIED))
}
#[allow(clippy::unused_self, clippy::unnecessary_wraps)]
#[instrument(skip(self))]
fn close(&mut self) -> IoResult<()> {
Ok(())
}
}

impl From<&io::Error> for ErrorKind {
Expand Down
10 changes: 0 additions & 10 deletions crates/trippy-core/src/net/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ impl SocketImpl {
#[allow(clippy::redundant_closure_call)]
impl Drop for SocketImpl {
fn drop(&mut self) {
self.close().unwrap_or_default();
if self.ol.hEvent != -1 && self.ol.hEvent != 0 {
syscall!(WSACloseEvent(self.ol.hEvent), |res| { res == 0 }).unwrap_or_default();
}
Expand Down Expand Up @@ -579,15 +578,6 @@ impl Socket for SocketImpl {
)),
}
}

// Interestingly, Socket2 sockets don't seem to call closesocket on drop??
#[instrument(skip(self))]
fn close(&mut self) -> IoResult<()> {
syscall!(closesocket(self.inner.as_raw_socket() as _), |res| res
== SOCKET_ERROR)
.map_err(|err| IoError::Other(err, IoOperation::Close))
.map(|_| ())
}
}

// Note that we handle `WSAENOBUFS`, which can occurs when calling send_to()
Expand Down
1 change: 0 additions & 1 deletion crates/trippy-core/src/net/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ where
fn peer_addr(&mut self) -> Result<Option<SocketAddr>>;
fn take_error(&mut self) -> Result<Option<SocketError>>;
fn icmp_error_info(&mut self) -> Result<IpAddr>;
fn close(&mut self) -> Result<()>;
}

/// A socket error returned by `Socket::take_error`.
Expand Down
11 changes: 1 addition & 10 deletions crates/trippy-core/src/net/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ impl SourceAddr {
}?;
let sock_addr = SocketAddr::new(source_addr, 0);
match socket.bind(sock_addr) {
Ok(()) => {
socket.close()?;
Ok(source_addr)
}
Ok(()) => Ok(source_addr),
Err(_) => Err(InvalidSourceAddr(sock_addr.ip())),
}
}
Expand Down Expand Up @@ -161,9 +158,6 @@ mod tests {
.with(predicate::eq(expected_bind_addr))
.times(1)
.returning(|_| Ok(()));

mocket.expect_close().times(1).returning(|| Ok(()));

Ok(mocket)
});

Expand All @@ -186,9 +180,6 @@ mod tests {
.with(predicate::eq(expected_bind_addr))
.times(1)
.returning(|_| Ok(()));

mocket.expect_close().times(1).returning(|| Ok(()));

Ok(mocket)
});

Expand Down

0 comments on commit 673825d

Please sign in to comment.