Skip to content

Commit c33fa74

Browse files
committed
Close file descriptor on drop in TimerFd
1 parent a279f78 commit c33fa74

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
- Added limited Fuchsia support (#[1285](https://github.com/nix-rust/nix/pull/1285))
1111
- Added `getpeereid` (#[1342](https://github.com/nix-rust/nix/pull/1342))
1212
### Fixed
13+
- `TimerFd` now closes the underlying fd on drop.
14+
([#1381](https://github.com/nix-rust/nix/pull/1381))
1315
### Changed
1416

1517
- Minimum supported Rust version is now 1.40.0.
@@ -25,6 +27,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2527

2628
- Removed `SockLevel`, which hasn't been used for a few years
2729
(#[1362](https://github.com/nix-rust/nix/pull/1362))
30+
- Removed both `Copy` and `Clone` from `TimerFd`.
31+
([#1381](https://github.com/nix-rust/nix/pull/1381))
2832

2933
## [0.19.1] - 28 November 2020
3034
### Fixed

src/sys/timerfd.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
3737

3838
/// A timerfd instance. This is also a file descriptor, you can feed it to
3939
/// other interfaces consuming file descriptors, epoll for example.
40-
#[derive(Debug, Clone, Copy)]
40+
#[derive(Debug)]
4141
pub struct TimerFd {
4242
fd: RawFd,
4343
}
@@ -166,7 +166,7 @@ pub enum Expiration {
166166
impl TimerFd {
167167
/// Creates a new timer based on the clock defined by `clockid`. The
168168
/// underlying fd can be assigned specific flags with `flags` (CLOEXEC,
169-
/// NONBLOCK).
169+
/// NONBLOCK). The underlying fd will be closed on drop.
170170
pub fn new(clockid: ClockId, flags: TimerFlags) -> Result<Self> {
171171
Errno::result(unsafe { libc::timerfd_create(clockid as i32, flags.bits()) })
172172
.map(|fd| Self { fd })
@@ -270,3 +270,16 @@ impl TimerFd {
270270
Ok(())
271271
}
272272
}
273+
274+
impl Drop for TimerFd {
275+
fn drop(&mut self) {
276+
if !std::thread::panicking() {
277+
let result = Errno::result(unsafe {
278+
libc::close(self.fd)
279+
});
280+
if let Err(Error::Sys(Errno::EBADF)) = result {
281+
panic!("close of TimerFd encountered EBADF");
282+
}
283+
}
284+
}
285+
}

0 commit comments

Comments
 (0)