Skip to content

Stream emitting same error twice under certain circomstances #36053

@watson

Description

@watson
  • Version: 12.18.1 and newer
  • Platform: Darwin watson 19.6.0 Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64 x86_64
  • Subsystem: stream

What steps will reproduce the bug?

Run this code on Node.js 12.18.1 or newer and notice how the error is emitted on b twice. On any older Node.js version it's only emitted once.

const { Readable, PassThrough, Writable, pipeline } = require('stream')

const a = new Readable({
  read () {
    this.destroy(new Error('foo'));
  }
})
const b = new PassThrough()
const c = new Writable()

a.once('error', (err) => {
  console.log('emitting error on b')
  b.emit('error', err) // Note: you're not supposed to emit error manually!
})

b.on('error', (err) => {
  console.log('b got error:', err.message)
})

pipeline(a.pipe(b), c, (err) => {
  console.log('end of pipeline, err:', err.message)
})

If I change b.emit('error', err) to b.destroy(err) the error goes away. Using destroy is best practise and the user isn't supposed to emit error events manually on streams like in the above code. However, this was how we told users to do it previously, so a lot of code most likely exists in the wild expecting this to be possible and emitting the same error twice can have bad consequences.

Thoughts:

  1. It would be cool if we could warn users if they manually emitted an error event on a stream.
  2. It would be nice if we could make this odd behaviour go away even if the user emits error manually.

This regression was most likely introduced in #30869.

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