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

> I think the proper model here is that transferring a ReadableStream transfers the ability to read from the stream - the end that writes to the stream is not transferred, and the connection between the writing end and the reading end remains in place.

That's the idea, yes.

The problem is mostly technical: we have to figure out *how to make that work*. There's a whole discussion on the original issue about all the peculiarities we have to deal with, such as:
* We not only have to transfer the message port, but we also have to "fast forward" the stream's state to match the original stream's state (things like `[[state]]`, `[[storedError]]`, `[[disturbed]]`,...).
* The `WritableStream` may still have chunks in its queue which have not yet been processed by the "cross-realm sink". If the stream is transferred, then we must *also* transfer this queue, so we don't lose those chunks. (I suppose the same applies to a queued close or abort request.)
* Similarly, the `ReadableStream` may have already received chunks from the "cross-realm source" and put them in its own `stream.[[controller]].[[queue]]` to be read later. Again, if the stream is transferred, then we must *also* transfer this queue. (I guess `[[closeRequested]]` also needs to be transferred?)
  * We *might* be able to avoid this, if we can assert that every chunk we pull from the cross-realm source *immediately* fulfills a pending read request. In other words, if the `[[queue]]` of a cross-realm readable stream is always empty.
* `writer.write()`, `writer.close()` and `writer.abort()` return a promise that resolves when the underlying sink has processed the requested operation. However, if we transfer the `WritableStream` while those promises are still pending, then we lose access to the message port, so we can no longer communicate with the "real" underlying sink on the other end. We still need to figure out what should happen with these pending promises. Do we leave them pending forever? Resolve them, assuming that "everything will be fine"? Or maybe reject with an error to indicate that the stream was transferred?

-- 
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-939374120

Received on Saturday, 9 October 2021 23:09:55 UTC