-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Async write_all sometimes silently fails to write to file #4296
Comments
This is because file operations are offloaded to the Note that the above implies that the data can only be lost if the write happens just before runtime shutdown. Otherwise it will complete normally even if the file is dropped before the write completes. It seems like async-std's approach is the following: impl Drop for File {
fn drop(&mut self) {
// We need to flush the file on drop. Unfortunately, that is not possible to do in a
// non-blocking fashion, but our only other option here is losing data remaining in the
// write cache. Good task schedulers should be resilient to occasional blocking hiccups in
// file destructors so we don't expect this to be a common problem in practice.
let _ = futures_lite::future::block_on(self.flush());
}
} However I would prefer to avoid this solution. If we want to solve this, I would find it better to have the runtime somehow always wait for the file operation before shutting down. |
Hi!
I don't understand why this guarantee is not/cannot be upheld by awaiting
But my understanding is that it is rather:
Is my first mental impractical to carry out, or is it a deliberate choice? In any case, I think @igorsol had a similar confusion so there may be some way we can think of improving the documentation. ¹ What I mean by "passed on to the OS for writting" is akin to |
Unfortunately, having
If the The behavior is conceptually similar to writing to a buffered writer. |
Just some additional thoughts:
|
The current semantics of The documentation already contains the following snippet:
Of course, documentation can always be improved. |
This at the very least should be documented as it's completely unexpected behavior at least for me. So errors returned by |
From @Darksonn's comment above
Is this something you think should be solved? Would it make sense that I look into writing a PR along the lines those lines? Or is it likely that the change wouldn't be accepted? |
If we can find a good solution, then I am ok with making the change, but it is probably best to discuss the details of the implementation idea with me before you spend a lot of time on implementing it. |
Cool, I'll have a look and if I find a way it could be solved then I'll draft a proposal to see what you think |
I ran into this before, I think the main point of confusion is how I don't really think the API should be changed but perhaps it should be documented loudly that this is something you may need to handle. |
Behaviour is definitely NOT same. Standard |
I agree that the behavior is not the same, but regarding the standard |
This issue is fixed now! |
Hi all, I know this issue is marked as closed and "fixed" but I am writing some Rust that performs many asynchronous HTTP requests spawned via tokio::spawn, and then each of the results are then again passed into a tokio::spawn. I have it set to parse these returned JSON blobs and write out to a .CSV file. However, I am noticing that towards the end of my program in some cases, in other cases not but when there have been a lot of writes I think?, I am getting an error where it's failing to get the handle to the mutex lock. I know this issue was a silent non-write, but this seems related in that it's a non-write but not silent? I hav eattached the backtrace here: |
It sounds like you are dealing with a different issue. Open a new issue. |
Version
tokio v1.14.0
tokio-macros v1.6.0 (proc-macro)
Platform
Linux 4.15.0-163-generic #171~16.04.1-Ubuntu SMP Wed Nov 10 11:19:51 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Description
While using write_all to write data to file I noticed that in some rare cases it does not write anything to the file despite the
await
ing on corresponding future.To reproduce the issue I've got the code from the write_all documentation:
This code should create
foo.txt
file in the current directory. To test the binary executable I created following bash script which calls test binary in a loop a checks if non-zero lengthfoo.txt
was created:I expected to see this happen:
bash script must run infinitely until Ctrl-C is pressed
Instead, this happened:
the script stops in less than a second reporting that zero-length
foo.txt
was created. Number of successful iteration varies from less than 10 to few hundreds.Additional information:
file.flush
call then file is always written as expected.async-std
runtime works as expected withoutfile.flush
.The text was updated successfully, but these errors were encountered: