Skip to content

The 'error' event can be emitted more than once when using writable.destroy() #26015

Closed
@lpinca

Description

@lpinca
  • 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.

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