Skip to content

Commit

Permalink
stream wit definition eliminates stream-error
Browse files Browse the repository at this point in the history
In WebAssembly/wasi-io#38 we got review feedback
to eliminate the stream-error in favor of the empty error in wit
`result<a>`.

This means we cant use trappable error anymore, and therefore leads to
all this other unsightly transformation of the streams trait definition
and all its call sites.

We'll fix the wasmtime-wit-bindgen macro to support this case better in
the future, but rn we gotta stay synchronized with upstream

On the upside this showed us that the host stream trait design doesnt
differentiate between a runtime and a trapping error, so lets fix that
next

introduce a StreamRuntimeError, use it in filesystem streams

and fix an incorrect error transformation in the filesystem read impl

fill in fixmes for distinguishing a stream runtime error

delete outdated fixmes: downcast is now guaranteed by child resource tracking
  • Loading branch information
Pat Hickey committed Aug 15, 2023
1 parent e3b4954 commit c4279b8
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 232 deletions.
2 changes: 0 additions & 2 deletions crates/wasi/src/preview2/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ wasmtime::component::bindgen!({
async: true,
trappable_error_type: {
"wasi:filesystem/types"::"error-code": Error,
"wasi:io/streams"::"stream-error": Error,
},
with: {
"wasi:filesystem/types": crate::preview2::bindings::filesystem::types,
Expand Down Expand Up @@ -61,7 +60,6 @@ pub mod sync {
async: false,
trappable_error_type: {
"wasi:filesystem/types"::"error-code": Error,
"wasi:io/streams"::"stream-error": Error,
},
with: {
"wasi:filesystem/types": crate::preview2::bindings::sync_io::filesystem::types,
Expand Down
30 changes: 21 additions & 9 deletions crates/wasi/src/preview2/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::preview2::{StreamState, Table, TableError};
use crate::preview2::{StreamRuntimeError, StreamState, Table, TableError};
use bytes::{Bytes, BytesMut};
use std::sync::Arc;

Expand Down Expand Up @@ -152,24 +152,36 @@ impl FileInputStream {
}
}

pub(crate) fn read_result(
r: Result<usize, std::io::Error>,
) -> Result<(usize, StreamState), std::io::Error> {
fn read_result(r: Result<usize, std::io::Error>) -> Result<(usize, StreamState), anyhow::Error> {
match r {
Ok(0) => Ok((0, StreamState::Closed)),
Ok(n) => Ok((n, StreamState::Open)),
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => Ok((0, StreamState::Open)),
Err(e) => Err(e),
Err(e)
if matches!(
rustix::io::Errno::from_io_error(&e),
Some(rustix::io::Errno::IO)
) =>
{
Err(StreamRuntimeError::from(anyhow::anyhow!(e)).into())
}
Err(e) => Err(e.into()),
}
}

pub(crate) fn write_result(
r: Result<usize, std::io::Error>,
) -> Result<(usize, StreamState), std::io::Error> {
fn write_result(r: Result<usize, std::io::Error>) -> Result<(usize, StreamState), anyhow::Error> {
match r {
Ok(0) => Ok((0, StreamState::Closed)),
Ok(n) => Ok((n, StreamState::Open)),
Err(e) => Err(e),
Err(e)
if matches!(
rustix::io::Errno::from_io_error(&e),
Some(rustix::io::Errno::IO)
) =>
{
Err(StreamRuntimeError::from(anyhow::anyhow!(e)).into())
}
Err(e) => Err(e.into()),
}
}

Expand Down
7 changes: 5 additions & 2 deletions crates/wasi/src/preview2/host/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,18 @@ impl<T: WasiView> types::Host for T {
})
.await;

let (bytes_read, state) = crate::preview2::filesystem::read_result(r)?;
let (bytes_read, state) = match r? {
0 => (0, true),
n => (n, false),
};

buffer.truncate(
bytes_read
.try_into()
.expect("bytes read into memory as u64 fits in usize"),
);

Ok((buffer, state.is_closed()))
Ok((buffer, state))
}

async fn write(
Expand Down
Loading

0 comments on commit c4279b8

Please sign in to comment.