async-rs / async-rs/async-std

Spawned task is stuck during flushing in File.drop()

Open
#900 18 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
4.1k
Forks
339
PR merge metrics
No merged PRs in 30d

Description

Original issue is https://github.com/deltachat/deltachat-core-rust/issues/2032

I have prepared a minimal example demonstrating the bug, which depends only on `async-std` 1.6.5.

An example which you can unpack and run with `cargo run`: [filedrop.tar.gz](https://github.com/async-rs/async-std/files/5425367/filedrop.tar.gz)

The source code for reference:
```
use async_std::prelude::*;
async fn create_file() {
let mut file = async_std::fs::OpenOptions::new()
.create(true)
.write(true)
.open("foo.txt")
.await
.unwrap();
file.write_all(b"foobarbaz").await.unwrap();
//file.flush().await.unwrap();
eprintln!("before drop");
}

async fn test() {
let tsk = async_std::task::spawn(async move {
create_file().await;
eprintln!("after drop");
});
tsk.await;
}

fn main() {
async_std::task::block_on(test());
}
```

I built this example in the following configuration:
1. Debian sid as a host machine.
2. NetBSD 9.1 as a guest, installed in qemu (kvm)
3. Rust 1.47.0 installed on NetBSD via rustup.

When I execute `cargo run`, the program prints `before drop` and gets stuck. Apparently the problem is that despite what comment says, executor does not handle blocking operation in Drop correctly:
https://github.com/async-rs/async-std/blob/11196c853dc42e86d608c4ed29af1a8f0c5c5084/src/fs/file.rs#L314

After waiting a minute and terminating the program, the file `foo.txt` is created, but is empty.

When I uncomment the line `file.flush().await.unwrap();`, the program prints
```
before drop
after drop
```
and exits.

On my Linux machine this works correctly. It is not a QEMU bug, as the same problem is experienced on some Android phones, see original issue https://github.com/deltachat/deltachat-core-rust/issues/2032

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.