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

Implement IntoRawFd for SharedFd and File #228

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,17 @@ impl File {
}
}

impl IntoRawFd for File {
// Consumes this object, returning the raw underlying file descriptor.
// Since all in-flight operations hold a reference to &self, the type-checker
// will ensure that there are no in-flight operations when this method is
// called.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment that says the in-flight operations hold a reference to &self. The in-flight operations I think you mean actually hold a cloned SharedFd, like in

fd: SharedFd,

Copy link
Author

@thomasbarrett thomasbarrett Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah... when I was trying to wrap my head around SharedFd, I came to the following conclusion (please correct me If I am wrong): The SharedFd close method will hang forever if there is any in-flight IO. However, the actual File::close API does not appear to share this issue because the Rust borrow-checker already actively prevents in-flight asynchronous methods (or even just references to File) when transferring ownership of a tokio_uring::File (either by File::close or by File::into_raw_fd).

Example 1: OK

fn main() -> std::io::Result<()> {
    tokio_uring::start(async {
        let file = File::open("test.txt").await?;
        let fd = file.into_raw_fd();
        let file = unsafe { File::from_raw_fd(fd) };
        file.close().await?;

        Ok(())
    })
}

Example 2: Compilation Error

An async block that isn't awaited before calling into_raw_fd or close will result in an error.

fn main() -> std::io::Result<()> {
    tokio_uring::start(async {
        let file = File::open("test.txt").await?;

        tokio_uring::spawn(async {
            let buf: Vec<u8> = Vec::with_capacity(16);
            let (buf, res) = file.read_at(buf, 0).await;
        });

        let fd = file.into_raw_fd();
        let file = unsafe { File::from_raw_fd(fd) };
        file.close().await?;

        Ok(())
    })
}

Example 3: Compilation Error

cannot move out of an Rc

fn main() -> std::io::Result<()> {
    tokio_uring::start(async {
        let file = std::rc::Rc::new(File::open("test.txt").await?);
        let file2 = file.clone();
        tokio_uring::spawn(async move {
            let buf: Vec<u8> = Vec::with_capacity(16);
            let (buf, res) = file2.read_at(buf, 0).await;
        });

        let fd = file.into_raw_fd();
        let file = unsafe { File::from_raw_fd(fd) };
        file.close().await?;

        Ok(())
    })
}

Example 4: Ok

This compiles since we use try_unwrap (which ensures that there are no other references to File).

fn main() -> std::io::Result<()> {
    tokio_uring::start(async {
        let file = std::rc::Rc::new(File::open("test.txt").await?);
        let file2 = file.clone();
        tokio_uring::spawn(async move {
            let buf: Vec<u8> = Vec::with_capacity(16);
            let (buf, res) = file2.read_at(buf, 0).await;
        });

        match std::rc::Rc::try_unwrap(file) {
            Ok(file) => {
                let fd = file.into_raw_fd();
                let file = unsafe { File::from_raw_fd(fd) };
                file.close().await;
            },
            Err(err) => {
                panic!("unexpected io in-flight")
            }
        }

        Ok(())
    })
}

Copy link
Author

@thomasbarrett thomasbarrett Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example 5: In-flight IO possible only with (incorrect usage of) unsafe code.

fn main() -> std::io::Result<()> {
    tokio_uring::start(async {
        let file = File::open("test.txt").await?;
        let fd = file.into_raw_fd();

        tokio_uring::spawn(async move {
            let file = unsafe { File::from_raw_fd(fd) };
            let buf: Vec<u8> = Vec::with_capacity(16);
            let (buf, res) = file.read_at(buf, 0).await;
        });

        let file = unsafe { File::from_raw_fd(fd) };
        file.close().await;

        Ok(())
    })
}

fn into_raw_fd(self) -> RawFd {
// This should never panic.
self.fd.into_raw_fd()
}
}

impl FromRawFd for File {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
File::from_shared_fd(SharedFd::new(fd))
Expand Down
25 changes: 24 additions & 1 deletion src/io/shared_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::io::Close;
use std::future::poll_fn;

use std::cell::RefCell;
use std::os::unix::io::{FromRawFd, RawFd};
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
use std::rc::Rc;
use std::task::Waker;

Expand Down Expand Up @@ -39,6 +39,26 @@ enum State {

/// The FD is fully closed
Closed,

// The FD is leaked. A SharedFd in this state will not be closed on drop.
// A SharedFd will only enter this state as a result of the `into_raw_fd`
// method.
Leaked,
}

impl IntoRawFd for SharedFd {
// Consumes this object, returning the raw underlying file descriptor.
// This method will panic if there are any in-flight operations.
fn into_raw_fd(mut self) -> RawFd {
// Change the SharedFd state to `Leaked` so that the file-descriptor is
// not closed on drop.
if let Some(inner) = Rc::get_mut(&mut self.inner) {
let state = RefCell::get_mut(&mut inner.state);
*state = State::Leaked;
return self.raw_fd();
}
panic!("unexpected operation in-flight")
}
}

impl SharedFd {
Expand Down Expand Up @@ -139,6 +159,9 @@ impl Inner {
Poll::Ready(())
}
State::Closed => Poll::Ready(()),
// By definition, a SharedFd in a Leaked state is not closed, so we should
// never encounter this case while waiting for a SharedFd to close.
State::Leaked => unreachable!(),
}
})
.await;
Expand Down