Skip to content

In writable stream, should we always set state.errorEmitted to true when executing stream.emit('error')? #18181

Description

@MoonBall

I have read the source code of lib/_stream_writable.js. I have a doubt about the meaning of state.errorEmitted.

Why do we set state.errorEmitted to true in some situations of stream.emit('error'), but doesn't set it in other situations. Moreover, it doesn't achieve emitting 'error' event only one time. For example, as shown below, the code will emit 'error' event two times :

const { Writable } = require('stream');

w = new Writable();
w.on('error', error => {
  console.log('on error.', error);
});
w.write(null);
w.write(null);

Activity

  1. MoonBall commented on Jan 17, 2018

    @MoonBall
    MemberAuthor
  2. mcollina commented on Jan 17, 2018

    @mcollina
    SponsorMember

    @MoonBall as a Node.js user, it means nothing. Internally it is used in some checks (in destroy()) to avoid doing certain things if the condition is met. This state variable is also used throughout core (which we should refactor according to #445).

    Making the change you propose is hard, but if you want to take a stab at it I will be happy review it.

  3. MoonBall commented on Jan 17, 2018

    @MoonBall
    MemberAuthor

    if it only work in destroy(), we can modify it's name or it's comments, so that we can better understand it. The comment(https://github.com/nodejs/node/blob/master/lib/_stream_writable.js#L144) is not appropriate.

  4. mcollina commented on Jan 17, 2018

    @mcollina
    SponsorMember

    It is not just destroy: https://github.com/nodejs/node/search?utf8=%E2%9C%93&q=errorEmitted&type=.

    Ideally all of that should not be needed, would you like to investigate?

  5. MoonBall commented on Jan 18, 2018

    @MoonBall
    MemberAuthor

    ok. I try to do it.

  6. Trott commented on Nov 25, 2018

    @Trott
    Member

    /ping @MoonBall @mcollina Should this remain open?

  7. mcollina commented on Nov 26, 2018

    @mcollina
    SponsorMember

    I think this should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions