Repository navigation
fix(runtime): complete stream reads with values already buffered from async sources - #2437
Merged
vados-cosmonic merged 1 commit intoNov 11, 2026
Conversation
… async sources A host-provided stream read drained the values it already held, then awaited the asynchronous source for more before delivering them. When the buffered values were fewer than the read's count, the read waited until the source produced again, and a read cancelled meanwhile stranded the drained values. The read now waits on the source only when it has nothing to deliver, or when the source is synchronous and can be drained eagerly. The inline intrinsic and the external runtime carry the same change. Fixes bytecodealliance#2435
vados-cosmonic
approved these changes
Nov 11, 2026
vados-cosmonic
left a comment
Collaborator
There was a problem hiding this comment.
LGTM 🚀
Thanks @zion-sati this looks great!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2435.
When the host lowers a stream from an asynchronous source (an async iterator or a
ReadableStream) and a guest read finds values already buffered, for example the rest of a chunk larger than the previous read, the read drains them and then awaits the source again before completing. With an interactive source such as a socket or stdin, the guest stalls with data in hand until the peer sends more, and if the read is cancelled meanwhile the drained values are lost.We hit this running .NET programs compiled with NetWasm on Node.js through jco, where reads from stdin and from sockets stalled until the peer sent more data.
The read loop now waits on the source only when the read has nothing to deliver, or when the source is synchronous and can be drained eagerly up to the requested count. This complements the existing check inside the loop, which already returns as soon as values arrive from an awaited source.
The same change is applied to the inline intrinsic (
crates/js-component-bindgen/src/intrinsics/p3/async_stream.rs) and to the external runtime (packages/jco-cm-runtime/src/intrinsics/stream/injection.ts).Test
stream-lowers.tsadds a case where an async source produces 5000 bytes and, 100 ms later, 100 more, while the guest reads 4096 values at a time. The reads now return 4096, 904 and 100 values. Before the change, the second read waited for the second chunk and returned 1004.