You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jgiannuzzi (via GitHub)" <gi...@apache.org> on 2026/03/23 13:07:46 UTC

[PR] feat: explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

jgiannuzzi opened a new pull request, #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Implements one part of #661.
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   See issue. This PR is limited to explicitly calling `close` on written files with error checking.
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   - Add `close_file()` helper that calls `libc::close()` (Unix) / `CloseHandle` (Windows) directly and propagates errors, instead of relying on `File::drop()`. Note that this uses `unsafe`, like in the platforms' respective `drop()` implementations.
   - Apply both to the `put` and `put_multipart` (via `LocalUpload`) code paths.
   - Add `libc` (Unix) and `windows-sys` (Windows) as optional dependencies behind the `fs` feature for their respective platforms.
   - Add unit test verifying `close_file` detects `EBADF` when the file descriptor is closed behind Rust's back. This is just to validate that `close_file` properly returns the OS error.
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please call them out.
   -->
   
   Close errors are now detected and propagated as `Error::UnableToCopyDataToFile`. Previously these were silently ignored. This is a correctness improvement but could surface errors that were previously hidden.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(local): explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

Posted by "jgiannuzzi (via GitHub)" <gi...@apache.org>.
jgiannuzzi commented on code in PR #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676#discussion_r3046448631


##########
src/local.rs:
##########
@@ -137,6 +137,37 @@ impl From<Error> for super::Error {
     }
 }
 
