Skip to content
Closed
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
51 changes: 25 additions & 26 deletions src/mono/wasm/runtime/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,38 +104,37 @@ export async function http_wasm_get_streamed_response_bytes(res: ResponseExtensi
// the bufferPtr is pinned by the caller
const view = new Span(bufferPtr, bufferLength, MemoryViewType.Byte);
return wrap_as_cancelable_promise(async () => {
if (!res.__chunk && res.body) {
if (!res.body) {
return 0;
}

if (!res.__reader) {
res.__reader = res.body.getReader();
}

if (!res.__chunk) {
res.__chunk = await res.__reader.read();
res.__source_offset = 0;
}

let target_offset = 0;
let bytes_read = 0;
// loop until end of browser stream or end of C# buffer
while (res.__reader && res.__chunk && !res.__chunk.done) {
const remaining_source = res.__chunk.value.byteLength - res.__source_offset;
if (remaining_source === 0) {
res.__chunk = await res.__reader.read();
res.__source_offset = 0;
continue;// are we done yet
}

const remaining_target = view.byteLength - target_offset;
const bytes_copied = Math.min(remaining_source, remaining_target);
const source_view = res.__chunk.value.subarray(res.__source_offset, res.__source_offset + bytes_copied);

// copy available bytes
view.set(source_view, target_offset);
target_offset += bytes_copied;
bytes_read += bytes_copied;
res.__source_offset += bytes_copied;

if (target_offset == view.byteLength) {
return bytes_read;
}
if (res.__chunk.done) {
return 0;
}
return bytes_read;

const remaining_source = res.__chunk.value.byteLength - res.__source_offset;

if (remaining_source === 0) {
res.__chunk = await res.__reader.read();
res.__source_offset = 0;
return 0;
Copy link
Member

@pavelsavara pavelsavara Jan 16, 2023

Choose a reason for hiding this comment

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

I think that we could not return zero here as it means end of stream on C# side.
https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readasync?view=net-7.0

I also think we rather keep the loop over __reader.read(); in case that browser returned zero length chunk.
I worry it may happen when server sends zero sized chunk.

I think we need to block the promise/task until we receive at least one byte or until we get done.

Copy link
Member

Choose a reason for hiding this comment

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

I think we need to block the promise/task until we receive at least one byte or until we get done.

Yes, unless zero bytes were requested (in which case you can either return immediately or wait for at least one byte to be available but not consume it), Stream.Read must not return 0 unless it's EOF, and Stream.ReadAsync must not complete the task with 0 unless it's EOF.

Copy link
Author

@rabirland rabirland Jan 16, 2023

Choose a reason for hiding this comment

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

Waiting until C# buffer is full is wrong in the current code, thanks for figuring it out @rabirland !

You're welcome. To remove the zero return, you gotta bridge the if (!res.body) check. After that, res__reader.read() will block until data is received.

The if (remaining_source === 0) need to re-run the chunk read, instead of returning and waiting for the C# side to call it again.

}

const bytes_copied = Math.min(remaining_source, view.byteLength);
const source_view = res.__chunk.value.subarray(res.__source_offset, bytes_copied);
view.set(source_view, 0);
res.__source_offset += bytes_copied;

return bytes_copied;
});
}

Expand Down