Skip to content

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

wasi-http(p3): tie outbound I/O task lifetime to the store (#14743)

Roman Volosatovs147c37a27c8c
diff --git a/crates/wasi-http/src/p3/host/handler.rs b/crates/wasi-http/src/p3/host/handler.rs
index 6a4633c836..a73b280a02 100644
--- a/crates/wasi-http/src/p3/host/handler.rs
+++ b/crates/wasi-http/src/p3/host/handler.rs
@@ -27,47 +27,23 @@ impl Drop for AbortOnDropJoinHandle {
const DROPPED_FUTURE_ERROR: &str =
"Future indicating transmission result dropped without being resolved.";
-async fn io_task_result(
- rx: oneshot::Receiver<(
- Option<Arc<AbortOnDropJoinHandle>>,
- oneshot::Receiver<Result<(), Error>>,
- )>,
-) -> Result<(), Error> {
- let Ok((_io, io_result_rx)) = rx.await else {
- return Err(Error::InternalError(Some(DROPPED_FUTURE_ERROR.to_string())));
- };
- io_result_rx
- .await
+async fn io_task_result(rx: oneshot::Receiver<Result<(), Error>>) -> Result<(), Error> {
+ rx.await
.unwrap_or_else(|_| Err(Error::InternalError(Some(DROPPED_FUTURE_ERROR.to_string()))))
}
-fn send_dummy_io(
- result: Result<(), Error>,
- io_result_tx: oneshot::Sender<(
- Option<Arc<AbortOnDropJoinHandle>>,
- oneshot::Receiver<Result<(), Error>>,
- )>,
-) {
- let (tx, rx) = oneshot::channel();
- let _ = tx.send(result);
- let _ = io_result_tx.send((None, rx));
-}
-
fn send_dummy_io_err<T, D>(
store: &Accessor<T, D>,
mut getter: impl FnMut(&mut T) -> WasiHttpCtxView<'_>,
e: Error,
- io_result_tx: oneshot::Sender<(
- Option<Arc<AbortOnDropJoinHandle>>,
- oneshot::Receiver<Result<(), Error>>,
- )>,
+ io_result_tx: oneshot::Sender<Result<(), Error>>,
) -> HttpError
where
D: HasData,
{
let err_code =
store.with(|mut store| getter(store.as_context_mut().data_mut()).error_to_p3(&e));
- send_dummy_io(Err(e), io_result_tx);
+ _ = io_result_tx.send(Err(e));
err_code.into()
}
@@ -90,12 +66,7 @@ where
D: HasData,
T: 'static,
{
- // A handle to the I/O task, if spawned, will be sent on this channel
- // and kept as part of request body state
- let (io_task_tx, io_task_rx) = oneshot::channel();
-
- // A handle to the I/O task, if spawned, will be sent on this channel
- // along with the result receiver
+ // The I/O result will be sent on this channel
let (io_result_tx, io_result_rx) = oneshot::channel();
// Response processing result will be sent on this channel
@@ -110,9 +81,7 @@ where
let (req, options) =
req.into_http_with_getter(&mut store, io_task_result(io_result_rx), getter)?;
HttpResult::Ok(getter(store.data_mut()).hooks.send_request(
- // Attach a reference to the io task to the body so that it
- // isn't cancelled if the body is dropped.
- req.map(|body| body.with_state(io_task_rx).boxed_unsync()),
+ req,
options.as_deref().copied(),
Box::new(async {
// Forward the response processing result to `WasiHttpCtx` implementation
@@ -127,7 +96,7 @@ where
Ok(fut) => fut,
Err(e) => match e.downcast() {
Ok(err_code) => {
- send_dummy_io(Err(err_code.clone().into()), io_result_tx);
+ _ = io_result_tx.send(Err(err_code.clone().into()));
return Err(err_code.into());
}
Err(e) => {
@@ -152,24 +121,35 @@ where
let mut io = Box::into_pin(io);
let body = match io.as_mut().poll(&mut Context::from_waker(Waker::noop())) {
Poll::Ready(Ok(())) => {
- send_dummy_io(Ok(()), io_result_tx);
+ _ = io_result_tx.send(Ok(()));
body
}
Poll::Ready(Err(e)) => {
return Err(send_dummy_io_err(store, getter, e, io_result_tx));
}
Poll::Pending => {
- // I/O driver still needs to be polled, spawn a task and send handles to it
- let (tx, rx) = oneshot::channel();
+ // I/O driver still needs to be polled, spawn a task
+ let (done_tx, done_rx) = oneshot::channel::<()>();
let io = Arc::new(AbortOnDropJoinHandle(task::spawn(async move {
+ let _done_tx = done_tx;
let res = io.await;
debug!(?res, "`send_request` I/O future finished");
- _ = tx.send(res);
+ _ = io_result_tx.send(res);
})));
- _ = io_result_tx.send((Some(Arc::clone(&io)), rx));
- _ = io_task_tx.send(Arc::clone(&io));
- // Attach a reference to the io task to the body so that it
- // isn't cancelled if the body is dropped.
+ // The store holds a reference to the task until it finishes, so
+ // that dropping the store cancels any I/O not referenced by
+ // anything outside the store, e.g. a response body handed to the
+ // host.
+ let store_io = Arc::clone(&io);
+ store
+ .spawn(async move |_| {
+ let _io = store_io;
+ _ = done_rx.await;
+ Ok(())
+ })
+ .map_err(HttpError::trap)?;
+ // Attach a reference to the io task to the response body so that
+ // it isn't cancelled while the body is being read.
body.with_state(io).boxed_unsync()
}
};
diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rs
index 2b6737124c..4f509b4cd7 100644
--- a/crates/wasi-http/tests/all/p3/mod.rs
+++ b/crates/wasi-http/tests/all/p3/mod.rs
@@ -29,8 +29,19 @@ use wasmtime_wasi_http::{
foreach_p3_http!(assert_test_exists);
+#[derive(Default)]
struct TestHooks {
request_tx: Option<oneshot::Sender<http::Request<WasiBody>>>,
+ io_dropped_tx: Option<oneshot::Sender<()>>,
+}
+
+impl TestHooks {
+ fn new(request_tx: oneshot::Sender<http::Request<WasiBody>>) -> Self {
+ Self {
+ request_tx: Some(request_tx),
+ ..Self::default()
+ }
+ }
}
impl WasiHttpHooks for TestHooks {
@@ -63,6 +74,23 @@ impl WasiHttpHooks for TestHooks {
Box::new(async { Ok(()) }) as Box<dyn Future<Output = _> + Send>,
))
})
+ } else if let Some("p3-test-keep-open") = request.uri().authority().map(|v| v.as_str()) {
+ // Simulate a long-running connection by returning an `io` future
+ // that owns the request and never resolves. `io_dropped_tx` is
+ // dropped along with it, which the test observes to tell whether
+ // the I/O was cancelled.
+ let io_dropped_tx = self.io_dropped_tx.take();
+ Box::new(async {
+ Ok((
+ http::Response::new(Default::default()),
+ Box::new(async move {
+ let _request = request;
+ let _io_dropped_tx = io_dropped_tx;
+ std::future::pending::<()>().await;
+ Ok(())
+ }) as Box<dyn Future<Output = _> + Send>,
+ ))
+ })
} else {
Box::new(async move {
use http_body_util::BodyExt;
@@ -86,13 +114,15 @@ struct Ctx {
impl Ctx {
fn new(request_tx: oneshot::Sender<http::Request<WasiBody>>) -> Self {
+ Self::with_hooks(TestHooks::new(request_tx))
+ }
+
+ fn with_hooks(hooks: TestHooks) -> Self {
Self {
table: ResourceTable::default(),
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
http: WasiHttpCtx::new(),
- hooks: TestHooks {
- request_tx: Some(request_tx),
- },
+ hooks,
}
}
}
@@ -151,6 +181,14 @@ async fn run_http<E: Into<Error> + 'static>(
component_filename: &str,
req: http::Request<impl Body<Data = Bytes, Error = E> + Send + Sync + 'static>,
request_tx: oneshot::Sender<http::Request<WasiBody>>,
+) -> wasmtime::Result<Result<http::Response<Collected<Bytes>>, Option<ErrorCode>>> {
+ run_http_with_hooks(component_filename, req, TestHooks::new(request_tx)).await
+}
+
+async fn run_http_with_hooks<E: Into<Error> + 'static>(
+ component_filename: &str,
+ req: http::Request<impl Body<Data = Bytes, Error = E> + Send + Sync + 'static>,
+ hooks: TestHooks,
) -> wasmtime::Result<Result<http::Response<Collected<Bytes>>, Option<ErrorCode>>> {
let engine = test_programs_artifacts::engine(|config| {
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
@@ -158,7 +196,7 @@ async fn run_http<E: Into<Error> + 'static>(
});
let component = Component::from_file(&engine, component_filename)?;
- let mut store = Store::new(&engine, Ctx::new(request_tx));
+ let mut store = Store::new(&engine, Ctx::with_hooks(hooks));
let mut linker = Linker::new(&engine);
wasmtime_wasi::p2::add_to_linker_async(&mut linker)
@@ -669,6 +707,44 @@ async fn p3_http_proxy() -> Result<()> {
Ok(())
}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p3_http_proxy_store_drop_cancels_io() -> Result<()> {
+ let (_body_tx, body_rx) = futures::channel::mpsc::channel::<Result<_, ErrorCode>>(1);
+
+ // Tell the guest to forward the request to `http://p3-test-keep-open/`,
+ // which we handle specially in `TestHooks::send_request` above by
+ // immediately returning a response paired with an I/O future that never
+ // resolves.
+ let request = http::Request::builder()
+ .uri("http://localhost/")
+ .method(http::Method::GET)
+ .header("url", "http://p3-test-keep-open/");
+
+ let (io_dropped_tx, io_dropped_rx) = oneshot::channel();
+ let response = run_http_with_hooks(
+ P3_HTTP_PROXY_COMPONENT,
+ request.body(http_body_util::StreamBody::new(body_rx))?,
+ TestHooks {
+ io_dropped_tx: Some(io_dropped_tx),
+ ..TestHooks::default()
+ },
+ )
+ .await?
+ .unwrap();
+ assert_eq!(response.status().as_u16(), 200);
+
+ // The guest has exited, the response body has been consumed and the store
+ // has been dropped, so nothing references the I/O task anymore and it
+ // must be cancelled.
+ let result = tokio::time::timeout(std::time::Duration::from_secs(10), io_dropped_rx).await;
+ assert_eq!(
+ result,
+ Ok(Err(oneshot::Canceled)),
+ "I/O task should have been cancelled"
+ );
+ Ok(())
+}
+
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p3_http_forbidden_headers() -> Result<()> {
let request = http::Request::builder()