Skip to content

fix(runtime): complete stream reads with values already buffered from async sources - #2437

Merged
vados-cosmonic merged 1 commit into
bytecodealliance:mainfrom
zion-sati:fix/stream-read-delivers-buffered-values
Nov 11, 2026
Merged

vados-cosmonic merged 1 commit into
bytecodealliance:mainfrom
zion-sati:fix/stream-read-delivers-buffered-values

Conversation

@zion-sati

@zion-sati zion-sati commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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.ts adds 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.

… 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 vados-cosmonic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM 🚀

Thanks @zion-sati this looks great!

@vados-cosmonic
vados-cosmonic added this pull request to the merge queue Nov 11, 2026
Merged via the queue into bytecodealliance:main with commit 0da38a4 Nov 11, 2026
74 checks passed
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.

P3 stream host injection waits on the source although it already holds values for the read

2 participants