Skip to content

Commit cffd2cc

Browse files
Revert "stream: revert fix cloned webstreams not being unref'd"
This reverts commit 8d20b64. PR-URL: #53144 Fixes: #53143 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent e6d9fbe commit cffd2cc

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

lib/internal/webstreams/transfer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class CrossRealmTransformReadableSource {
146146
error);
147147
port.close();
148148
};
149+
150+
port.unref();
149151
}
150152

151153
start(controller) {
@@ -219,6 +221,7 @@ class CrossRealmTransformWritableSink {
219221
port.close();
220222
};
221223

224+
port.unref();
222225
}
223226

224227
start(controller) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { ok } = require('node:assert');
5+
6+
// This test verifies that cloned ReadableStream and WritableStream instances
7+
// do not keep the process alive. The test fails if it timesout (it should just
8+
// exit immediately)
9+
10+
const rs1 = new ReadableStream();
11+
const ws1 = new WritableStream();
12+
13+
const [rs2, ws2] = structuredClone([rs1, ws1], { transfer: [rs1, ws1] });
14+
15+
ok(rs2 instanceof ReadableStream);
16+
ok(ws2 instanceof WritableStream);

test/parallel/test-whatwg-webstreams-transfer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,23 @@ const theData = 'hello';
454454
tracker.verify();
455455
});
456456
457+
// We create an interval to keep the event loop alive while
458+
// we wait for the stream read to complete. The reason this is needed is because there's
459+
// otherwise nothing to keep the worker thread event loop alive long enough to actually
460+
// complete the read from the stream. Under the covers the ReadableStream uses an
461+
// unref'd MessagePort to communicate with the main thread. Because the MessagePort
462+
// is unref'd, it's existence would not keep the thread alive on its own. There was previously
463+
// a bug where this MessagePort was ref'd which would block the thread and main thread
464+
// from terminating at all unless the stream was consumed/closed.
465+
const i = setInterval(() => {}, 1000);
466+
457467
parentPort.onmessage = tracker.calls(({ data }) => {
458468
assert(isReadableStream(data));
459469
const reader = data.getReader();
460470
reader.read().then(tracker.calls((result) => {
461471
assert(!result.done);
462472
assert(result.value instanceof Uint8Array);
473+
clearInterval(i);
463474
}));
464475
parentPort.close();
465476
});

0 commit comments

Comments
 (0)