From 673825d207ff72307c386d29f68c401bbf248d21 Mon Sep 17 00:00:00 2001 From: FujiApple Date: Sat, 21 Dec 2024 23:02:26 +0800 Subject: [PATCH] fix(core): socket being closed twice on Windows --- crates/trippy-core/src/error.rs | 2 -- crates/trippy-core/src/net/platform/unix.rs | 5 ----- crates/trippy-core/src/net/platform/windows.rs | 10 ---------- crates/trippy-core/src/net/socket.rs | 1 - crates/trippy-core/src/net/source.rs | 11 +---------- 5 files changed, 1 insertion(+), 28 deletions(-) diff --git a/crates/trippy-core/src/error.rs b/crates/trippy-core/src/error.rs index 7d80353f1..542d3fef4 100644 --- a/crates/trippy-core/src/error.rs +++ b/crates/trippy-core/src/error.rs @@ -90,7 +90,6 @@ pub enum IoOperation { SetReusePort, SetHeaderIncluded, SetUnicastHopsV6, - Close, WSACreateEvent, WSARecvFrom, WSAEventSelect, @@ -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"), diff --git a/crates/trippy-core/src/net/platform/unix.rs b/crates/trippy-core/src/net/platform/unix.rs index 70eee69ed..67a0eed1e 100644 --- a/crates/trippy-core/src/net/platform/unix.rs +++ b/crates/trippy-core/src/net/platform/unix.rs @@ -480,11 +480,6 @@ mod socket { fn icmp_error_info(&mut self) -> IoResult { 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 { diff --git a/crates/trippy-core/src/net/platform/windows.rs b/crates/trippy-core/src/net/platform/windows.rs index 26fdf06fa..ac3a80d2b 100644 --- a/crates/trippy-core/src/net/platform/windows.rs +++ b/crates/trippy-core/src/net/platform/windows.rs @@ -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(); } @@ -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() diff --git a/crates/trippy-core/src/net/socket.rs b/crates/trippy-core/src/net/socket.rs index 3db2f7cf4..6150050b2 100644 --- a/crates/trippy-core/src/net/socket.rs +++ b/crates/trippy-core/src/net/socket.rs @@ -45,7 +45,6 @@ where fn peer_addr(&mut self) -> Result>; fn take_error(&mut self) -> Result>; fn icmp_error_info(&mut self) -> Result; - fn close(&mut self) -> Result<()>; } /// A socket error returned by `Socket::take_error`. diff --git a/crates/trippy-core/src/net/source.rs b/crates/trippy-core/src/net/source.rs index dc857be12..6b2df2636 100644 --- a/crates/trippy-core/src/net/source.rs +++ b/crates/trippy-core/src/net/source.rs @@ -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())), } } @@ -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) }); @@ -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) });