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 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
17 changes: 11 additions & 6 deletions tokio/src/io/async_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,8 @@ 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));
}
if self.inner.is_some() {
self.into_inner_inplace();
}
}
}
Expand Down Expand Up @@ -230,10 +227,18 @@ impl<T: AsRawFd> AsyncFd<T> {
self.inner.as_mut().unwrap()
}

fn into_inner_inplace(&mut self) -> T {
let fd = self.inner.as_ref().unwrap().as_raw_fd();
if let Some(driver) = self.handle.inner() {
let _ = driver.deregister_source(&mut SourceFd(&fd));
}
self.inner.take().unwrap()
}

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

/// 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
Original file line number Diff line number Diff line change
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