Skip to content

fs: wait for in-flight writes in File::metadata - #8751

Merged
Darksonn merged 1 commit into
tokio-rs:masterfrom
b84330808:fs-file-metadata-inflight
Nov 8, 2026
Merged

Darksonn merged 1 commit into
tokio-rs:masterfrom
b84330808:fs-file-metadata-inflight

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

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, as in the example from #7378. Thus, wait for in-flight operations first, like try_clone, sync_all and set_times already do.

Refs: #7378

I used Claude Code to write this change.

`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]>
@Darksonn Darksonn added C-bug Category: This is a bug. A-tokio Area: The main tokio crate M-fs Module: tokio/fs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 1, 2026
@darkraider01

Copy link
Copy Markdown
Contributor

I wonder if we want metadata() to guarantee this ordering in the first place, given that #7378 was closed as wontfix.

Comment thread tokio/src/fs/file.rs
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems like this would change more than just the ordering of metadata queries.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 Darksonn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks. I'm ok with this change.

@Darksonn
Darksonn merged commit cf5041b into tokio-rs:master Nov 8, 2026
109 checks passed
@Darksonn Darksonn removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 8, 2026
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-fs Module: tokio/fs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants