Re: [whatwg/streams] Transferable streams: the double transfer problem (#1063)

Hmm, delaying the transfer of the queued chunks is indeed quite risky.

While the proposed solution could work for `WritableStream`, I'm not so sure it'd work for a `ReadableStream`. The current spec tries to avoid sending chunks from the original stream to the transferred stream that are not yet being requested, so ideally the transferred stream's queue is always empty. However, other spec changes might *make it possible* for that queue to become non-empty. For example, with #1103, you might do this:
```javascript
const controller = new AbortController();
const reader = readable.getReader({ signal: controller.signal });
reader.read(); // causes the cross-realm readable to send a "pull" message
controller.abort(); // discards the pending read request
// At this point, we have no pending read requests, but we are already pulling...

// After some time, we receive a "chunk" message and put the chunk in the queue.
// Which means that if you now transfer the stream...
worker.postMessage(readable, { transfer: [readable] });
// ...we have to do something with the chunks in the queue first.
```
I think it's better if we transfer the entire queue synchronously as part of the transfer steps. In the transfer-receiving steps, we would re-enqueue those chunks with `controller.enqueue()` and `writer.write()`.

> 1. We give up the nice abstraction of the "cross-realm identity transform" because it makes the following stuff harder. Instead we have separate transfer logic for ReadableStream and WritableStream.

So the transfer steps for `ReadableStream` would acquire a reader, and for `WritableStream` they would acquire a writer? I think I would like that *better* than the current solution with `pipeTo()`, actually. 😛

> After step 9, realm A has been "unhooked" and can safely be destroyed.

Is step 9 needed? Can't A close itself immediately after step 7?


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

Received on Tuesday, 16 November 2021 14:37:40 UTC