GeezerVison is showing github.com — stream: ensure iterator cleanup on done, reject, etc · nodejs/node@0356689
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/streams/iter
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/broadcast.js +2-7Lines changed: 2 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,7 +52,7 @@ const { | 52 | 52 | } = require('internal/streams/iter/from'); | 53 | 53 |
| 54 | 54 | const { | 55 | | - pull: pullWithTransforms, | | 55 | + pullWithConsumerCleanup, | 56 | 56 | } = require('internal/streams/iter/pull'); | 57 | 57 |
| 58 | 58 | const { | @@ -157,12 +157,7 @@ class BroadcastImpl { | 157 | 157 | // When no transforms, return rawConsumer directly (controller elided | 158 | 158 | // per PULL-02 optimization -- no transforms means no signal recipient). | 159 | 159 | if (transforms.length > 0 || signal) { | 160 | | - const pullArgs = [...transforms]; | 161 | | - if (signal) { | 162 | | - ArrayPrototypePush(pullArgs, | 163 | | - { __proto__: null, signal }); | 164 | | - } | 165 | | - return pullWithTransforms(rawConsumer, ...pullArgs); | | 160 | + return pullWithConsumerCleanup(rawConsumer, transforms, signal); | 166 | 161 | } | 167 | 162 | return rawConsumer; | 168 | 163 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/pull.js +72Lines changed: 72 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ const { | 12 | 12 | ArrayIsArray, | 13 | 13 | ArrayPrototypePush, | 14 | 14 | ArrayPrototypeSlice, | | 15 | + FunctionPrototypeCall, | 15 | 16 | PromisePrototypeThen, | 16 | 17 | PromiseResolve, | 17 | 18 | SymbolAsyncIterator, | @@ -896,6 +897,76 @@ function pull(source, ...args) { | 896 | 897 | }; | 897 | 898 | } | 898 | 899 |
| | 900 | +// Keep ownership of a bonded consumer outside the transform pipeline so it can | | 901 | +// be detached even when the pipeline never starts or terminates early. | | 902 | +function pullWithConsumerCleanup(source, transforms, signal) { | | 903 | + const sourceIterator = source[SymbolAsyncIterator](); | | 904 | + const pipelineSource = { | | 905 | + __proto__: null, | | 906 | + [SymbolAsyncIterator]() { | | 907 | + return sourceIterator; | | 908 | + }, | | 909 | + }; | | 910 | + const pipeline = signal === undefined ? | | 911 | + pull(pipelineSource, ...transforms) : | | 912 | + pull(pipelineSource, ...transforms, { __proto__: null, signal }); | | 913 | + let sourceClosed = false; | | 914 | + let abortHandler; | | 915 | + | | 916 | + function closeSource(method, value) { | | 917 | + if (sourceClosed) return; | | 918 | + sourceClosed = true; | | 919 | + if (abortHandler !== undefined) { | | 920 | + signal.removeEventListener('abort', abortHandler); | | 921 | + } | | 922 | + const close = sourceIterator[method] ?? sourceIterator.return; | | 923 | + if (typeof close === 'function') { | | 924 | + const result = FunctionPrototypeCall(close, sourceIterator, value); | | 925 | + PromisePrototypeThen(PromiseResolve(result), undefined, () => {}); | | 926 | + } | | 927 | + } | | 928 | + | | 929 | + if (signal !== undefined) { | | 930 | + abortHandler = () => closeSource('throw', signal.reason); | | 931 | + signal.addEventListener('abort', abortHandler, | | 932 | + { __proto__: null, once: true }); | | 933 | + if (signal.aborted) abortHandler(); | | 934 | + } | | 935 | + | | 936 | + return { | | 937 | + __proto__: null, | | 938 | + [SymbolAsyncIterator]() { | | 939 | + const iterator = pipeline[SymbolAsyncIterator](); | | 940 | + return { | | 941 | + __proto__: null, | | 942 | + next(value) { | | 943 | + return PromisePrototypeThen( | | 944 | + iterator.next(value), | | 945 | + (result) => { | | 946 | + if (result.done) closeSource('return'); | | 947 | + return result; | | 948 | + }, | | 949 | + (error) => { | | 950 | + closeSource('throw', error); | | 951 | + throw error; | | 952 | + }); | | 953 | + }, | | 954 | + return(value) { | | 955 | + closeSource('return', value); | | 956 | + return iterator.return(value); | | 957 | + }, | | 958 | + throw(error) { | | 959 | + closeSource('throw', error); | | 960 | + return iterator.throw(error); | | 961 | + }, | | 962 | + [SymbolAsyncIterator]() { | | 963 | + return this; | | 964 | + }, | | 965 | + }; | | 966 | + }, | | 967 | + }; | | 968 | +} | | 969 | + | 899 | 970 | // ============================================================================= | 900 | 971 | // Public API: pipeTo() and pipeToSync() | 901 | 972 | // ============================================================================= | @@ -1112,4 +1183,5 @@ module.exports = { | 1112 | 1183 | pipeToSync, | 1113 | 1184 | pull, | 1114 | 1185 | pullSync, | | 1186 | + pullWithConsumerCleanup, | 1115 | 1187 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/push.js +3-7Lines changed: 3 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,7 +46,7 @@ const { | 46 | 46 | } = require('internal/streams/iter/utils'); | 47 | 47 |
| 48 | 48 | const { | 49 | | - pull: pullWithTransforms, | | 49 | + pullWithConsumerCleanup, | 50 | 50 | } = require('internal/streams/iter/pull'); | 51 | 51 |
| 52 | 52 | const { | @@ -756,12 +756,8 @@ function push(...args) { | 756 | 756 | // Apply transforms lazily if provided | 757 | 757 | let readable; | 758 | 758 | if (transforms.length > 0) { | 759 | | - if (options.signal) { | 760 | | - readable = pullWithTransforms( | 761 | | - rawReadable, ...transforms, { __proto__: null, signal: options.signal }); | 762 | | - } else { | 763 | | - readable = pullWithTransforms(rawReadable, ...transforms); | 764 | | - } | | 759 | + readable = pullWithConsumerCleanup( | | 760 | + rawReadable, transforms, options.signal); | 765 | 761 | } else { | 766 | 762 | readable = rawReadable; | 767 | 763 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/share.js +2-8Lines changed: 2 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,8 +30,8 @@ const { | 30 | 30 | } = require('internal/streams/iter/from'); | 31 | 31 |
| 32 | 32 | const { | 33 | | - pull: pullWithTransforms, | 34 | 33 | pullSync: pullSyncWithTransforms, | | 34 | + pullWithConsumerCleanup, | 35 | 35 | } = require('internal/streams/iter/pull'); | 36 | 36 |
| 37 | 37 | const { | @@ -111,13 +111,7 @@ class ShareImpl { | 111 | 111 | const rawConsumer = this.#createRawConsumer(); | 112 | 112 |
| 113 | 113 | if (transforms.length > 0 || signal) { | 114 | | - if (signal) { | 115 | | - return pullWithTransforms( | 116 | | - rawConsumer, | 117 | | - ...transforms, | 118 | | - { __proto__: null, signal }); | 119 | | - } | 120 | | - return pullWithTransforms(rawConsumer, ...transforms); | | 114 | + return pullWithConsumerCleanup(rawConsumer, transforms, signal); | 121 | 115 | } | 122 | 116 | return rawConsumer; | 123 | 117 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-pull-async.js +36-1Lines changed: 36 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | const common = require('../common'); | 5 | 5 | const assert = require('assert'); | 6 | | -const { pull, from, text, tap } = require('stream/iter'); | | 6 | +const { | | 7 | + broadcast, | | 8 | + from, | | 9 | + pull, | | 10 | + push, | | 11 | + share, | | 12 | + tap, | | 13 | + text, | | 14 | +} = require('stream/iter'); | 7 | 15 |
| 8 | 16 | async function testPullIdentity() { | 9 | 17 | const data = await text(pull(from('hello-async'))); | @@ -199,6 +207,32 @@ async function testPullReturnWhileSourceNextPending() { | 199 | 207 | await next; | 200 | 208 | } | 201 | 209 |
| | 210 | +async function testTransformedConsumerReturnBeforeNext() { | | 211 | + const identity = (chunks) => chunks; | | 212 | + const pushed = push(identity); | | 213 | + const { broadcast: bc } = broadcast(); | | 214 | + const broadcastConsumer = bc.push(identity); | | 215 | + const shared = share(from('shared')); | | 216 | + const sharedConsumer = shared.pull(identity); | | 217 | + | | 218 | + assert.strictEqual(bc.consumerCount, 1); | | 219 | + assert.strictEqual(shared.consumerCount, 1); | | 220 | + | | 221 | + const cases = [ | | 222 | + [pushed.readable, common.mustCall( | | 223 | + () => assert.strictEqual(pushed.writer.canWrite, null))], | | 224 | + [broadcastConsumer, common.mustCall( | | 225 | + () => assert.strictEqual(bc.consumerCount, 0))], | | 226 | + [sharedConsumer, common.mustCall( | | 227 | + () => assert.strictEqual(shared.consumerCount, 0))], | | 228 | + ]; | | 229 | + | | 230 | + for (const [readable, verify] of cases) { | | 231 | + await readable[Symbol.asyncIterator]().return(); | | 232 | + verify(); | | 233 | + } | | 234 | +} | | 235 | + | 202 | 236 | async function testPullSignalAbortWithTransformWhileSourceNextPending() { | 203 | 237 | const source = { | 204 | 238 | [Symbol.asyncIterator]() { | @@ -447,6 +481,7 @@ async function testTransformOptionsNotShared() { | 447 | 481 | testPullSignalAbortMidIteration(), | 448 | 482 | testPullSignalAbortWhileSourceNextPending(), | 449 | 483 | testPullReturnWhileSourceNextPending(), | | 484 | + testTransformedConsumerReturnBeforeNext(), | 450 | 485 | testPullSignalAbortWithTransformWhileSourceNextPending(), | 451 | 486 | testPullConsumerBreakCleanup(), | 452 | 487 | testPullTransformReturnsPromise(), | |
|