Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: non-readable Duplex async construct race condition #34448

Closed
jasnell opened this issue Jul 20, 2020 · 0 comments
Closed

stream: non-readable Duplex async construct race condition #34448

jasnell opened this issue Jul 20, 2020 · 0 comments
Labels
confirmed-bug Issues with confirmed bugs. stream Issues and PRs related to the stream subsystem.

Comments

@jasnell
Copy link
Member

jasnell commented Jul 20, 2020

@nodejs/streams @ronag

Try..

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

class M extends Duplex {
  constructor() {
    super({ readable: false });
  }

  _construct(callback) {
    setTimeout(() => {
      console.log(1);
      callback();
    }, 2000);
  }

  _write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }

  _read() {
    this.push(null);
  }
}

const m = new M();
m.resume();
m.end('foo');
m.on('close', () => console.log('destroyed'));

Outputs:

foo
destroyed
1

Note that the auto-destruction of the Duplex does not appropriately wait for the completion of the async _construct(). Changing the construction options to { readable: true } causes the code to work as expected:

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

class M extends Duplex {
  constructor() {
    super({ readable: true});
  }

  _construct(callback) {
    setTimeout(() => {
      console.log(1);
      callback();
    }, 2000);
  }

  _write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }

  _read() {
    this.push(null);
  }
}

const m = new M();
m.resume();
m.end('foo');
m.on('close', () => console.log('destroyed'));

Outputs:

foo
1
destroyed
@jasnell jasnell added confirmed-bug Issues with confirmed bugs. stream Issues and PRs related to the stream subsystem. labels Jul 20, 2020
ronag added a commit to nxtedition/node that referenced this issue Jul 21, 2020
Ensures that _construct has finished before invoking
_destroy.

The 'constructed' property was not properly set to false
for both writable and readable state.

Fixes: nodejs#34448
cjihrig pushed a commit that referenced this issue Jul 23, 2020
Ensures that _construct has finished before invoking
_destroy.

The 'constructed' property was not properly set to false
for both writable and readable state.

Fixes: #34448

PR-URL: #34456
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant