Skip to content
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

Docs: Improve the documentation around AsyncWriteExt::shutdown #6068

Closed
IvanUkhov opened this issue Oct 12, 2023 · 0 comments · Fixed by #6149
Closed

Docs: Improve the documentation around AsyncWriteExt::shutdown #6068

IvanUkhov opened this issue Oct 12, 2023 · 0 comments · Fixed by #6149
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-io Module: tokio/io

Comments

@IvanUkhov
Copy link

For write_all, we have the following example:

use tokio::io::{self, AsyncWriteExt};
use tokio::fs::File;

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut file = File::create("foo.txt").await?;

    file.write_all(b"some bytes").await?;
    Ok(())
}

It is correct if this is precisely the scenario at hand: we write, and we shutdown the runtime. However, one might get into non-deterministic troubles if one does not flush via shutdown before yielding the control:

#[tokio::main]
async fn main() -> io::Result<()> {
  write().await?;
  read().await?;
}

async fn write() -> io::Result<()> {
    let mut file = File::create("foo.txt").await?;
    file.write_all(b"some bytes").await?;
    Ok(())
}

async fn read() -> io::Result<()> {
    let mut file = File::open("foo.txt").await?;
    # …
    Ok(())
}

The read after the write might find the file incomplete, as the background thread for the write might still be around.

@IvanUkhov IvanUkhov added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Oct 12, 2023
@Darksonn Darksonn added the M-io Module: tokio/io label Nov 11, 2023
Darksonn added a commit to Darksonn/tokio that referenced this issue Nov 12, 2023
Darksonn added a commit that referenced this issue Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-io Module: tokio/io
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants