Skip to content

Make sure to sync on file-io.rs tokio test #4370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 1, 2025

Conversation

Noratrieb
Copy link
Member

@Noratrieb Noratrieb commented Jun 1, 2025

Tokio AsyncWriteExt::write doesn't actually ensure that the contents have written, it just starts the write operation. To ensure that the file has actually been written, we need to sync_all first.

actually fixes #4367

Tokio `AsyncWriteExt::write` doesn't actually ensure that the contents
have written, it just *starts* the write operation. To ensure that the
file has actually been written, we need to `sync_all` first.
@tiif
Copy link
Member

tiif commented Jun 1, 2025

To add more context, me and nora tried

async fn test_create_and_write() -> io::Result<()> {
    let path = utils::prepare("foo.txt");
    let mut file = File::create(&path).await?;

    // Write 10 bytes to the file.
    let a = file.write(b"some bytes").await?;
    assert_eq!(a, 10);
    assert_eq!(file.metadata().await.unwrap().len(), 10);
    
    remove_file(&path).unwrap();
    Ok(())
}

and the test only fails on second assert in certain seeds. That means the file metadata is not completely synced before we access it.

@RalfJung
Copy link
Member

RalfJung commented Jun 1, 2025

Isn't that a tokio bug? When a single task creates a file and then tries to read it, surely that should just work?

@Noratrieb
Copy link
Member Author

The way the tokio code is written makes me think that this is deliberate behavior. I haven't found any docs about this though, so I will report that to tokio to at least ask them to document this behavior.

@RalfJung
Copy link
Member

RalfJung commented Jun 1, 2025

Not having program order establish happens-before is at least rather unconventional...

Cc @Darksonn

@Noratrieb
Copy link
Member Author

filed tokio-rs/tokio#7378

@RalfJung
Copy link
Member

RalfJung commented Jun 1, 2025

Is this PR required to un-break CI or should the other one be enough?

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to improve docs, but this is intended behavior. Tokio's file behaves like BufWriter<File> in std.

@Noratrieb
Copy link
Member Author

Noratrieb commented Jun 1, 2025

Is this PR required to un-break CI or should the other one be enough?

this one is required. i was able to produce failures with write_all locally (though it might be possible that just write_all reduces the amount of failures)

@RalfJung RalfJung added this pull request to the merge queue Jun 1, 2025
bors added a commit to rust-lang/rust that referenced this pull request Jun 1, 2025
Miri subtree update

r? `@ghost`

Includes rust-lang/miri#4370 to unbreak PR CI. (So we're committing to having bda28aa38 in the Miri history by landing this, whether or not that Miri PR lands.)

Cc `@Noratrieb` `@tiif`
Merged via the queue into rust-lang:master with commit d0a9d59 Jun 1, 2025
8 checks passed
@Noratrieb Noratrieb deleted the fix-for-real branch June 1, 2025 15:31
github-actions bot pushed a commit that referenced this pull request Jun 3, 2025
Miri subtree update

r? `@ghost`

Includes #4370 to unbreak PR CI. (So we're committing to having bda28aa in the Miri history by landing this, whether or not that Miri PR lands.)

Cc `@Noratrieb` `@tiif`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tokio/file-io test is non-deterministic
4 participants