Skip to content

stream pipeline kills process when writeStream is closed  #52622

Description

@DevT0ny

Version

v21.7.1

Platform

Linux

Subsystem

No response

What steps will reproduce the bug?

To reproduce this follow this

echo "Hello from src file" > /tmp/src.txt

Now run this script

//@ts-check

const fs = require('node:fs')
const { pipeline } = require('node:stream/promises')
const { promisify } = require('node:util')

const main = async () => {
  const dst = '/tmp/dest.txt'
  const writeStream = fs.createWriteStream(dst, { flags: 'a' })

  const close = promisify(writeStream.close)
  await close.call(writeStream)

  console.log(writeStream.closed, writeStream.writableEnded)

  await pipeline(fs.createReadStream('/tmp/src.txt'), writeStream)
  const dstfile = await fs.promises.readFile(dst, { encoding: 'utf8' })
  console.log({ dstfile })
}

main()
  .then(() => {
    console.log('done')
  })
  .catch((err) => {
    console.log('error')
    console.error(err)
  })
  .finally(() => {
    console.log('over')
  })

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

No response

What is the expected behavior? Why is that the expected behavior?

pipeline function should throw error saying writeStream is closed.

What do you see instead?

prints true true then exits with code 0. true true is part of script and its working as intended.

Additional information

No response

Activity

  1. benjamingr commented on Apr 24, 2024

    @benjamingr
    Member

    @ronag wdyt?

  2. benjamingr commented on Apr 24, 2024

    @benjamingr
    Member

    (also weren't we deprecating close?)

  3. ronag commented on Apr 25, 2024

    @ronag
    Member

    Yea. I think we should throw if pipelining to a destroyed or ended stream. However, that will be semver major.

  4. benjamingr commented on Apr 25, 2024

    @benjamingr
    Member

    Yes definitely semver-major and I agree it's better behavior conceptually

  5. jakecastelli commented on May 31, 2024

    @jakecastelli
    Member

    Hi guys, I'd like to take some time to work on this issue, @ronag may I ask what should be the error code for this one if we decide to throw an error?

    I think we have ERR_STREAM_CANNOT_PIPE that sounds really close

    E('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable', Error);

    But I think this one probably should be something like Cannot pipe, the stream has already ended / destroyed / closed.

    Being said should I add new error code(s) or there is an existing one that fits the purpose.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions