Skip to content

Stream not emitting end after removeListener #9002

Closed
@davedoesdev

Description

@davedoesdev

This program prints "END":

const s = new require('stream').PassThrough();

function readable()
{
    console.log(this.read());
}

s.end();

setTimeout(function ()
{
    s.on('readable', readable);
    s.on('end', function ()
    {
        console.log("END");
    });
}, 1000);

whereas this program does not:

const s = new require('stream').PassThrough();

function readable()
{
    console.log(this.read());
}

s.on('readable', readable);
s.removeListener('readable', readable);

s.end();

setTimeout(function ()
{
    s.on('end', function ()
    {
        console.log("END");
    });
}, 1000);

The only difference is that a readable listener is added and then removed before the stream is ended.

My question is whether the behaviour should be the same, given that there is no readable listener in both cases when the stream is ended.

I can see in the code why: At https://github.com/nodejs/node/blob/v6.7.0/lib/_stream_readable.js#L696 readableListening is set to true but it's never reset to false if all readable listeners are removed.

Before making any changes however, I'd like to ask whether this is intended behaviour?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.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