Skip to content

Conversation

wingo
Copy link
Collaborator

@wingo wingo commented Sep 16, 2025

No description provided.

@wingo wingo force-pushed the filesystem-io branch 2 times, most recently from b04d3fe to 7268371 Compare September 22, 2025 09:57
@wingo wingo changed the title wasi:[email protected]: Add tests for read/write/append wasi:[email protected]: Add tests for read/write/append Sep 22, 2025
@wingo wingo marked this pull request as ready for review September 22, 2025 14:34
Copy link
Contributor

@pchickey pchickey left a comment

Choose a reason for hiding this comment

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

LGTM but I want to tag in @alexcrichton for this one because I'm not confident about subtleties of stream handling here.

(result, data) = rx.read(data).await;
}
StreamResult::Dropped => {
assert_eq!(data.len(), bytes_read);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This assert may want to get deferred to the Ok return value of this function because a short-read might happen due to an error and the error may want to get returned instead of panicking here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am trying to understand the subtleties but I am a bit lost. If I understand you correctly, Dropped can be accompanied by data, is that right? If so we should add an n to Dropped: bytecodealliance/wit-bindgen#1396. If not I think this assertion will never cause a failed pread, because bytes_read and data.len() are both incremented as part of Complete.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You're correct that Dropped can be accompanied by data, yeah. The lack of n here is definitely an oversight and the assumption was that you'd look at the length of the buffer before/after to figure out the size (but then that's inconsistent with n on StreamResult::Complete, so n should be on Dropped and Cancelled as well for consistency)

I think I actually confused myself about this assertion and what it originally meant. I thought it was somehow taking the size parameter to the function into account but it isn't. Otherwise though you're also correct that this can panic in the case that Dropped communicates some data, which wasn't actually what I was thinking about but is true nonetheless!

Comment on lines +64 to +86
let (mut result, mut buf) = tx.write(data.to_vec()).await;
loop {
match result {
StreamResult::Complete(n) => {
assert!(n <= len - written);
written += n;
assert_eq!(buf.remaining(), len - written);
if buf.remaining() != 0 {
(result, buf) = tx.write_buf(buf).await;
} else {
break;
}
}
StreamResult::Dropped => {
panic!("receiver dropped the stream?");
}
StreamResult::Cancelled => {
break;
}
}
}
assert_eq!(buf.remaining(), len - written);
drop(tx);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This might be replacable with StreamWriter::write_all perhaps? (plus an assert that the result of write_all is empty)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FWIW... I have been avoiding write_all because I wanted to understand the stream mechanics. But, this is a WASI test suite, not a component model test suite, and so perhaps write_all is the right thing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

True! Either way seems reasonable to me

@pchickey
Copy link
Contributor

pchickey commented Oct 7, 2025

I have granted @alexcrichton write privs so that his review of this makes it green. I think this is ready to go?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants