Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions crates/misc/component-async-tests/src/resource_stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::Result;
use wasmtime::component::{Accessor, AccessorTask, HostStream, Resource, StreamWriter};
use wasmtime::component::{
Accessor, AccessorTask, GuardedStreamWriter, Resource, StreamReader, StreamWriter,
};
use wasmtime_wasi::p2::IoView;

use super::Ctx;
Expand Down Expand Up @@ -36,31 +38,31 @@ impl bindings::local::local::resource_stream::HostWithStore for Ctx {
async fn foo<T: 'static>(
accessor: &Accessor<T, Self>,
count: u32,
) -> wasmtime::Result<HostStream<Resource<ResourceStreamX>>> {
) -> wasmtime::Result<StreamReader<Resource<ResourceStreamX>>> {
struct Task {
tx: StreamWriter<Option<Resource<ResourceStreamX>>>,
tx: StreamWriter<Resource<ResourceStreamX>>,

count: u32,
}

impl<T> AccessorTask<T, Ctx, Result<()>> for Task {
async fn run(self, accessor: &Accessor<T, Ctx>) -> Result<()> {
let mut tx = self.tx;
let mut tx = GuardedStreamWriter::new(accessor, self.tx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about this in the context of rebasing with wasi:sockets -- is this necessary? Tasks can only be cancelled for two reasons:

  1. The store is dropped - in this case there's no need to do further cleanup
  2. The AbortHandle is triggered, but in this case it's statically known we just forget about that and ignore it.

Given that does WASI actually need Guarded*?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we don't need it, but it saves us from having to remember to close the handle explicitly before returning, which isn't too onerous for this simple task, but could become a maintenance hazard later.

wasmtime-wasi-http has some pretty complicated control flow, where keeping track of what to close when without RAII would be a real pain.

for _ in 0..self.count {
let item =
accessor.with(|mut view| view.get().table().push(ResourceStreamX))?;
tx.write_all(accessor, Some(item)).await;
tx.write_all(Some(item)).await;
}
Ok(())
}
}

let (tx, rx) = accessor.with(|mut view| {
let instance = view.instance();
instance.stream::<_, _, Option<_>>(&mut view)
instance.stream(&mut view)
})?;
accessor.spawn(Task { tx, count });
Ok(rx.into())
Ok(rx)
}
}

Expand Down
Loading