From 52ddd5fe0ac0e47acd9dbee1cd00ee560cacda68 Mon Sep 17 00:00:00 2001 From: Jitse De Smet Date: Thu, 3 Dec 2026 16:10:51 +0000 Subject: [PATCH 1/3] Close wrapped sync iterators on early end WrappingIterator now calls return() on an unfinished sync iterator when it is closed or destroyed early, as for...of does on break, so generator finally blocks run. Respects destroySource: false. --- asynciterator.ts | 6 ++++ test/WrappingIterator-test.js | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/asynciterator.ts b/asynciterator.ts index 38f5872..6d341d9 100644 --- a/asynciterator.ts +++ b/asynciterator.ts @@ -2136,6 +2136,12 @@ export class WrappingIterator extends AsyncIterator { } return null; }; + // Close an unfinished iterator, as `for...of` does on `break` + source.destroy = () => { + const unfinishedIterator = iterator; + iterator = null; + unfinishedIterator?.return?.(); + }; } // Process any other readable source else { diff --git a/test/WrappingIterator-test.js b/test/WrappingIterator-test.js index 3210989..60fc7a7 100644 --- a/test/WrappingIterator-test.js +++ b/test/WrappingIterator-test.js @@ -705,4 +705,63 @@ describe('wrap', () => { }); }); }); + + describe('with a generator source that is ended early', () => { + let generatorCleanedUp; + function * generateNumbers() { + try { + yield 1; + yield 2; + } + finally { + generatorCleanedUp = true; + } + } + + beforeEach(() => { + generatorCleanedUp = false; + }); + + it('should clean up the generator when destroyed', () => { + const iterator = wrap(generateNumbers()); + iterator.read(); + iterator.destroy(); + generatorCleanedUp.should.be.true; + }); + + it('should clean up the generator when closed', done => { + const iterator = wrap(generateNumbers()); + iterator.read(); + iterator.on('end', () => { + generatorCleanedUp.should.be.true; + done(); + }); + iterator.close(); + }); + + it('should return the generator when destroyed before its promise resolves', async () => { + const source = generateNumbers(); + sinon.spy(source, 'return'); + const sourcePromise = Promise.resolve(source); + wrap(sourcePromise).destroy(); + await sourcePromise; + source.return.should.have.been.calledOnce; + }); + + it('should not clean up the generator when destroySource is false', () => { + const iterator = wrap(generateNumbers(), { destroySource: false }); + iterator.read(); + iterator.destroy(); + generatorCleanedUp.should.be.false; + }); + }); + + describe('with an iterator source that ends by itself', () => { + it('should not call return on the iterator', async () => { + const source = [1, 2][Symbol.iterator](); + source.return = sinon.spy(); + expect(await wrap(source).toArray()).to.deep.equal([1, 2]); + source.return.should.not.have.been.called; + }); + }); }); From 02e4059d740ab34f88c3f03bc0261200dc84dc53 Mon Sep 17 00:00:00 2001 From: Jitse De Smet Date: Thu, 3 Dec 2026 16:13:04 +0000 Subject: [PATCH 2/3] Simplify iterator return handling and tests Use isFunction like the other optional calls, and assert via return() spies in a single test suite, also covering the buffering options path. --- asynciterator.ts | 6 +++-- test/WrappingIterator-test.js | 49 +++++++++++++++-------------------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/asynciterator.ts b/asynciterator.ts index 6d341d9..b0190f7 100644 --- a/asynciterator.ts +++ b/asynciterator.ts @@ -2136,11 +2136,13 @@ export class WrappingIterator extends AsyncIterator { } return null; }; - // Close an unfinished iterator, as `for...of` does on `break` + // Close an unfinished iterator, as `for...of` does on `break`; + // clear it first so its cleanup code cannot re-enter it source.destroy = () => { const unfinishedIterator = iterator; iterator = null; - unfinishedIterator?.return?.(); + if (unfinishedIterator !== null && isFunction(unfinishedIterator.return)) + unfinishedIterator.return(); }; } // Process any other readable source diff --git a/test/WrappingIterator-test.js b/test/WrappingIterator-test.js index 60fc7a7..6d3cb78 100644 --- a/test/WrappingIterator-test.js +++ b/test/WrappingIterator-test.js @@ -706,60 +706,53 @@ describe('wrap', () => { }); }); - describe('with a generator source that is ended early', () => { - let generatorCleanedUp; - function * generateNumbers() { - try { + describe('with a generator source', () => { + let source; + beforeEach(() => { + source = (function * () { yield 1; yield 2; - } - finally { - generatorCleanedUp = true; - } - } - - beforeEach(() => { - generatorCleanedUp = false; + }()); + sinon.spy(source, 'return'); }); - it('should clean up the generator when destroyed', () => { - const iterator = wrap(generateNumbers()); + it('should return the generator when destroyed early', () => { + const iterator = wrap(source); iterator.read(); iterator.destroy(); - generatorCleanedUp.should.be.true; + source.return.should.have.been.calledOnce; }); - it('should clean up the generator when closed', done => { - const iterator = wrap(generateNumbers()); + it('should return the generator when closed early', done => { + const iterator = wrap(source); iterator.read(); iterator.on('end', () => { - generatorCleanedUp.should.be.true; + source.return.should.have.been.calledOnce; done(); }); iterator.close(); }); it('should return the generator when destroyed before its promise resolves', async () => { - const source = generateNumbers(); - sinon.spy(source, 'return'); const sourcePromise = Promise.resolve(source); wrap(sourcePromise).destroy(); await sourcePromise; source.return.should.have.been.calledOnce; }); - it('should not clean up the generator when destroySource is false', () => { - const iterator = wrap(generateNumbers(), { destroySource: false }); + it('should return the generator when wrapped with buffering options and destroyed early', () => { + wrap(source, { maxBufferSize: 1 }).destroy(); + source.return.should.have.been.calledOnce; + }); + + it('should not return the generator when destroySource is false', () => { + const iterator = wrap(source, { destroySource: false }); iterator.read(); iterator.destroy(); - generatorCleanedUp.should.be.false; + source.return.should.not.have.been.called; }); - }); - describe('with an iterator source that ends by itself', () => { - it('should not call return on the iterator', async () => { - const source = [1, 2][Symbol.iterator](); - source.return = sinon.spy(); + it('should not return the generator when it ends by itself', async () => { expect(await wrap(source).toArray()).to.deep.equal([1, 2]); source.return.should.not.have.been.called; }); From be8c38b030412cd379e2cf48bd35eaff39f458f7 Mon Sep 17 00:00:00 2001 From: Jitse De Smet Date: Thu, 3 Dec 2026 16:15:09 +0000 Subject: [PATCH 3/3] Emit errors thrown by iterator return() and document destroySource A throwing return() no longer escapes _end, which left the wrapper unended and crashed the process on close(). --- asynciterator.ts | 11 +++++++++-- test/WrappingIterator-test.js | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/asynciterator.ts b/asynciterator.ts index b0190f7..464b6a2 100644 --- a/asynciterator.ts +++ b/asynciterator.ts @@ -2141,8 +2141,14 @@ export class WrappingIterator extends AsyncIterator { source.destroy = () => { const unfinishedIterator = iterator; iterator = null; - if (unfinishedIterator !== null && isFunction(unfinishedIterator.return)) - unfinishedIterator.return(); + if (unfinishedIterator !== null && isFunction(unfinishedIterator.return)) { + try { + unfinishedIterator.return(); + } + catch (error) { + this.emit('error', error); + } + } }; } // Process any other readable source @@ -2201,6 +2207,7 @@ export class WrappingIterator extends AsyncIterator { @function @param [source] The source this iterator generates items from @param {object} [options] Settings of the iterator + @param {boolean} [options.destroySource=true] Whether the source should be destroyed when this iterator is closed or destroyed; an unfinished sync iterator is closed with `return()` @returns {module:asynciterator.AsyncIterator} A new iterator with the items from the given iterator */ export function wrap(source?: MaybePromise> | null, diff --git a/test/WrappingIterator-test.js b/test/WrappingIterator-test.js index 6d3cb78..7c4efb4 100644 --- a/test/WrappingIterator-test.js +++ b/test/WrappingIterator-test.js @@ -757,4 +757,19 @@ describe('wrap', () => { source.return.should.not.have.been.called; }); }); + + describe('with an iterator source whose return throws', () => { + it('should emit the error and still end when closed early', done => { + const returnError = new Error('return failed'); + const iterator = wrap({ next: () => ({ value: 1 }), return: () => { throw returnError; } }); + const emittedErrors = []; + iterator.read(); + iterator.on('error', error => emittedErrors.push(error)); + iterator.on('end', () => { + emittedErrors.should.deep.equal([returnError]); + done(); + }); + iterator.close(); + }); + }); });