Skip to content

[streams] Releasing write() callbacks on error #1746

@mcollina

Description

@mcollina

In case the streams errors, e.g. for a broken TCP socket, or an application error, all buffered write callbacks are never called, here is an example:

var Writable = require('stream').Writable
var inherits = require('util').inherits

function MyWritable (opts) {
  Writable.call(this, opts)
}

inherits(MyWritable, Writable)

MyWritable.prototype._write = function (chunk, enc, done) {
  console.log('_write', chunk.toString())
  setTimeout(function () {
    done(new Error('faulty one'))
  }, 500)
}

var stream = new MyWritable()

stream.write(new Buffer('Hello First'), 'utf8', print('hello'))
stream.write(new Buffer('Hello Second'), 'utf8', print('second'))

stream.on('error', function (err) {
  console.log('error received', err)
})

function print (value, err) {
  return function () {
    console.log('done', value, err)
  }
}

Here is the output of the above program:

_write Hello First
done hello [Error: faulty one]
error received [Error: faulty one]

The code I am using to fix this behavior is https://github.com/mcollina/aedes/blob/master/lib/client.js#L87-L91.

I am ok to submit a PR about this issue, if you think it is worth fixing.

cc @mafintosh

Metadata

Metadata

Assignees

No one assigned

    Labels

    streamIssues and PRs related to the stream subsystem.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions