Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async_fd: make into_inner() deregister the fd #3104

Merged
merged 2 commits into from Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions tokio/src/io/async_fd.rs
Expand Up @@ -177,12 +177,7 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> {

impl<T: AsRawFd> Drop for AsyncFd<T> {
fn drop(&mut self) {
if let Some(driver) = self.handle.inner() {
if let Some(inner) = self.inner.as_ref() {
let fd = inner.as_raw_fd();
let _ = driver.deregister_source(&mut SourceFd(&fd));
}
}
let _ = self.take_inner();
}
}

Expand Down Expand Up @@ -230,10 +225,20 @@ impl<T: AsRawFd> AsyncFd<T> {
self.inner.as_mut().unwrap()
}

fn take_inner(&mut self) -> Option<T> {
let fd = self.inner.as_ref().map(AsRawFd::as_raw_fd);
if let Some(fd) = fd {
if let Some(driver) = self.handle.inner() {
let _ = driver.deregister_source(&mut SourceFd(&fd));
}
}
self.inner.take()
}

/// Deregisters this file descriptor, and returns ownership of the backing
/// object.
pub fn into_inner(mut self) -> T {
self.inner.take().unwrap()
self.take_inner().unwrap()
}

/// Polls for read readiness. This function retains the waker for the last
Expand Down
9 changes: 9 additions & 0 deletions tokio/tests/io_async_fd.rs
Expand Up @@ -303,6 +303,15 @@ async fn drop_closes() {
std::mem::drop(arc_fd); // suppress unnecessary clone clippy warning
}

#[tokio::test]
async fn reregister() {
let (a, _b) = socketpair();

let afd_a = AsyncFd::new(a).unwrap();
let a = afd_a.into_inner();
AsyncFd::new(a).unwrap();
}

#[tokio::test]
async fn with_poll() {
use std::task::Poll;
Expand Down