From a64cfac2ca594d08b42d06cdb0f6bdd18404e8a6 Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" <69878+youknowone@users.noreply.github.com> Date: Wed, 10 Dec 2025 10:02:53 +0900 Subject: [PATCH] Fix multiprocessing closesocket, set_errno (#6388) * Fix multiprocessing closesocket * set_errno --- Cargo.lock | 1 + crates/common/Cargo.toml | 3 +++ crates/common/src/os.rs | 14 ++++++++++++++ crates/stdlib/src/multiprocessing.rs | 2 +- crates/stdlib/src/socket.rs | 7 +++++-- crates/vm/src/stdlib/posix.rs | 3 ++- 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aa2382d99..1de5f6751 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3007,6 +3007,7 @@ dependencies = [ "malachite-base", "malachite-bigint", "malachite-q", + "nix 0.30.1", "num-complex", "num-traits", "once_cell", diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml index c75f33a7b..9fd7ea388 100644 --- a/crates/common/Cargo.toml +++ b/crates/common/Cargo.toml @@ -35,6 +35,9 @@ lock_api = "0.4" siphasher = "1" num-complex.workspace = true +[target.'cfg(unix)'.dependencies] +nix = { workspace = true } + [target.'cfg(windows)'.dependencies] widestring = { workspace = true } windows-sys = { workspace = true, features = [ diff --git a/crates/common/src/os.rs b/crates/common/src/os.rs index e61c77e0f..2d82b1a6c 100644 --- a/crates/common/src/os.rs +++ b/crates/common/src/os.rs @@ -48,6 +48,20 @@ pub fn get_errno() -> i32 { std::io::Error::last_os_error().posix_errno() } +/// Set errno to the specified value. +#[cfg(windows)] +pub fn set_errno(value: i32) { + unsafe extern "C" { + fn _set_errno(value: i32) -> i32; + } + unsafe { suppress_iph!(_set_errno(value)) }; +} + +#[cfg(unix)] +pub fn set_errno(value: i32) { + nix::errno::Errno::from_raw(value).set(); +} + #[cfg(unix)] pub fn bytes_as_os_str(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> { use std::os::unix::ffi::OsStrExt; diff --git a/crates/stdlib/src/multiprocessing.rs b/crates/stdlib/src/multiprocessing.rs index 280eff7d3..9ff2d3dc3 100644 --- a/crates/stdlib/src/multiprocessing.rs +++ b/crates/stdlib/src/multiprocessing.rs @@ -9,7 +9,7 @@ mod _multiprocessing { #[pyfunction] fn closesocket(socket: usize, vm: &VirtualMachine) -> PyResult<()> { let res = unsafe { WinSock::closesocket(socket as SOCKET) }; - if res == 0 { + if res != 0 { Err(vm.new_last_os_error()) } else { Ok(()) diff --git a/crates/stdlib/src/socket.rs b/crates/stdlib/src/socket.rs index c00799fea..d75595648 100644 --- a/crates/stdlib/src/socket.rs +++ b/crates/stdlib/src/socket.rs @@ -2143,10 +2143,11 @@ mod _socket { #[pyfunction] fn if_nametoindex(name: FsPath, vm: &VirtualMachine) -> PyResult { let name = name.to_cstring(vm)?; - + // in case 'if_nametoindex' does not set errno + crate::common::os::set_errno(libc::ENODEV); let ret = unsafe { c::if_nametoindex(name.as_ptr() as _) }; if ret == 0 { - Err(vm.new_os_error("no interface with this name".to_owned())) + Err(vm.new_last_errno_error()) } else { Ok(ret) } @@ -2156,6 +2157,8 @@ mod _socket { #[pyfunction] fn if_indextoname(index: IfIndex, vm: &VirtualMachine) -> PyResult { let mut buf = [0; c::IF_NAMESIZE + 1]; + // in case 'if_indextoname' does not set errno + crate::common::os::set_errno(libc::ENXIO); let ret = unsafe { c::if_indextoname(index, buf.as_mut_ptr()) }; if ret.is_null() { Err(vm.new_last_errno_error()) diff --git a/crates/vm/src/stdlib/posix.rs b/crates/vm/src/stdlib/posix.rs index 3fbea58e4..6a6703f48 100644 --- a/crates/vm/src/stdlib/posix.rs +++ b/crates/vm/src/stdlib/posix.rs @@ -2337,8 +2337,9 @@ pub mod module { #[pyfunction] fn sysconf(name: SysconfName, vm: &VirtualMachine) -> PyResult { + crate::common::os::set_errno(0); let r = unsafe { libc::sysconf(name.0) }; - if r == -1 { + if r == -1 && crate::common::os::get_errno() != 0 { return Err(vm.new_last_errno_error()); } Ok(r)