Skip to content

Commit 4179233

Browse files
jasnelladuh95
authored andcommitted
stream: have broadcast clean factory listeners on cancel
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #66030 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent aefc7f0 commit 4179233

4 files changed

Lines changed: 145 additions & 25 deletions

File tree

‎lib/internal/streams/iter/broadcast.js‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const kOnBufferDrained = Symbol('kOnBufferDrained');
8888
const kOnEndDrained = Symbol('kOnEndDrained');
8989
const kOnCancel = Symbol('kOnCancel');
9090
const kPendingWriteRemoved = Symbol('kPendingWriteRemoved');
91+
const kSetFactorySignal = Symbol('kSetFactorySignal');
9192

9293
function raceEndWithSignal(promise, signal) {
9394
if (!signal) return promise;
@@ -119,6 +120,7 @@ class BroadcastImpl {
119120
#writer = null;
120121
#cachedMinCursor = 0;
121122
#cachedMinCursorConsumers = 0;
123+
#abortHandler;
122124
/** Cumulative byte size of buffered entries */
123125
#bufferedBytes = 0;
124126

@@ -133,6 +135,11 @@ class BroadcastImpl {
133135
this.#writer = writer;
134136
}
135137

138+
[kSetFactorySignal](signal) {
139+
this.#abortHandler = () => this.cancel(signal.reason);
140+
onSignalAbort(signal, this.#abortHandler);
141+
}
142+
136143
get backpressurePolicy() {
137144
return this.#options.backpressure;
138145
}
@@ -307,6 +314,7 @@ class BroadcastImpl {
307314
this.#consumers.clear();
308315
this.#waiters.clear();
309316
this.#cachedMinCursorConsumers = 0;
317+
this.#cleanupFactorySignal();
310318
const onCancel = this[kOnCancel];
311319
this[kOnCancel] = null;
312320
onCancel?.(reason);
@@ -416,6 +424,7 @@ class BroadcastImpl {
416424
this.#consumers.clear();
417425
this.#waiters.clear();
418426
this.#cachedMinCursorConsumers = 0;
427+
this.#cleanupFactorySignal();
419428
}
420429

421430
/**
@@ -435,10 +444,18 @@ class BroadcastImpl {
435444

436445
#notifyEndDrained() {
437446
if (this.#ended && this.#consumers.size === 0) {
447+
this.#cleanupFactorySignal();
438448
this[kOnEndDrained]?.();
439449
}
440450
}
441451

452+
#cleanupFactorySignal() {
453+
if (this.#abortHandler !== undefined) {
454+
this.#options.signal.removeEventListener('abort', this.#abortHandler);
455+
this.#abortHandler = undefined;
456+
}
457+
}
458+
442459
#recomputeMinCursor() {
443460
const { minCursor, minCursorConsumers } = getMinCursor(
444461
this.#consumers, this.#bufferStart + this.#buffer.length);
@@ -864,10 +881,6 @@ function wireBroadcastWriteSignal(entry, signal, resolve, reject, self) {
864881
signal.addEventListener('abort', onAbort, { __proto__: null, once: true });
865882
}
866883

867-
function onBroadcastCancel(broadcastImpl, signal) {
868-
onSignalAbort(signal, () => broadcastImpl.cancel(signal.reason));
869-
}
870-
871884
// =============================================================================
872885
// Public API
873886
// =============================================================================
@@ -902,7 +915,7 @@ function broadcast(options = { __proto__: null }) {
902915
broadcastImpl.setWriter(writer);
903916

904917
if (signal) {
905-
onBroadcastCancel(broadcastImpl, signal);
918+
broadcastImpl[kSetFactorySignal](signal);
906919
}
907920

908921
return { __proto__: null, writer, broadcast: broadcastImpl };

‎lib/internal/streams/iter/duplex.js‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ function duplex(options = { __proto__: null }) {
5252
backpressure: b?.backpressure ?? backpressure,
5353
});
5454

55-
const channelA = createDuplexChannel(aWriter, aReadable);
56-
const channelB = createDuplexChannel(bWriter, bReadable);
55+
let cleanupSignal;
56+
const onClose = () => cleanupSignal?.();
57+
const channelA = createDuplexChannel(aWriter, aReadable, onClose);
58+
const channelB = createDuplexChannel(bWriter, bReadable, onClose);
5759

5860
// Signal handler: fail both writers with the abort reason so consumers
5961
// see the error. This is an error-path shutdown, not a clean close.
@@ -62,6 +64,11 @@ function duplex(options = { __proto__: null }) {
6264
const reason = signal.reason;
6365
aWriter.fail(reason);
6466
bWriter.fail(reason);
67+
cleanupSignal?.();
68+
};
69+
cleanupSignal = () => {
70+
signal.removeEventListener('abort', abortBoth);
71+
cleanupSignal = undefined;
6572
};
6673
if (signal.aborted) {
6774
abortBoth();
@@ -74,7 +81,7 @@ function duplex(options = { __proto__: null }) {
7481
return [channelA, channelB];
7582
}
7683

77-
function createDuplexChannel(writer, readable) {
84+
function createDuplexChannel(writer, readable, onClose) {
7885
// A push readable has one shared consumer state. Keeping an iterator from
7986
// creation lets close() terminate that state even if no caller has iterated.
8087
const closeIterator = readable[SymbolAsyncIterator]();
@@ -85,7 +92,7 @@ function createDuplexChannel(writer, readable) {
8592
get writer() { return writer; },
8693
get readable() { return readable; },
8794
close() {
88-
closePromise ??= closeDuplexChannel(writer, closeIterator);
95+
closePromise ??= closeDuplexChannel(writer, closeIterator, onClose);
8996
return closePromise;
9097
},
9198
[SymbolAsyncDispose]() {
@@ -94,19 +101,23 @@ function createDuplexChannel(writer, readable) {
94101
};
95102
}
96103

97-
async function closeDuplexChannel(writer, closeIterator) {
98-
const result = writer.endSync();
99-
const endPromise = result < 0 ? writer.end() : undefined;
100-
const returnPromise = closeIterator.return();
104+
async function closeDuplexChannel(writer, closeIterator, onClose) {
105+
try {
106+
const result = writer.endSync();
107+
const endPromise = result < 0 ? writer.end() : undefined;
108+
const returnPromise = closeIterator.return();
101109

102-
if (endPromise !== undefined) {
103-
try {
104-
await SafePromiseAllReturnVoid([endPromise, returnPromise]);
105-
} catch (error) {
106-
if (!isConsumerReturnError(error)) throw error;
110+
if (endPromise !== undefined) {
111+
try {
112+
await SafePromiseAllReturnVoid([endPromise, returnPromise]);
113+
} catch (error) {
114+
if (!isConsumerReturnError(error)) throw error;
115+
}
116+
} else {
117+
await returnPromise;
107118
}
108-
} else {
109-
await returnPromise;
119+
} finally {
120+
onClose();
110121
}
111122
}
112123

‎lib/internal/streams/iter/share.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const {
7171

7272
const kNoShareError = Symbol('kNoShareError');
7373
const kShareCancelled = Symbol('kShareCancelled');
74+
const kSetFactorySignal = Symbol('kSetFactorySignal');
7475

7576
class ShareImpl {
7677
#source;
@@ -89,6 +90,7 @@ class ShareImpl {
8990
#cancelError = kNoShareError;
9091
#cachedMinCursor = 0;
9192
#cachedMinCursorConsumers = 0;
93+
#abortHandler;
9294
/** Cumulative byte size of buffered entries */
9395
#bufferedBytes = 0;
9496

@@ -104,6 +106,11 @@ class ShareImpl {
104106
return this.#consumers.size;
105107
}
106108

109+
[kSetFactorySignal](signal) {
110+
this.#abortHandler = () => this.cancel(signal.reason);
111+
onSignalAbort(signal, this.#abortHandler);
112+
}
113+
107114
pull(...args) {
108115
const { transforms, options } = parsePullArgs(args);
109116
const signal = options?.signal;
@@ -294,6 +301,7 @@ class ShareImpl {
294301
this.#consumers.clear();
295302
this.#buffer.clear();
296303
this.#bufferedBytes = 0;
304+
this.#cleanupFactorySignal();
297305

298306
for (let i = 0; i < this.#pullWaiters.length; i++) {
299307
this.#pullWaiters[i]();
@@ -415,6 +423,7 @@ class ShareImpl {
415423
this.#sourceError = wrapError(error);
416424
this.#sourceExhausted = true;
417425
} finally {
426+
if (this.#sourceExhausted) this.#cleanupFactorySignal();
418427
this.#pulling = false;
419428
for (let i = 0; i < this.#pullWaiters.length; i++) {
420429
this.#pullWaiters[i]();
@@ -466,6 +475,13 @@ class ShareImpl {
466475
this.#cachedMinCursorConsumers = minCursorConsumers;
467476
}
468477

478+
#cleanupFactorySignal() {
479+
if (this.#abortHandler !== undefined) {
480+
this.#options.signal.removeEventListener('abort', this.#abortHandler);
481+
this.#abortHandler = undefined;
482+
}
483+
}
484+
469485
#deleteConsumerFromMin(consumer) {
470486
if (consumer.cursor === this.#cachedMinCursor) {
471487
this.#cachedMinCursorConsumers--;
@@ -758,10 +774,6 @@ class SyncShareImpl {
758774
}
759775
}
760776

761-
function onShareCancel(shareImpl, signal) {
762-
onSignalAbort(signal, () => shareImpl.cancel(signal.reason));
763-
}
764-
765777
// =============================================================================
766778
// Public API
767779
// =============================================================================
@@ -791,7 +803,7 @@ function share(source, options = { __proto__: null }) {
791803
const shareImpl = new ShareImpl(normalized, opts);
792804

793805
if (signal) {
794-
onShareCancel(shareImpl, signal);
806+
shareImpl[kSetFactorySignal](signal);
795807
}
796808

797809
return shareImpl;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Flags: --experimental-stream-iter
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const { getEventListeners } = require('events');
7+
const {
8+
broadcast,
9+
duplex,
10+
push,
11+
share,
12+
text,
13+
} = require('stream/iter');
14+
15+
function abortListenerCount(signal) {
16+
return getEventListeners(signal, 'abort').length;
17+
}
18+
19+
async function testPushSignalLifetime() {
20+
const controller = new AbortController();
21+
const { writer, readable } = push({ signal: controller.signal });
22+
const iterator = readable[Symbol.asyncIterator]();
23+
24+
assert.strictEqual(abortListenerCount(controller.signal), 1);
25+
assert.strictEqual(writer.writeSync('x'), true);
26+
const ending = writer.end();
27+
assert.strictEqual(abortListenerCount(controller.signal), 1);
28+
29+
assert.strictEqual((await iterator.next()).done, false);
30+
assert.strictEqual((await iterator.next()).done, true);
31+
assert.strictEqual(await ending, 1);
32+
assert.strictEqual(abortListenerCount(controller.signal), 0);
33+
}
34+
35+
async function testBroadcastSignalLifetime() {
36+
const controller = new AbortController();
37+
const { writer, broadcast: shared } = broadcast({
38+
signal: controller.signal,
39+
});
40+
const iterator = shared.push()[Symbol.asyncIterator]();
41+
42+
assert.strictEqual(abortListenerCount(controller.signal), 1);
43+
assert.strictEqual(writer.writeSync('x'), true);
44+
const ending = writer.end();
45+
assert.strictEqual(abortListenerCount(controller.signal), 1);
46+
47+
assert.strictEqual((await iterator.next()).done, false);
48+
assert.strictEqual((await iterator.next()).done, true);
49+
assert.strictEqual(await ending, 1);
50+
assert.strictEqual(abortListenerCount(controller.signal), 0);
51+
}
52+
53+
async function testShareSignalLifetime() {
54+
const controller = new AbortController();
55+
const shared = share('x', { signal: controller.signal });
56+
const iterator = shared.pull()[Symbol.asyncIterator]();
57+
58+
assert.strictEqual(abortListenerCount(controller.signal), 1);
59+
assert.strictEqual((await iterator.next()).done, false);
60+
assert.strictEqual(abortListenerCount(controller.signal), 1);
61+
assert.strictEqual((await iterator.next()).done, true);
62+
assert.strictEqual(abortListenerCount(controller.signal), 0);
63+
}
64+
65+
async function testDuplexSignalLifetime() {
66+
const controller = new AbortController();
67+
const [channelA, channelB] = duplex({ signal: controller.signal });
68+
69+
assert.strictEqual(abortListenerCount(controller.signal), 1);
70+
await channelA.writer.write('x');
71+
const closing = channelA.close();
72+
assert.strictEqual(abortListenerCount(controller.signal), 1);
73+
74+
assert.strictEqual(await text(channelB.readable), 'x');
75+
await closing;
76+
assert.strictEqual(abortListenerCount(controller.signal), 0);
77+
}
78+
79+
Promise.all([
80+
testPushSignalLifetime(),
81+
testBroadcastSignalLifetime(),
82+
testShareSignalLifetime(),
83+
testDuplexSignalLifetime(),
84+
]).then(common.mustCall());

0 commit comments

Comments
 (0)