Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a double-close warning to close_range()
  • Loading branch information
Chris Pick committed Oct 5, 2023
commit 46bd488c04352de740c8f47df4d657b44cc27549
24 changes: 24 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,30 @@ libc_bitflags! {

/// Close a range of raw file descriptors.
/// [close_range(2)](https://man7.org/linux/man-pages/man2/close_range.2.html).
///
/// Be aware that many Rust types implicitly close-on-drop, including
/// `std::fs::File`. Explicitly closing them with this method too can result in
/// a double-close condition, which can cause confusing `EBADF` errors in
/// seemingly unrelated code. Caveat programmer.
///
/// # Examples
///
/// ```no_run
/// use std::os::unix::io::AsRawFd;
/// use nix::unistd::{close_range, CloseRangeFlags};
///
/// let f = tempfile::tempfile().unwrap();
/// close_range(f.as_raw_fd(), f.as_raw_fd(), CloseRangeFlags::empty()).unwrap(); // Bad! f will also close on drop!
/// ```
///
/// ```rust
/// use std::os::unix::io::IntoRawFd;
/// use nix::unistd::{close_range, CloseRangeFlags};
///
/// let f = tempfile::tempfile().unwrap();
/// let fd = f.into_raw_fd(); // Good. into_raw_fd consumes f
/// close_range(fd, fd, CloseRangeFlags::empty()).unwrap();
/// ```
#[cfg(target_os = "linux")]
pub fn close_range(
first: RawFd,
Expand Down