diff --git a/library/std/src/os/fd/owned.rs b/library/std/src/os/fd/owned.rs index 010ce4e5076ba..a626415a5d16a 100644 --- a/library/std/src/os/fd/owned.rs +++ b/library/std/src/os/fd/owned.rs @@ -176,7 +176,16 @@ impl Drop for OwnedFd { // something like EINTR), we might close another valid file descriptor // opened after we closed ours. #[cfg(not(target_os = "hermit"))] - let _ = libc::close(self.fd); + { + match cvt(libc::close(self.fd)) { + Err(e) if e.raw_os_error() == Some(libc::EBADF) => { + rtabort!( + "IO Safety violation: owned file descriptor already closed (EBADF)" + ); + } + _ => {} + } + } #[cfg(target_os = "hermit")] let _ = hermit_abi::close(self.fd); }