Repository navigation
fs: wait for in-flight writes in File::metadata - #8751
Conversation
`File::write` returns once the data is handed to a blocking task, and `File::metadata` queries the file right away without waiting for that task. So `write_all` followed by `metadata` can report the old length. Thus, wait for in-flight operations first, like `try_clone`, `sync_all` and `set_times` already do. Refs: tokio-rs#7378 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
I wonder if we want |
| pub async fn metadata(&self) -> io::Result<Metadata> { | ||
| // Wait for in-flight operations so that the returned metadata (such | ||
| // as the file length) reflects earlier writes. | ||
| self.inner.lock().await.complete_inflight().await; |
There was a problem hiding this comment.
It seems like this would change more than just the ordering of metadata queries.
There was a problem hiding this comment.
Fair question. #7378 was closed because writes can't be made completion-based under AsyncWrite, and this doesn't change that. It only makes metadata() wait for the in-flight operation the way sync_all, sync_data, set_len, try_clone and set_times already do.
What else changes: metadata() now takes the inner lock and waits for whatever operation is in flight, not only a write (for example a read or seek whose future was dropped). If that in-flight write failed, metadata() doesn't return the error; as with the other methods, it's kept in last_write_err and returned by the next write or flush.
If the preferred answer is to document "call flush() before metadata()" instead, I'm happy to close this.
Darksonn
left a comment
There was a problem hiding this comment.
Thanks. I'm ok with this change.
File::writereturns once the data is handed to a blocking task, andFile::metadataqueries the file right away without waiting for that task. Sowrite_allfollowed bymetadatacan report the old length, as in the example from #7378. Thus, wait for in-flight operations first, liketry_clone,sync_allandset_timesalready do.Refs: #7378
I used Claude Code to write this change.