Skip to content

Commit

Permalink
Fix flaky tests in preview2 streams (#6763)
Browse files Browse the repository at this point in the history
* preview2::pipe sink_write_stream test bugfix

I didn't realize this test was racy, but it makes sense because the
worker is consuming these writes in another thread. usually the
foreground thread wins, but we observed the other case once in CI

* fix wasi-preview2-components-sync poll_oneoff_stdio flaky test

I made a typo translating the sync calls to async calls, and in the sync
implementation stream::blocking_write was calling async stream::write,
so it was missing the logic to make the write blocking.

This would in turn cause panics in rust's std::io::stdio::print_to
when writes return less than the length of the input.

thank you to @silentbicycle for autoclave, which we used to reproduce
this bug.
  • Loading branch information
Pat Hickey authored Jul 24, 2023
1 parent f4eeb02 commit e81af41
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions crates/wasi/src/preview2/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,13 @@ mod test {
assert_eq!(len, chunk.len());
assert_eq!(state, StreamState::Open);

// But I expect this to block additional writes:
// It is possible for subsequent writes to be refused, but it is nondeterminstic because
// the worker task consuming them is in another thread:
let (len, state) = writer.write(chunk.clone()).unwrap();
assert_eq!(len, 0);
assert_eq!(state, StreamState::Open);
if !(len == 0 || len == chunk.len()) {
unreachable!()
}

tokio::time::timeout(REASONABLE_DURATION, writer.ready())
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/preview2/preview2/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ pub mod sync {
stream: OutputStream,
bytes: Vec<u8>,
) -> Result<(u64, streams::StreamStatus), streams::Error> {
in_tokio(async { AsyncHost::write(self, stream, bytes).await })
in_tokio(async { AsyncHost::blocking_write(self, stream, bytes).await })
.map(|(a, b)| (a, b.into()))
.map_err(streams::Error::from)
}
Expand Down

0 comments on commit e81af41

Please sign in to comment.