Skip to content

stream.finished throws ERR_STREAM_PREMATURE_CLOSE when it shouldn't #45281

Closed
@watson

Description

@watson

Version

17.x, 18.x, 19.x

Platform

Darwin watson.localdomain 21.6.0 Darwin Kernel Version 21.6.0: Thu Sep 29 20:13:56 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T6000 arm64

Subsystem

stream

What steps will reproduce the bug?

On Node.js 17 and newer, the program below will call the finished callback with an ERR_STREAM_PREMATURE_CLOSE error.

const { Duplex, finished } = require('stream');

const stream = new Duplex({
  write (chunk, enc, cb) {
    setImmediate(cb);
  }
});

stream.end('foo');

finished(stream, { readable: false }, (err) => {
  console.log('Finished!', { err });
});

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

I'm not sure. For me it's unexpected and I'm not sure how to use finished in a scenario like this without getting this error.

What do you see instead?

I would expect the finished callback to be called without an error.

Additional information

As mentioned above, I'm not 100% if this is a bug or not. If it's not a bug, then the behaviour is at least unexpected and finished probably needs better documentation.

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