Node: Async iterator does not work with Duplex streams

Created on 28 Apr 2020  路  3Comments  路  Source: nodejs/node

  • Version: v14.0.0
  • Platform: Linux solus 5.5.11-151.current #1 SMP PREEMPT Tue Mar 24 18:06:46 UTC 2020 x86_64 GNU/Linux
  • Subsystem: stream

What steps will reproduce the bug?

const {Duplex, PassThrough} = require('stream');

const response = new PassThrough();
setTimeout(() => response.write('chunk 1'), 500);
setTimeout(() => response.write('chunk 2'), 1000);
setTimeout(() => response.write('chunk 3'), 1500);
setTimeout(() => response.end(), 2000);

class HelloWorld extends Duplex {
    constructor(response) {
        super();

        this.response = response;
        this.readMore = false;

        response.once('end', () => {
            this.push(null);
        });

        response.on('readable', () => {
            if (this.readMore) {
                this._read();
            }
        });
    }

    _read() {
        const {response} = this;

        this.readMore = true;

        if (response.readableLength) {
            this.readMore = false;
        }

        let data;
        while ((data = response.read()) !== null) {
            this.push(data);
        }
    }
}

const instance = new HelloWorld(response);
instance.end();

(async () => {
    await new Promise(resolve => setTimeout(resolve, 100));

    for await (const data of instance) {
        console.log(data.toString());
    }

    console.log('finished await');
})();

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior?

chunk 1
chunk 2
chunk 3
finished await

What do you see instead?

finished await

Additional information

Async iterator utilizes the end-of-stream module. The culprit is this:

https://github.com/nodejs/node/blob/f64c640e66ef9393139062991aa87b51f06f6294/lib/internal/streams/end-of-stream.js#L152

wState.finished is true, therefore the callback is called even though it should not.

Original Got issue: https://github.com/sindresorhus/got/issues/1172

Most helpful comment

I'm on it.

All 3 comments

Please note that it works as expected on Node.js v13.13.0.

/cc @ronag @mcollina

I'm on it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

filipesilvaa picture filipesilvaa  路  3Comments

akdor1154 picture akdor1154  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments

ksushilmaurya picture ksushilmaurya  路  3Comments

dfahlander picture dfahlander  路  3Comments