+/// Explicitly close a file, checking for errors that would be silently ignored by Rust's `File::drop()`.
+///
+/// On network filesystems (e.g. NFS), `close()` can fail and indicate data loss.
+fn close_file(file: File) -> std::result::Result<(), io::Error> {
+    #[cfg(target_family = "unix")]
+    {
+        use std::os::unix::io::IntoRawFd;
+        let fd = file.into_raw_fd();
+        // SAFETY: `fd` is a valid, owned file descriptor obtained from `into_raw_fd()`.
+        match unsafe { libc::close(fd) } {
+            0 => Ok(()),
+            _ => Err(io::Error::last_os_error()),
+        }

Review Comment:
   `nix` was only used as a dev dependency which is why I went with `libc` instead, cutting down the middleman (`nix` uses `libc` under the hood). I am happy to use `nix` instead though, as it simplifies this piece of code significantly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(local): explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

Posted by "crepererum (via GitHub)" <gi...@apache.org>.
crepererum commented on code in PR #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676#discussion_r3044274272


##########
src/local.rs:
##########
@@ -1913,4 +1956,16 @@ mod unix_test {
 
         spawned.await.unwrap();
     }
+
+    #[test]
+    fn test_close_file_detects_error() {
+        use std::os::unix::io::AsRawFd;
+
+        let file = tempfile::tempfile().unwrap();
+        let fd = file.as_raw_fd();
+        // Close the fd behind Rust's back so close_file will get EBADF
+        assert_eq!(unsafe { libc::close(fd) }, 0);
+        let err = super::close_file(file).unwrap_err();
+        assert_eq!(err.raw_os_error(), Some(libc::EBADF));
+    }

Review Comment:
   This test tests the new function that you've just added. However I somewhat fail to see what actual error case you're trying to prevent here. The file descriptor isn't exposed to the API user of `object_store`. In #661 you reference https://github.com/pola-rs/polars/issues/21002 but that issue is about polar's own file handling, not about `object_store`. Also you say this is an alternative to `fsync`, but that's not true. Closing the file doesn't give you ANY guarantees with regards to durability. So what concrete order of events leads to a case where the additional error check (via the manual FD close) would help?



##########
src/local.rs:
##########
@@ -137,6 +137,37 @@ impl From<Error> for super::Error {
     }
 }
 
+/// Explicitly close a file, checking for errors that would be silently ignored by Rust's `File::drop()`.
+///
+/// On network filesystems (e.g. NFS), `close()` can fail and indicate data loss.
+fn close_file(file: File) -> std::result::Result<(), io::Error> {
+    #[cfg(target_family = "unix")]
+    {
+        use std::os::unix::io::IntoRawFd;
+        let fd = file.into_raw_fd();
+        // SAFETY: `fd` is a valid, owned file descriptor obtained from `into_raw_fd()`.
+        match unsafe { libc::close(fd) } {
+            0 => Ok(()),
+            _ => Err(io::Error::last_os_error()),
+        }

Review Comment:
   Since we already depend on `nix`, could we use that library on Unix instead?
   
   https://docs.rs/nix/latest/nix/unistd/fn.close.html



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(local): explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

Posted by "jgiannuzzi (via GitHub)" <gi...@apache.org>.
jgiannuzzi commented on PR #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676#issuecomment-4259456094

   hey @crepererum, could you please take another look at this?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(local): explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

Posted by "crepererum (via GitHub)" <gi...@apache.org>.
crepererum merged PR #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(local): explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

Posted by "crepererum (via GitHub)" <gi...@apache.org>.
crepererum commented on code in PR #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676#discussion_r3046526213


##########
src/local.rs:
##########
@@ -1913,4 +1956,16 @@ mod unix_test {
 
         spawned.await.unwrap();
     }
+
+    #[test]
+    fn test_close_file_detects_error() {
+        use std::os::unix::io::AsRawFd;
+
+        let file = tempfile::tempfile().unwrap();
+        let fd = file.as_raw_fd();
+        // Close the fd behind Rust's back so close_file will get EBADF
+        assert_eq!(unsafe { libc::close(fd) }, 0);
+        let err = super::close_file(file).unwrap_err();
+        assert_eq!(err.raw_os_error(), Some(libc::EBADF));
+    }

Review Comment:
   Ah, NFS our old friend :grin: . This makes sense, thank you!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(local): explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

Posted by "jgiannuzzi (via GitHub)" <gi...@apache.org>.
jgiannuzzi commented on code in PR #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676#discussion_r3046493043


##########
src/local.rs:
##########
@@ -1913,4 +1956,16 @@ mod unix_test {
 
         spawned.await.unwrap();
     }
+
+    #[test]
+    fn test_close_file_detects_error() {
+        use std::os::unix::io::AsRawFd;
+
+        let file = tempfile::tempfile().unwrap();
+        let fd = file.as_raw_fd();
+        // Close the fd behind Rust's back so close_file will get EBADF
+        assert_eq!(unsafe { libc::close(fd) }, 0);
+        let err = super::close_file(file).unwrap_err();
+        assert_eq!(err.raw_os_error(), Some(libc::EBADF));
+    }

Review Comment:
   This test is just validating our logic by emulating a failure case.
   
   The actual failure that can happen is when using a remote file system (e.g. NFS) and suffering from an underlying issue with the mount. In this case, the `close` could fail because some previous queued writes failed and checking the `close` return code allows the calling application to know that something failed, _even if `fsync` wasn't called_.
   
   See this comment on the Polars issue for more details: https://github.com/pola-rs/polars/issues/21002#issuecomment-3579381898
   
   I don't think I claimed this was an alternative to `fsync`. It's rather a way to make sure these errors are reported to the caller. For durability guarantees, `fsync` remains required (and I think this should be part of another PR).
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(local): explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

Posted by "crepererum (via GitHub)" <gi...@apache.org>.
crepererum commented on code in PR #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676#discussion_r3046529686


##########
src/local.rs:
##########
@@ -137,6 +137,37 @@ impl From<Error> for super::Error {
     }
 }
 
+/// Explicitly close a file, checking for errors that would be silently ignored by Rust's `File::drop()`.
+///
+/// On network filesystems (e.g. NFS), `close()` can fail and indicate data loss.
+fn close_file(file: File) -> std::result::Result<(), io::Error> {
+    #[cfg(target_family = "unix")]
+    {
+        use std::os::unix::io::IntoRawFd;
+        let fd = file.into_raw_fd();
+        // SAFETY: `fd` is a valid, owned file descriptor obtained from `into_raw_fd()`.
+        match unsafe { libc::close(fd) } {
+            0 => Ok(()),
+            _ => Err(io::Error::last_os_error()),
+        }

Review Comment:
   I think `nix` is a fine choice here and keeps the dev and prod world kinda in-sync :+1: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(local): explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

Posted by "crepererum (via GitHub)" <gi...@apache.org>.
crepererum commented on code in PR #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676#discussion_r3046539699


##########
src/local.rs:
##########
@@ -1913,4 +1956,16 @@ mod unix_test {
 
         spawned.await.unwrap();
     }
+
+    #[test]
+    fn test_close_file_detects_error() {
+        use std::os::unix::io::AsRawFd;
+
+        let file = tempfile::tempfile().unwrap();
+        let fd = file.as_raw_fd();
+        // Close the fd behind Rust's back so close_file will get EBADF
+        assert_eq!(unsafe { libc::close(fd) }, 0);
+        let err = super::close_file(file).unwrap_err();
+        assert_eq!(err.raw_os_error(), Some(libc::EBADF));
+    }

Review Comment:
   BTW: you could also call `close_file` twice and expect that the 2nd call fails. That would make this test here also work on Windows.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(local): explicit close with error checking for LocalFileSystem [arrow-rs-object-store]

Posted by "jgiannuzzi (via GitHub)" <gi...@apache.org>.
jgiannuzzi commented on code in PR #676:
URL: https://github.com/apache/arrow-rs-object-store/pull/676#discussion_r3051845713


##########
src/local.rs:
##########
@@ -1913,4 +1956,16 @@ mod unix_test {
 
         spawned.await.unwrap();
     }
+
+    #[test]
+    fn test_close_file_detects_error() {
+        use std::os::unix::io::AsRawFd;
+
+        let file = tempfile::tempfile().unwrap();
+        let fd = file.as_raw_fd();
+        // Close the fd behind Rust's back so close_file will get EBADF
+        assert_eq!(unsafe { libc::close(fd) }, 0);
+        let err = super::close_file(file).unwrap_err();
+        assert_eq!(err.raw_os_error(), Some(libc::EBADF));
+    }

Review Comment:
   Good point! I've updated the Unix test and added a Windows version of it. Sadly I still need to use a raw fd/handle in order to not consume the file and be able to close it twice. Thank you Rust for generally preventing misusing APIs! (That's not sarcasm 😅)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org