- From: Mattias Buelens <notifications@github.com>
- Date: Sat, 13 Apr 2019 04:41:23 -0700
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/streams/issues/276/482801869@github.com>
<details>
<summary>Reproduction case</summary>
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Re-transferring streams</title>
</head>
<body>
<script id="worker-script">
if (self.importScripts) {
self.onmessage = (ev) => {
const rs = ev.data;
self.postMessage(rs, [rs]);
};
}
</script>
<script>
var rs = new ReadableStream({
start(c) {
c.enqueue('a');
c.enqueue('b');
c.enqueue('c');
}
});
var workerCode = document.getElementById('worker-script').innerHTML;
var worker = new Worker(URL.createObjectURL(new Blob([workerCode], { type: 'application/javascript' })));
worker.onmessage = async (ev) => {
var transferred = ev.data;
worker.terminate(); // comment this line to "fix" the pipe
transferred.pipeTo(new WritableStream({
write(x) {
console.log(x);
},
close() {
console.log('closed');
}
}));
};
worker.postMessage(rs, [rs]);
</script>
</body>
</html>
```
</details>
The problem is that the worker creates a new internal `MessageChannel` to transfer the `ReadableStream` back to the web page, which leads to this chain:
```
original ReadableStream <--> MessageChannel <--> worker's ReadableStream <--> MessageChannel <--> re-transferred ReadableStream
```
When the worker is terminated, it closes its side of both `MessageChannel`s, breaking the chain between the original stream and the re-transferred one.
A better solution would be for the worker to re-use the internal `MessagePort` that was used to transfer the original stream. That way, we don't create an additional internal `MessageChannel` that requires the worker to stay alive to forward messages between them. 😉
--
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/276#issuecomment-482801869
Received on Saturday, 13 April 2019 11:41:45 UTC