bytedance / monoio

Rust async runtime based on io-uring.
Apache License 2.0
4.02k stars 226 forks source link

Bug: Bad file descriptor #295

Closed Lzzzzzt closed 2 months ago

Lzzzzzt commented 2 months ago

Version

cargo tree | grep monoio
monoio v0.2.4 (/home/ubuntu/Code/Rust/monoio/monoio)
├── monoio-macros v0.1.0 (proc-macro) (/home/ubuntu/Code/Rust/monoio/monoio-macros)

Platform

uname -a
Linux ubuntu-dev 6.8.0-41-generic #41-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug  2 20:41:06 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
ulimit -l
3074960

Description I am trying to implement AsyncWriteRent/AsyncReadRent for monoio::fs::FIle, but when I write unit tests, I encountered an error:

Os { code: 9, kind: Uncategorized, message: "Bad file descriptor" }

And I try the same code on the latest release, it will happen too.

Here is the minimum code to reproduce this error:

const HELLO: &[u8] = b"Hello, world!";

#[monoio::main]
async fn main() {
    let file = monoio::fs::File::create("test.txt").await.unwrap();
    file.write_at(HELLO, 0).await.0.unwrap();
    file.sync_all().await.unwrap();

    let result = file.read_at(vec![0; 13], 0).await;
    let res = result.0;
    println!("{:?}", res);
}

the file test.txt is successfully written with "Hello, world!", but can not read.

ihciah commented 2 months ago

strace with the code above(but switch to epoll for better observability):

epoll_create1(EPOLL_CLOEXEC)            = 3
eventfd2(0, EFD_CLOEXEC|EFD_NONBLOCK)   = 4
epoll_ctl(3, EPOLL_CTL_ADD, 4, {events=EPOLLIN|EPOLLRDHUP|EPOLLET, data={u32=2147483648, u64=2147483648}}) = 0
openat(AT_FDCWD, "test.txt", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = 5
pwrite64(5, "Hello, world!", 13, 0)     = 13
fsync(5)                                = 0
pread64(5, 0x58f06b7111f0, 13, 0)       = -1 EBADF (Bad file descriptor)
write(1, "Err(Os { code: 9, kind: Uncatego"..., 73Err(Os { code: 9, kind: Uncategorized, message: "Bad file descriptor" })
) = 73
fcntl(5, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
close(5)                                = 0
close(3)                                = 0
fcntl(4, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
close(4)

EBADF should be about the open mode of the file. After open it with read(O_WRONLY -> O_RDWR), it shows expected result. The strace log is below:

epoll_create1(EPOLL_CLOEXEC)            = 3
eventfd2(0, EFD_CLOEXEC|EFD_NONBLOCK)   = 4
epoll_ctl(3, EPOLL_CTL_ADD, 4, {events=EPOLLIN|EPOLLRDHUP|EPOLLET, data={u32=2147483648, u64=2147483648}}) = 0
openat(AT_FDCWD, "test.txt", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = 5
pwrite64(5, "Hello, world!", 13, 0)     = 13
fsync(5)                                = 0
pread64(5, "Hello, world!", 13, 0)      = 13
write(1, "Ok(13)\n", 7Ok(13)
)                 = 7
fcntl(5, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
close(5)                                = 0
close(3)                                = 0
fcntl(4, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
close(4)                                = 0
Lzzzzzt commented 2 months ago

got it, it's not a bug, the std::fs::File also act like this. Thank you. @ihciah