Skip to content

Commit a64cfac

Browse files
authored
Fix multiprocessing closesocket, set_errno (RustPython#6388)
* Fix multiprocessing closesocket * set_errno
1 parent 30fb9d7 commit a64cfac

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ lock_api = "0.4"
3535
siphasher = "1"
3636
num-complex.workspace = true
3737

38+
[target.'cfg(unix)'.dependencies]
39+
nix = { workspace = true }
40+
3841
[target.'cfg(windows)'.dependencies]
3942
widestring = { workspace = true }
4043
windows-sys = { workspace = true, features = [

crates/common/src/os.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ pub fn get_errno() -> i32 {
4848
std::io::Error::last_os_error().posix_errno()
4949
}
5050

51+
/// Set errno to the specified value.
52+
#[cfg(windows)]
53+
pub fn set_errno(value: i32) {
54+
unsafe extern "C" {
55+
fn _set_errno(value: i32) -> i32;
56+
}
57+
unsafe { suppress_iph!(_set_errno(value)) };
58+
}
59+
60+
#[cfg(unix)]
61+
pub fn set_errno(value: i32) {
62+
nix::errno::Errno::from_raw(value).set();
63+
}
64+
5165
#[cfg(unix)]
5266
pub fn bytes_as_os_str(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> {
5367
use std::os::unix::ffi::OsStrExt;

crates/stdlib/src/multiprocessing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod _multiprocessing {
99
#[pyfunction]
1010
fn closesocket(socket: usize, vm: &VirtualMachine) -> PyResult<()> {
1111
let res = unsafe { WinSock::closesocket(socket as SOCKET) };
12-
if res == 0 {
12+
if res != 0 {
1313
Err(vm.new_last_os_error())
1414
} else {
1515
Ok(())

crates/stdlib/src/socket.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,10 +2143,11 @@ mod _socket {
21432143
#[pyfunction]
21442144
fn if_nametoindex(name: FsPath, vm: &VirtualMachine) -> PyResult<IfIndex> {
21452145
let name = name.to_cstring(vm)?;
2146-
2146+
// in case 'if_nametoindex' does not set errno
2147+
crate::common::os::set_errno(libc::ENODEV);
21472148
let ret = unsafe { c::if_nametoindex(name.as_ptr() as _) };
21482149
if ret == 0 {
2149-
Err(vm.new_os_error("no interface with this name".to_owned()))
2150+
Err(vm.new_last_errno_error())
21502151
} else {
21512152
Ok(ret)
21522153
}
@@ -2156,6 +2157,8 @@ mod _socket {
21562157
#[pyfunction]
21572158
fn if_indextoname(index: IfIndex, vm: &VirtualMachine) -> PyResult<String> {
21582159
let mut buf = [0; c::IF_NAMESIZE + 1];
2160+
// in case 'if_indextoname' does not set errno
2161+
crate::common::os::set_errno(libc::ENXIO);
21592162
let ret = unsafe { c::if_indextoname(index, buf.as_mut_ptr()) };
21602163
if ret.is_null() {
21612164
Err(vm.new_last_errno_error())

crates/vm/src/stdlib/posix.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2337,8 +2337,9 @@ pub mod module {
23372337

23382338
#[pyfunction]
23392339
fn sysconf(name: SysconfName, vm: &VirtualMachine) -> PyResult<libc::c_long> {
2340+
crate::common::os::set_errno(0);
23402341
let r = unsafe { libc::sysconf(name.0) };
2341-
if r == -1 {
2342+
if r == -1 && crate::common::os::get_errno() != 0 {
23422343
return Err(vm.new_last_errno_error());
23432344
}
23442345
Ok(r)

0 commit comments

Comments
 (0)