Closed
Description
- Version: v11.9.0
- Platform: macOS
- Subsystem: stream
The 'error'
event can be emitted multiple times when using writable.destroy()
if the _destroy()
callback is called asynchronously. Here is a test case:
const { Writable } = require('stream');
const writable = new Writable({
destroy(err, callback) {
process.nextTick(callback, new Error('oops'));
}
});
writable.on('error', console.error);
writable.destroy();
// Assume an internal resource is closed in the `_destroy()` implementation.
// The resource fails to be closed cleanly causing `writable.destroy()` to be
// called again with an error.
writable.destroy(new Error('error'));
Actual result:
The 'error'
event is emitted twice.
Expected result:
The 'error'
event is emitted only once.
This is because the _writableState.errorEmitted
guard is set to true
when the callback is called.