Skip to content

Commit

Permalink
Don't leak eventfd on dup() error.
Browse files Browse the repository at this point in the history
  • Loading branch information
detly committed Apr 3, 2022
1 parent 1b087e5 commit e82f88c
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/sources/ping/eventfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,36 @@ const INCREMENT_CLOSE: u64 = 0x1;
#[inline]
pub fn make_ping() -> std::io::Result<(Ping, PingSource)> {
let read = eventfd(0, EfdFlags::EFD_CLOEXEC | EfdFlags::EFD_NONBLOCK)?;

// We only have one fd for the eventfd. If the sending end closes it when
// all copies are dropped, the receiving end will be closed as well. We can
// avoid this by duplicating the FD.
let write = dup(read)?;

let source = PingSource {
event: Generic::new(read, Interest::READ, Mode::Level),
};
match dup(read) {
Ok(write) => {
let source = PingSource {
event: Generic::new(read, Interest::READ, Mode::Level),
};

let ping = Ping {
event: Arc::new(FlagOnDrop { fd: write }),
};
let ping = Ping {
event: Arc::new(FlagOnDrop { fd: write }),
};

Ok((ping, source))
Ok((ping, source))
}
Err(dup_err) => {
if let Err(close_err) = close(read) {
// There's not much we can do with this, especially since this
// might simply have the same cause as the error we're already
// propagating.
log::error!(
"Error closing ping eventfd while handling error: {:?}",
close_err
);
}

Err(dup_err.into())
}
}
}

// Helper functions for the event source IO.
Expand Down

0 comments on commit e82f88c

Please sign in to comment.