Skip to content

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

Description

@link2xt

Original issue is chatmail/core#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

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:

// We need to flush the file on drop. Unfortunately, that is not possible to do in a

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 chatmail/core#2032

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions