Skip to content

Close wrapped sync iterators on early end - #111

Open
jitsedesmet wants to merge 3 commits into
RubenVerborgh:mainfrom
jitsedesmet:claude/quirky-fermat-c8dcxb
Open

jitsedesmet wants to merge 3 commits into
RubenVerborgh:mainfrom
jitsedesmet:claude/quirky-fermat-c8dcxb

Conversation

@jitsedesmet

Copy link
Copy Markdown

Needed for: jitsedesmet/rdf-stores.js#1

wrap() never called return() on a wrapped sync iterator, so closing or destroying the resulting AsyncIterator early skipped generator finally blocks.

function* numbers() { try { yield 1; yield 2; } finally { console.log('released'); } }
const it = wrap(numbers());
it.once('data', () => it.destroy()); // now logs 'released'

Changes

  • The sync-iterator adapter in WrappingIterator now has a destroy() that calls return() on an unfinished iterator, as for...of does on break. It goes through the existing destroySource path, so destroySource: false still skips it.
  • The adapter clears the iterator before calling return(), so cleanup code cannot re-enter it.
  • If return() throws, the error is emitted as 'error' and the wrapper still ends.
  • An iterator that runs to the end is not returned.
  • wrap() JSDoc now documents destroySource.

Tests

New wrap suites cover: destroy, close, destroy before a promised source resolves, the buffering-options path, destroySource: false, natural end, and a throwing return(). Coverage stays at 100%.


Generated by Claude Code

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.
Use isFunction like the other optional calls, and assert via return() spies
in a single test suite, also covering the buffering options path.
A throwing return() no longer escapes _end, which left the wrapper unended
and crashed the process on close().
Comment thread asynciterator.ts
};
// Close an unfinished iterator, as `for...of` does on `break`;
// clear it first so its cleanup code cannot re-enter it
source.destroy = () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we'll also want this behaviour on calling asynciterator's close().
So instead of implementing source.destroy, source._end() could also be implemented, which should cover both cases.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close() is already covered:

I'd keep destroy over _end: WrappingIterator only ever calls destroy() on its source, in _end() and when a promised source resolves after the iterator is done. Nothing calls _end() on a source, so an adapter _end() would need new calls in both places, duplicating the existing destroy() handling, destroySource check included.

~ Claude

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't see there also was a test for closing early. Should be fine then indeed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants