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

submit all op before uring drop #245

Merged
merged 1 commit into from
Feb 27, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions monoio/src/driver/uring/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ impl Drop for IoUringDriver {

impl Drop for UringInner {
fn drop(&mut self) {
// no need to wait for completion, as the kernel will clean up the ring asynchronically.
let _ = self.uring.submitter().submit();
unsafe {
ManuallyDrop::drop(&mut self.uring);
}
Expand Down
39 changes: 31 additions & 8 deletions monoio/tests/fs_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn read_hello(file: &File) {
async fn basic_read() {
let mut tempfile = tempfile();
tempfile.write_all(HELLO).unwrap();
tempfile.as_file_mut().sync_data().unwrap();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just flush make sure that the content can be read.


let file = File::open(tempfile.path()).await.unwrap();
read_hello(&file).await;
Expand All @@ -31,6 +32,7 @@ async fn basic_read() {
async fn basic_read_exact() {
let mut tempfile = tempfile();
tempfile.write_all(HELLO).unwrap();
tempfile.as_file_mut().sync_data().unwrap();

let file = File::open(tempfile.path()).await.unwrap();
let buf = Vec::with_capacity(HELLO.len());
Expand All @@ -49,6 +51,7 @@ async fn basic_write() {

let file = File::create(tempfile.path()).await.unwrap();
file.write_at(HELLO, 0).await.0.unwrap();
file.sync_all().await.unwrap();

let file = std::fs::read(tempfile.path()).unwrap();
assert_eq!(file, HELLO);
Expand All @@ -60,6 +63,7 @@ async fn basic_write_all() {

let file = File::create(tempfile.path()).await.unwrap();
file.write_all_at(HELLO, 0).await.0.unwrap();
file.sync_all().await.unwrap();

let file = std::fs::read(tempfile.path()).unwrap();
assert_eq!(file, HELLO);
Expand All @@ -69,6 +73,7 @@ async fn basic_write_all() {
async fn cancel_read() {
let mut tempfile = tempfile();
tempfile.write_all(HELLO).unwrap();
tempfile.as_file_mut().sync_data().unwrap();

let file = File::open(tempfile.path()).await.unwrap();

Expand All @@ -82,6 +87,7 @@ async fn cancel_read() {
async fn explicit_close() {
let mut tempfile = tempfile();
tempfile.write_all(HELLO).unwrap();
tempfile.as_file_mut().sync_data().unwrap();

let file = File::open(tempfile.path()).await.unwrap();
#[cfg(unix)]
Expand All @@ -91,7 +97,7 @@ async fn explicit_close() {

file.close().await.unwrap();

assert_invalid_fd(fd);
assert_invalid_fd(fd, tempfile.as_file().metadata().unwrap());
}

#[monoio::test_all]
Expand All @@ -101,6 +107,7 @@ async fn drop_open() {
// Do something else
let file_w = File::create(tempfile.path()).await.unwrap();
file_w.write_at(HELLO, 0).await.0.unwrap();
file_w.sync_all().await.unwrap();

let file = std::fs::read(tempfile.path()).unwrap();
assert_eq!(file, HELLO);
Expand All @@ -125,7 +132,7 @@ fn drop_off_runtime() {
let fd = file.as_raw_handle();
drop(file);

assert_invalid_fd(fd);
assert_invalid_fd(fd, tempfile.as_file().metadata().unwrap());
}

#[monoio::test_all]
Expand Down Expand Up @@ -158,13 +165,29 @@ async fn poll_once(future: impl std::future::Future) {
.await;
}

fn assert_invalid_fd(fd: RawFd) {
fn assert_invalid_fd(fd: RawFd, base: std::fs::Metadata) {
use std::fs::File;
#[cfg(unix)]
let mut f = unsafe { File::from_raw_fd(fd) };
let f = unsafe { File::from_raw_fd(fd) };
#[cfg(windows)]
let mut f = unsafe { File::from_raw_handle(fd) };
let mut buf = vec![];

assert!(f.read_to_end(&mut buf).is_err());
let f = unsafe { File::from_raw_handle(fd) };

let meta = f.metadata();
std::mem::forget(f);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need forget.
The fd maybe reused by system.


if let Ok(meta) = meta {
if !meta.is_file() {
return;
}

#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
let inode = meta.ino();
let actual = base.ino();
if inode == actual {
panic!();
}
}
}
}
Loading