Skip to content

Unexpected event ordering when destroying a readable stream #18171

@mafintosh

Description

@mafintosh

Running the below example

var stream = require('stream')
var rs = new stream.Readable({read: () => {}})

rs.on('end', function () {
  console.log('onend')
})

rs.on('error', function () {
  console.log('onerror')
})

rs.resume()
rs.read()
rs.destroy(new Error('error'))

... I get the following output

onend
onerror

This was confusing to me as I would expect a stream.destroy(err) to result in an immediate error event (especially before any end was emitted under the hood).

This order of events makes error handling hard as everything looks like it ended as it should when the end is emitted before the error.

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