Skip to content

Stream async iteration breaks when the loop body calls .destroy(null) #23890

@TimothyGu

Description

@TimothyGu
  • Version: master
  • Platform: all
  • Subsystem: stream
'use strict';

const { Readable } = require('stream');

const r = new Readable({
  objectMode: true,
  read() {
    this.push('asdf');
    this.push('hehe');
    // note: no this.push(null);
    // also reproducible with setTimeout(() => { this.push(null); }, 1000); here.
  }
});

(async () => {
  for await (const a of r) {
    r.destroy(null);
  }
  console.log('done');
})();

Prints

(node:90745) ExperimentalWarning: Readable[Symbol.asyncIterator] is an experimental feature. This feature could change at any time
(node:90745) UnhandledPromiseRejectionWarning: Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at Readable.onclose (internal/streams/end-of-stream.js:64:36)
    at Readable.emit (events.js:182:13)
    at emitCloseNT (internal/streams/destroy.js:59:8)
    at internalTickCallback (internal/process/next_tick.js:72:19)
    at process._tickCallback (internal/process/next_tick.js:47:5)
    at Function.Module.runMain (internal/modules/cjs/loader.js:763:11)
    at startup (internal/bootstrap/node.js:308:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:878:3)
(node:90745) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an asyncfunction without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:90745) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

It seems the ERR_STREAM_PREMATURE_CLOSE is erroneous, as I would expect the loop to terminate without error. Replacing r.destroy(null) with r.destroy(new Error()) would result in a rejected promise with the newly created error, as expected.

/cc @mcollina @devsnek

Refs: #23730 #23785

Metadata

Metadata

Assignees

No one assigned

    Labels

    streamIssues and PRs related to the stream subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions