netliomax25-code opened PR #15196 from netliomax25-code:wasi-tls-p3-flush-after-write to bytecodealliance:main:
AsyncWriteConsumerin the p3 wasi-tls host never flushed theAsyncWriteit writes to.connector.senduses it to write the guest's cleartext into the TLS stream, and tokio-rustls'spoll_writehands the plaintext to rustls, writes TLS records to the transport until it would block, then returnsOk(n)with the rest still queued in rustls. Those records only go out on a later write, flush or shutdown. The transport is the 16 KiB ciphertext pipe, so when a guest makes one write of more than about 16 KiB (say an HTTP request with a 17 KB body) and then waits for the response, the peer never gets a complete record and both sides wait.The consumer now flushes after each write and keeps the write pending until the flush finishes, the same way
OutputStreamConsumerin wasmtime-wasi's p3 CLI host does. On the receive side the consumer writes into the in-memory pipe, where flushing is a no-op.To reproduce I drove the real
send/receive/connecthost functions against an in-process rustls server, with one cleartext write and the stream left open afterward. 1000 and 16000 bytes arrived; 17000 and 40960 bytes arrived as 0. With this change all four arrive in full. The unit test added here uses anAsyncWritethat only passes data on when flushed, and it fails without the change.cargo test -p wasmtime-wasi-tls --features nativetls, clippy and the per-feature checks pass locally. I couldn't build theopensslprovider here, but the change doesn't touch the providers.wasi-tls is tier 3, so I don't think this falls under the security policy. I didn't find an existing issue for it.
netliomax25-code removed the review request for wasmtime-wasi-reviewers from PR #15196.
netliomax25-code requested rvolosatovs for a review on PR #15196.
netliomax25-code requested wasmtime-wasi-reviewers for a review on PR #15196.
:memo: rvolosatovs submitted PR review:
Thanks! The fix looks ok, however I think we can simplify the implementation a bit and it feels like testing infrastructure is a bit excessive for this feature.
It seems that
flush_pendingwould not be necessary ifpoll_consumewould always flush, even after 0-length writes? (which is likely the right behavior, in fact)Roughly:
let mut src = src.as_direct(store); let remaining = src.remaining()?; if !remaining.is_empty() { match Pin::new(&mut *io).poll_write(cx, remaining) { Poll::Ready(Ok(0)) => { self.notify_dropped(Err(std::io::ErrorKind::WriteZero.into())); return Poll::Ready(Ok(StreamResult::Dropped)); } Poll::Ready(Ok(n)) => src.mark_read(n), Poll::Ready(Err(e)) => { self.notify_dropped(Err(e)); return Poll::Ready(Ok(StreamResult::Dropped)); } Poll::Pending if finish => return Poll::Ready(Ok(StreamResult::Cancelled)), Poll::Pending => return Poll::Pending, } } match Pin::new(io).poll_flush(cx) { Poll::Ready(Ok(())) => Poll::Ready(Ok(StreamResult::Completed)), Poll::Ready(Err(e)) => { self.notify_dropped(Err(e)); Poll::Ready(Ok(StreamResult::Dropped)) } Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)), Poll::Pending => Poll::Pending, }For test, could we perhaps reuse some of the existing trait impls on do something roughly like this?
#[tokio::test] async fn write_is_flushed_before_completing() -> wasmtime::Result<()> { let mut config = Config::default(); config.wasm_component_model_async(true); let engine = Engine::new(&config)?; let mut store = Store::new(&engine, ()); let (tx, rx) = oneshot::channel(); let consumer = AsyncWriteConsumer::new(BufWriter::new(Vec::default()), tx); StreamReader::new(&mut store, b"hello".to_vec())?.pipe(&mut store, consumer)?; let io = store.run_concurrent(async |_| rx.await).await??; let io = io?; assert_eq!(io.get_ref(), b"hello"); Ok(()) }
Last updated: Nov 19 2026 at 20:05 UTC