Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Flush stdout on every chunk accepted from the p3 stream (#13654)

Tomasz Andrzejakd8e667ccde00
diff --git a/crates/test-programs/src/bin/p3_cli_stdout_flush.rs b/crates/test-programs/src/bin/p3_cli_stdout_flush.rs
new file mode 100644
index 0000000000..ccc73c9cbe
--- /dev/null
+++ b/crates/test-programs/src/bin/p3_cli_stdout_flush.rs
@@ -0,0 +1,29 @@
+//! Writes a prompt without a newline, then waits for stdin.
+
+use test_programs::p3::{wasi, wit_stream};
+
+struct Component;
+
+test_programs::p3::export!(Component);
+
+impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
+ async fn run() -> Result<(), ()> {
+ let (mut tx, rx) = wit_stream::new();
+ futures::join!(
+ async { wasi::cli::stdout::write_via_stream(rx).await.unwrap() },
+ async {
+ assert!(tx.write_all(b"READY".to_vec()).await.is_empty());
+
+ let (mut stdin, _result) = wasi::cli::stdin::read_via_stream();
+ let _ = stdin.read(Vec::with_capacity(1)).await;
+
+ drop(tx);
+ },
+ );
+ Ok(())
+ }
+}
+
+fn main() {
+ unreachable!();
+}
diff --git a/crates/wasi/src/p3/cli/host.rs b/crates/wasi/src/p3/cli/host.rs
index d2a1be06b1..cce2faa3b4 100644
--- a/crates/wasi/src/p3/cli/host.rs
+++ b/crates/wasi/src/p3/cli/host.rs
@@ -84,6 +84,39 @@ impl<D> StreamProducer<D> for InputStreamProducer {
struct OutputStreamConsumer {
tx: Pin<Box<dyn AsyncWrite + Send + Sync>>,
result_tx: Option<oneshot::Sender<ErrorCode>>,
+ flush_pending: bool,
+}
+
+impl OutputStreamConsumer {
+ fn poll_flush(
+ &mut self,
+ cx: &mut Context<'_>,
+ finish: bool,
+ ) -> Poll<wasmtime::Result<StreamResult>> {
+ match self.tx.as_mut().poll_flush(cx) {
+ Poll::Ready(Ok(())) => {
+ self.flush_pending = false;
+ Poll::Ready(Ok(StreamResult::Completed))
+ }
+ Poll::Ready(Err(e)) => self.dropped(e),
+ Poll::Pending => {
+ if finish {
+ self.flush_pending = false;
+ Poll::Ready(Ok(StreamResult::Cancelled))
+ } else {
+ self.flush_pending = true;
+ Poll::Pending
+ }
+ }
+ }
+ }
+
+ fn dropped(&mut self, err: io::Error) -> Poll<wasmtime::Result<StreamResult>> {
+ if let Some(tx) = self.result_tx.take() {
+ let _ = tx.send(io_error_to_error_code(err));
+ }
+ Poll::Ready(Ok(StreamResult::Dropped))
+ }
}
impl<D> StreamConsumer<D> for OutputStreamConsumer {
@@ -96,6 +129,10 @@ impl<D> StreamConsumer<D> for OutputStreamConsumer {
src: Source<Self::Item>,
finish: bool,
) -> Poll<wasmtime::Result<StreamResult>> {
+ if self.flush_pending {
+ return self.poll_flush(cx, finish);
+ }
+
let mut src = src.as_direct(store);
let buf = src.remaining();
@@ -110,18 +147,12 @@ impl<D> StreamConsumer<D> for OutputStreamConsumer {
return Poll::Ready(Ok(StreamResult::Completed));
}
match self.tx.as_mut().poll_write(cx, buf) {
+ Poll::Ready(Ok(0)) => self.dropped(io::ErrorKind::WriteZero.into()),
Poll::Ready(Ok(n)) => {
src.mark_read(n);
- Poll::Ready(Ok(StreamResult::Completed))
- }
- Poll::Ready(Err(e)) => {
- let _ = self
- .result_tx
- .take()
- .unwrap()
- .send(io_error_to_error_code(e));
- Poll::Ready(Ok(StreamResult::Dropped))
+ self.poll_flush(cx, finish)
}
+ Poll::Ready(Err(e)) => self.dropped(e),
Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),
Poll::Pending => Poll::Pending,
}
@@ -228,6 +259,7 @@ impl<U> stdout::HostWithStore<U> for WasiCli {
OutputStreamConsumer {
tx: Box::into_pin(tx),
result_tx: Some(result_tx),
+ flush_pending: false,
},
)?;
FutureReader::new(&mut store, async {
@@ -253,6 +285,7 @@ impl<U> stderr::HostWithStore<U> for WasiCli {
OutputStreamConsumer {
tx: Box::into_pin(tx),
result_tx: Some(result_tx),
+ flush_pending: false,
},
)?;
FutureReader::new(&mut store, async {
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index c0c786660f..62b19e2a1d 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -2926,6 +2926,54 @@ start a print 1234
Ok(())
}
+ #[test]
+ #[cfg_attr(not(feature = "component-model-async"), ignore)]
+ fn p3_cli_stdout_flush() -> Result<()> {
+ let mut child = get_wasmtime_command()?
+ .arg("-Sp3")
+ .arg("-Wcomponent-model-async")
+ .arg(P3_CLI_STDOUT_FLUSH_COMPONENT)
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .spawn()?;
+
+ let mut stdin = child.stdin.take().unwrap();
+ let mut stdout = child.stdout.take().unwrap();
+
+ let (tx, rx) = std::sync::mpsc::channel();
+ let reader = thread::spawn(move || {
+ let mut buf = [0u8; 5];
+ let res = stdout.read_exact(&mut buf).map(|()| buf);
+ let _ = tx.send(res);
+ });
+
+ let buf = match rx.recv_timeout(std::time::Duration::from_secs(10)) {
+ Ok(Ok(buf)) => buf,
+ Ok(Err(e)) => {
+ let _ = child.kill();
+ let _ = child.wait();
+ return Err(e.into());
+ }
+ Err(_) => {
+ let _ = child.kill();
+ let _ = child.wait();
+ let _ = reader.join();
+ panic!("guest stdout was not flushed");
+ }
+ };
+ assert_eq!(&buf, b"READY");
+
+ stdin.write_all(b"x")?;
+ drop(stdin);
+
+ let output = child.wait_with_output()?;
+ reader.join().unwrap();
+ assert!(output.status.success());
+
+ Ok(())
+ }
+
#[tokio::test]
async fn p3_cli_serve_post_return() -> Result<()> {
let server = WasmtimeServe::new(P3_CLI_SERVE_POST_RETURN_COMPONENT, move |cmd| {