This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs-object-store.git


The following commit(s) were added to refs/heads/main by this push:
     new bdc9f63  Fix flaky local close file test (#757)
bdc9f63 is described below

commit bdc9f632a645273d6412512a2213e9cbe6b926e4
Author: Kevin Liu <[email protected]>
AuthorDate: Wed Jun 17 15:29:39 2026 -0400

    Fix flaky local close file test (#757)
---
 src/local.rs | 52 ++++++++++++++++++++--------------------------------
 1 file changed, 20 insertions(+), 32 deletions(-)

diff --git a/src/local.rs b/src/local.rs
index a627e2d..b81cf7e 100644
--- a/src/local.rs
+++ b/src/local.rs
@@ -146,18 +146,15 @@ impl From<Error> for super::Error {
 fn close_file(file: File) -> std::result::Result<(), io::Error> {
     #[cfg(target_family = "unix")]
     {
-        nix::unistd::close(file).map_err(|e| e.into())
+        use std::os::fd::IntoRawFd;
+
+        close_fd(file.into_raw_fd())
     }
     #[cfg(target_family = "windows")]
     {
         use std::os::windows::io::IntoRawHandle;
 
-        let handle = file.into_raw_handle();
-        // SAFETY: `handle` is a valid, owned handle obtained from 
`into_raw_handle()`.
-        match unsafe { windows_sys::Win32::Foundation::CloseHandle(handle) } {
-            0 => Err(io::Error::last_os_error()),
-            _ => Ok(()),
-        }
+        close_handle(file.into_raw_handle())
     }
     #[cfg(not(any(target_family = "unix", target_family = "windows")))]
     {
@@ -166,6 +163,20 @@ fn close_file(file: File) -> std::result::Result<(), 
io::Error> {
     }
 }
 
+#[cfg(target_family = "unix")]
+fn close_fd(fd: std::os::fd::RawFd) -> std::result::Result<(), io::Error> {
+    nix::unistd::close(fd).map_err(|e| e.into())
+}
+
+#[cfg(target_family = "windows")]
+fn close_handle(handle: std::os::windows::io::RawHandle) -> 
std::result::Result<(), io::Error> {
+    // SAFETY: `handle` must be an owned handle, except when testing invalid 
handle errors.
+    match unsafe { windows_sys::Win32::Foundation::CloseHandle(handle) } {
+        0 => Err(io::Error::last_os_error()),
+        _ => Ok(()),
+    }
+}
+
 /// Local filesystem storage providing an [`ObjectStore`] interface to files on
 /// local disk. Can optionally be created with a directory prefix
 ///
@@ -2128,37 +2139,14 @@ mod tests {
     #[test]
     #[cfg(target_family = "unix")]
     fn test_close_file_detects_error_unix() {
-        use std::os::fd::FromRawFd;
-        use std::os::unix::io::AsRawFd;
-
-        let file = tempfile::tempfile().unwrap();
-
-        // Close and reclaim a File from the now-invalid fd
-        let file = {
-            let fd = file.as_raw_fd();
-            super::close_file(file).unwrap();
-            unsafe { std::fs::File::from_raw_fd(fd) }
-        };
-
-        let err = super::close_file(file).unwrap_err();
+        let err = super::close_fd(-1).unwrap_err();
         assert_eq!(err.raw_os_error(), Some(nix::libc::EBADF), "got: {err:?}");
     }
 
     #[test]
     #[cfg(target_family = "windows")]
     fn test_close_file_detects_error_windows() {
-        use std::os::windows::io::{AsRawHandle, FromRawHandle};
-
-        let file = tempfile::tempfile().unwrap();
-
-        // Close and reclaim a File from the now-invalid handle
-        let file = {
-            let handle = file.as_raw_handle();
-            super::close_file(file).unwrap();
-            unsafe { std::fs::File::from_raw_handle(handle) }
-        };
-
-        let err = super::close_file(file).unwrap_err();
+        let err = super::close_handle(std::ptr::null_mut()).unwrap_err();
         assert_eq!(
             err.raw_os_error(),
             Some(windows_sys::Win32::Foundation::ERROR_INVALID_HANDLE as i32),

Reply via email to