nodejs / readable-stream

Node-core streams for userland
https://nodejs.org/api/stream.html
Other
1.03k stars 227 forks source link

Releasing write() callbacks on error #133

Closed mcollina closed 9 years ago

mcollina commented 9 years ago

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)
  }
}

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

I spoke to @mafintosh about this one at a couple of conference, so better log it here (and maybe resolve it).

mcollina commented 9 years ago

I am ok to submit a PR on this issue, if you think this should be addressed.

mcollina commented 9 years ago

Or should I resubmit this into the iojs repo?

mcollina commented 9 years ago

Closing and resubmitting to iojs, as it might be the best place to address this anyway.

kanongil commented 9 years ago

I wanted something like this in node, but for some reason the issue was closed by @chrisdickinson. See https://github.com/joyent/node/issues/5920.

mcollina commented 9 years ago

I reposted on iojs: https://github.com/nodejs/io.js/issues/1746