Skip to content

Inconsistent stream events using destroy method #25373

Closed
@greguz

Description

@greguz
  • Version: v8.15.0
  • Platform: Linux 4.20.0-arch1-1-ARCH
  • Subsystem: stream

As for official Node.js docs, with a custom Writable, the only standard way to know if the stream has finished, is to listen the finish event.

const { Writable } = require('stream')

const stream = new Writable({
  write(chunk, encoding, callback) {
    callback()
  },
})

stream
  .on('close', () => console.log('close'))
  .on('error', () => console.log('error'))
  .on('finish', () => console.log('finish'))

stream.write('test')
stream.end()

The code above works correctly on both node 8 and 10, with the finish event emitted.

const { Writable } = require('stream')

const stream = new Writable({
  write(chunk, encoding, callback) {
    callback()
  },
})

stream
  .on('close', () => console.log('close'))
  .on('error', () => console.log('error'))
  .on('finish', () => console.log('finish'))

stream.destroy(new Error())

// node v8.15.0
// - finish
// - error
//
// node v10.15.0
// - error
// - close

The problem I've noticed is during the usage of the destroy() method, on node 8 the stream will emit first the finish event, then a error event, and no close event.

On node 10, the finish event is not emitted.

I'm not sure if the problem is node 8 or 10, but by reading the docs I suppose the finish event have to be emitted after a possible error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.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