Skip to content

fs: don't poison File when blocking op is cancelled - #8291

Merged
ADD-SP merged 1 commit into
tokio-rs:masterfrom
kilyanni:fix/cancelled-fs-poisoning
Aug 9, 2026
Merged

fs: don't poison File when blocking op is cancelled#8291
ADD-SP merged 1 commit into
tokio-rs:masterfrom
kilyanni:fix/cancelled-fs-poisoning

Conversation

@kilyanni

@kilyanni kilyanni commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Motivation

tokio::fs::File tracks its blocking ops with an Idle -> Busy -> Idle state machine. When a blocking task is cancelled (e.g. on runtime shutdown) or fails to spawn, the error path returns mid-transition and leaves an invalid state, so the next op panics.

Affects read/write/seek/flush/set_len.

Found in the wild as a flaky JoinHandle polled after completion crash in Wasmer, which was tearing down a tokio runtime while host-file I/O was still in flight.

Solution

Reset to a valid Idle state before returning the error. Only error branches are touched, success/Pending paths are unchanged.
Adds a regression test.

@kilyanni
kilyanni force-pushed the fix/cancelled-fs-poisoning branch from fbb0ed6 to 74ed2b6 Compare July 16, 2026 17:36
@kilyanni
kilyanni marked this pull request as draft July 16, 2026 17:41
@kilyanni
kilyanni force-pushed the fix/cancelled-fs-poisoning branch 2 times, most recently from ba918b9 to 9b52514 Compare July 16, 2026 17:54
Previously, running certain tokio::fs::File ops (read, seek, etc)
after the backing blocking task was cancelled (e.g. due to a
runtime shutdown) would lead to the second operation panicking,
instead of erroring out gracefully.

This is due to the ops returing via `?` without resetting their
internal state into a valid one.
@kilyanni
kilyanni force-pushed the fix/cancelled-fs-poisoning branch from 9b52514 to 29add6c Compare July 16, 2026 17:54
@kilyanni
kilyanni marked this pull request as ready for review July 16, 2026 17:56
@mattiapitossi mattiapitossi added A-tokio Area: The main tokio crate M-fs Module: tokio/fs labels Jul 16, 2026
Samielakkad

This comment was marked as resolved.

@ADD-SP
ADD-SP self-requested a review August 8, 2026 17:00
@Darksonn

Darksonn commented Aug 9, 2026

Copy link
Copy Markdown
Member

It looks ok to me, but it looks like @ADD-SP also wants to review.

@ADD-SP ADD-SP 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!

@ADD-SP
ADD-SP merged commit 83e9c57 into tokio-rs:master Aug 9, 2026
102 checks passed
@kilyanni

kilyanni commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the merge!

@kilyanni
kilyanni deleted the fix/cancelled-fs-poisoning branch August 9, 2026 23:51
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 M-fs Module: tokio/fs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants