Skip to content

Commit

Permalink
Fix #229
Browse files Browse the repository at this point in the history
  • Loading branch information
hxzhao527 committed Feb 26, 2024
1 parent a258e59 commit a2bb9bc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
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();

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);

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!();
}
}
}
}

0 comments on commit a2bb9bc

Please sign in to comment.