From b756faad2b093b6db2f195171ad7b421490bc828 Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Sun, 17 Aug 2025 14:11:27 +0800 Subject: [PATCH] refactor(connect): safely convert `Socket` to Tokio `TcpSocket` --- src/client/legacy/connect/http.rs | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/client/legacy/connect/http.rs b/src/client/legacy/connect/http.rs index 12eed073..56324e79 100644 --- a/src/client/legacy/connect/http.rs +++ b/src/client/legacy/connect/http.rs @@ -914,24 +914,8 @@ fn connect( ) .map_err(ConnectError::m("tcp bind local error"))?; - #[cfg(unix)] - let socket = unsafe { - // Safety: `from_raw_fd` is only safe to call if ownership of the raw - // file descriptor is transferred. Since we call `into_raw_fd` on the - // socket2 socket, it gives up ownership of the fd and will not close - // it, so this is safe. - use std::os::unix::io::{FromRawFd, IntoRawFd}; - TcpSocket::from_raw_fd(socket.into_raw_fd()) - }; - #[cfg(windows)] - let socket = unsafe { - // Safety: `from_raw_socket` is only safe to call if ownership of the raw - // Windows SOCKET is transferred. Since we call `into_raw_socket` on the - // socket2 socket, it gives up ownership of the SOCKET and will not close - // it, so this is safe. - use std::os::windows::io::{FromRawSocket, IntoRawSocket}; - TcpSocket::from_raw_socket(socket.into_raw_socket()) - }; + // Convert the `Socket` to a Tokio `TcpSocket`. + let socket = TcpSocket::from_std_stream(socket.into()); if config.reuse_address { if let Err(e) = socket.set_reuseaddr(true) {