Re: [whatwg/streams] Stream read hangs when stream is closed during a BYOB read (Issue #1321)

> Why isn't calling `close()` alone sufficient?

With the current `ReadableByteStreamController` API, we *always* need an explicit call to either `respond()` or `respondWithNewView()` in order to pass control of the BYOB view back to the BYOB reader. Otherwise, we don't really know when you are "done" using it.

For example, consider the following stream:
```javascript
const stream = new ReadableStream({
  type: 'bytes',
  async pull(controller) {
    const { byobRequest } = controller;
    const { buffer, byteOffset, byteLength } = byobRequest;

    const newBuffer = buffer.transfer();
    const bytesWritten = await fillBuffer(buffer, byteOffset, byteLength);

    if (bytesWritten === 0) {
      controller.close();
      byobRequest.respondWithNewView(new Uint8Array(newBuffer, byteOffset, 0));
    } else {
      byobRequest.respondWithNewView(new Uint8Array(newBuffer, byteOffset, byteLength));
    }
  }
});
```
Here, the stream is *transferring* the BYOB buffer before filling it. It can do this with an explicit `.transfer()`, or by passing it in the transfer list of a `Worker.postMessage()` call. The result is that the original `ArrayBuffer` of the BYOB view is now *detached*, and in order to fulfill the BYOB request, the stream must pass the "new" `ArrayBuffer` back to the stream.

The `ReadableStream` can't do this by itself at the moment that you call `close()`. It can see that its `ArrayBuffer` is now detached, but it doesn't know where the "non-detached" version of that buffer is currently located. (Even if it could, trying to take ownership of that buffer could cause all sorts of problems when the buffer is being used by another thread.)

I agree that it's annoying that you *have* to call `respond()` even if you never transfer the BYOB buffer (which is the likely case for most byte streams). I'm wondering if we can improve this.
* Can we have `close()` automatically call `respond(0)` if the BYOB buffer is not detached?
* Or would that cause too many headaches if you "conditionally" transfer the BYOB buffer, and now you're debugging weird edge cases where *sometimes* the stream gets stuck (instead of the status quo, where it *always* gets stuck)?

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1321#issuecomment-2277783549
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/streams/issues/1321/2277783549@github.com>

Received on Friday, 9 August 2024 11:58:43 UTC