Skip to content
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

Fix lost Waker instances with async stdio streams #8782

Merged
merged 2 commits into from
Jun 12, 2024

Conversation

alexcrichton
Copy link
Member

This commit fixes a bug in the Subscribe trait implementation for AsyncStd{in,out}Stream structures in the wasmtime-wasi crate. Previously these implementations would create a future for the duration of a single poll but then the future was dropped which could lead to lost wakeups as the waker is gone after the future is dropped. The fix was to use a tokio::sync::Mutex here instead of a std::sync::Mutex and leave some comments about why contention isn't expected.

Closes #8781

This commit fixes a bug in the `Subscribe` trait implementation for
`AsyncStd{in,out}Stream` structures in the `wasmtime-wasi` crate.
Previously these implementations would create a future for the duration
of a single `poll` but then the future was dropped which could lead to
lost wakeups as the waker is gone after the future is dropped. The fix
was to use a `tokio::sync::Mutex` here instead of a `std::sync::Mutex`
and leave some comments about why contention isn't expected.

Closes bytecodealliance#8781
@alexcrichton alexcrichton requested a review from a team as a code owner June 12, 2024 16:30
@alexcrichton alexcrichton requested review from fitzgen and removed request for a team June 12, 2024 16:30
@github-actions github-actions bot added the wasi Issues pertaining to WASI label Jun 12, 2024
});

let mut buf = [0; 100];
let n = read.read(&mut buf).await.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be more comfortable with this test if the amount read was in excess of the buffer sizes in the io::duplex and the AsyncWriteStream. You could just lower those to 32 each.

Copy link
Contributor

@pchickey pchickey left a comment

Choose a reason for hiding this comment

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

Great catch. Thank you!

@alexcrichton alexcrichton enabled auto-merge June 12, 2024 20:07
@alexcrichton alexcrichton added this pull request to the merge queue Jun 12, 2024
Merged via the queue into bytecodealliance:main with commit 34d2a08 Jun 12, 2024
36 checks passed
@alexcrichton alexcrichton deleted the fix-deadlock branch June 12, 2024 20:31
@coolreader18
Copy link
Contributor

Why not just use tokio::sync::Mutex::blocking_lock()?

@alexcrichton
Copy link
Member Author

Semantically these are all non-blocking functions, not blocking ones, so blocking on contention isn't appropriate for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasi Issues pertaining to WASI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AsyncStdoutStream causes hangs after first write
3 participants