Description
As requested by @mcollina in #31179 (comment) I'm opening this issue to discuss the possibility of reverting a recent behavior change of writable streams.
writable.write() takes an optional callback which, quoting the documentation, is a
Callback for when this chunk of data is flushed
The documentation further clarifies the callback behavior as follows:
The
writable.write()
method writes some data to the stream, and calls the
suppliedcallback
once the data has been fully handled. If an error occurs,
the callback may or may not be called with the error as its first argument.
To reliably detect write errors, add a listener for the'error'
event. If
callback is called with an error, it will be called before the'error'
event
is emitted.
This is quite accurate and inline with the actual implementation. There can only be a single write at time so if writable.write()
is called while a write is in progress that write is buffered. If the stream errors or is destroyed the callbacks of buffered writes are not called.
In #29028 and then in #30596 this behavior was changed to call the callbacks of buffered writes with an error.
In my opinion this change was not need or better not justified by a specific issue. From what I've gathered (correct me if I am wrong) the main reason behind the change was that code like this
await for (const chunk of src) {
new Promise(function(resolve, reject) {
dst.write(chunk, err => err ? reject(err) : resolve());
});
}
could result in a promise that is never settled, but the destination stream in the example does not buffer writes so the callback is always called.
I think this change creates more problems than it solves:
- Not all callbacks must be called. A lot of callbacks are only called in response of a specific event / operation. If that event or operation never occurred its associated callback should not be called. This depends on case-by-case basis. In this specific case if the callback is called it makes me think that buffered writes are attempted even after a stream errors or is destroyed and this is not true even after stream: invoke buffered write callbacks on error #30596.
- AFAIK no issues have been created? about buffered write callbacks not being called on error in all these years so I think the original behavior is not broken.
- There are some side effects like net:
write(cb)
not called ifdestroy()
:ed before'connect'
#30841 or [v13.x backport] stream: invoke buffered write callbacks on error #31179 (comment). - It is hard to adapt user land code to this new behavior. For example
net.Socket
buffered write callbacks are now called with an error if a write error occurs. Doing the same on Node.js < 13 will be hard and hacky for userland libs. They should wait until Node.js 12 goes EOL to have the same behavior across all supported Node.js versions.