Skip to content

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

fix panic in `Instance::set_consumer` when write end is already dropped (#11669)

Joel Dice15b69138ecce
diff --git a/crates/misc/component-async-tests/tests/scenario/streams.rs b/crates/misc/component-async-tests/tests/scenario/streams.rs
index a0a934de61..3434dff629 100644
--- a/crates/misc/component-async-tests/tests/scenario/streams.rs
+++ b/crates/misc/component-async-tests/tests/scenario/streams.rs
@@ -249,3 +249,50 @@ pub async fn async_closed_streams() -> Result<()> {
Ok(())
}
+
+mod closed_stream {
+ wasmtime::component::bindgen!({
+ path: "wit",
+ world: "closed-stream-guest",
+ exports: { default: store | async },
+ });
+}
+
+#[tokio::test]
+pub async fn async_closed_stream() -> Result<()> {
+ let engine = Engine::new(&config())?;
+
+ let component = make_component(
+ &engine,
+ &[test_programs_artifacts::ASYNC_CLOSED_STREAM_COMPONENT],
+ )
+ .await?;
+
+ let mut linker = Linker::new(&engine);
+
+ wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
+
+ let mut store = Store::new(
+ &engine,
+ Ctx {
+ wasi: WasiCtxBuilder::new().inherit_stdio().build(),
+ table: ResourceTable::default(),
+ continue_: false,
+ wakers: Arc::new(Mutex::new(None)),
+ },
+ );
+
+ let instance = linker.instantiate_async(&mut store, &component).await?;
+ let guest = closed_stream::ClosedStreamGuest::new(&mut store, &instance)?;
+ instance
+ .run_concurrent(&mut store, async move |accessor| {
+ let stream = guest.local_local_closed_stream().call_get(accessor).await?;
+
+ let (tx, mut rx) = mpsc::channel(1);
+ accessor.with(move |store| stream.pipe(store, PipeConsumer::new(tx)));
+ assert!(rx.next().await.is_none());
+
+ Ok(())
+ })
+ .await?
+}
diff --git a/crates/misc/component-async-tests/tests/test_all.rs b/crates/misc/component-async-tests/tests/test_all.rs
index 833983ba39..1c4c76e35d 100644
--- a/crates/misc/component-async-tests/tests/test_all.rs
+++ b/crates/misc/component-async-tests/tests/test_all.rs
@@ -30,7 +30,7 @@ use scenario::round_trip_many::{
async_round_trip_many_stackful, async_round_trip_many_stackless,
async_round_trip_many_synchronous, async_round_trip_many_wait,
};
-use scenario::streams::async_closed_streams;
+use scenario::streams::{async_closed_stream, async_closed_streams};
use scenario::transmit::{
async_cancel_callee, async_cancel_caller, async_cancel_transmit, async_intertask_communication,
async_poll_stackless, async_poll_synchronous, async_readiness, async_synchronous_transmit,
diff --git a/crates/misc/component-async-tests/wit/test.wit b/crates/misc/component-async-tests/wit/test.wit
index 41488a8125..5a6a62f2fb 100644
--- a/crates/misc/component-async-tests/wit/test.wit
+++ b/crates/misc/component-async-tests/wit/test.wit
@@ -174,6 +174,10 @@ interface sleep-post-return {
run: async func(sleep-time-millis: u64);
}
+interface closed-stream {
+ get: func() -> stream<u8>;
+}
+
world yield-caller {
import continue;
import ready;
@@ -338,3 +342,7 @@ world sleep-post-return-caller {
import sleep-post-return;
export sleep-post-return;
}
+
+world closed-stream-guest {
+ export closed-stream;
+}
diff --git a/crates/test-programs/src/bin/async_closed_stream.rs b/crates/test-programs/src/bin/async_closed_stream.rs
new file mode 100644
index 0000000000..187de49090
--- /dev/null
+++ b/crates/test-programs/src/bin/async_closed_stream.rs
@@ -0,0 +1,22 @@
+mod bindings {
+ wit_bindgen::generate!({
+ path: "../misc/component-async-tests/wit",
+ world: "closed-stream-guest",
+ });
+
+ use super::Component;
+ export!(Component);
+}
+
+use {bindings::exports::local::local::closed_stream::Guest, wit_bindgen::StreamReader};
+
+struct Component;
+
+impl Guest for Component {
+ fn get() -> StreamReader<u8> {
+ bindings::wit_stream::new().1
+ }
+}
+
+// Unused function; required since this file is built as a `bin`:
+fn main() {}
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
index cc02d3d261..78d9390e69 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -2108,7 +2108,10 @@ impl Instance {
state.push_future(Box::pin(future));
}
- WriteState::Dropped => unreachable!(),
+ WriteState::Dropped => {
+ let reader = transmit.read_handle;
+ self.host_drop_reader(store.0, reader, kind).unwrap();
+ }
}
}