Skip to content

tokio::fs::File::write returns early before OS says that the operation is completed #7378

@Noratrieb

Description

@Noratrieb

Version
latest master

Platform
any

Description

This comes from rust-lang/miri#4367, where we hit this behavior in the Miri test suite.

let mut file = File::create(&path).await?;
file.write_all(b"some bytes").await?;
assert_eq!(file.metadata().await.unwrap().len(), 10);

While of course write_all is not guaranteed to actually flush to disk, the assumption that a single thread of execution sees the results after a write seems to be true for normal sync code (and is very intuitive), but this assumption does not hold for async code using tokio.

Tokio File just does an early return after starting the thread for the write asynchronously.

return Poll::Ready(Ok(n));

The code is written in a way that makes this look deliberate, but I could not find documentation about this rather surprising behavior, so consider this more of a documentation request, though of course making it behave consistently would be useful too (but maybe slower?).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateC-bugCategory: This is a bug.M-fsModule: tokio/fsT-docsTopic: documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions