Skip to content

Fix async stream cancel corrupting read/write state#12704

Merged
dicej merged 1 commit intobytecodealliance:mainfrom
jellevandenhooff:fix-async-stream-cancel-state-corruption
Mar 3, 2026
Merged

Fix async stream cancel corrupting read/write state#12704
dicej merged 1 commit intobytecodealliance:mainfrom
jellevandenhooff:fix-async-stream-cancel-state-corruption

Conversation

@jellevandenhooff
Copy link
Copy Markdown
Contributor

Ran into an issue with async stream.cancel-read causing a panic. The attached change makes my guest pass, and the test reproduces. However, it is LLM written and I am not 100% confident in either the diagnosis or the description of the bug in the test.

@jellevandenhooff jellevandenhooff requested a review from a team as a code owner March 2, 2026 19:19
@jellevandenhooff jellevandenhooff requested review from fitzgen and removed request for a team March 2, 2026 19:19
@dicej dicej requested review from dicej and removed request for fitzgen March 2, 2026 19:37
Copy link
Copy Markdown
Contributor

@dicej dicej left a comment

Choose a reason for hiding this comment

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

Thanks for catching and fixing this, @jellevandenhooff.

Currently, not many guest binding generators support cancellation, and while the Rust binding generator does, it uses the synchronous versions of cancel-read and cancel-write, allowing this to fly under the radar.

@alexcrichton's recent PR downgrades the panic to a trap, but it's great to have it fixed altogether.

@dicej dicej added this pull request to the merge queue Mar 2, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Mar 2, 2026
@jellevandenhooff jellevandenhooff force-pushed the fix-async-stream-cancel-state-corruption branch from 14635c0 to 1b56f8a Compare March 2, 2026 21:57
@jellevandenhooff
Copy link
Copy Markdown
Contributor Author

Happy to contribute! Rebased on that change.

(As shameless plug, I've been discovering these issues working on wasip3 go guest at https://github.com/jellevandenhooff/go/commits/wasip3-prototype/. I run the entire go test suite with WASMTIME=~/hack/wasmtime/target/release/wasmtime GOARCH=wasm32 GOOS=wasip3 GOWASIRUNTIME=wasmtime ./bin/go tool dist test. The guest bindings support cancellation for all socket operations through go's built-in netpoll mechanisms, which has been good for stress-testing wasmtime.)

@alexcrichton
Copy link
Copy Markdown
Member

The stress testing is very much appreciated, thank you so much for the reports here and fixes! The surface area of async is broad enough that it's tough to ensure comprehensive coverage so it's great to get a different runtime tickling different bits than JS/C/Rust/Python have all tickled so far

When `stream.cancel-read` or `stream.cancel-write` is called with the
`async` option and the cancel cannot complete immediately (returns
BLOCKED), the code was unconditionally transitioning the read/write
state from GuestReady to Open. This destroyed the buffer address/count
info stored in GuestReady, causing incorrect behavior when the host
producer/consumer later tried to access the stream state.

Guard the GuestReady -> Open state transition with a check that the
cancel did not return BLOCKED. When blocked, the cancel is still
in-flight and the read/write state must be preserved until the cancel
completes.

Adds a regression test that creates a host StreamProducer, starts an
async read (BLOCKED), then async-cancels (BLOCKED), and waits for
cancel completion.
@jellevandenhooff jellevandenhooff force-pushed the fix-async-stream-cancel-state-corruption branch from 1b56f8a to ece5372 Compare March 2, 2026 23:08
@dicej dicej added this pull request to the merge queue Mar 2, 2026
Merged via the queue into bytecodealliance:main with commit 8de60f1 Mar 3, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants