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: add auto-destroy mode #22795

Closed
wants to merge 7 commits into from

Conversation

mafintosh
Copy link
Member

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

This adds a new options to the stream constructor called autoDestroy that is default false.
When autoDestroy is enabled .destroy() will automatically be called when the stream has ended and/or finished depending on whether it's a readable/writable/duplex.

This simplifies error handling / resource management for users as you can then always do your teardown logic in the destroy method.

@nodejs-github-bot nodejs-github-bot added dont-land-on-v6.x http2 Issues or PRs related to the http2 subsystem. stream Issues and PRs related to the stream subsystem. labels Sep 10, 2018
@mcollina mcollina added semver-minor PRs that contain new features and should be released in the next minor version. dont-land-on-v8.x labels Sep 10, 2018
@mcollina
Copy link
Member

@mafintosh I think it needs to call .destroy(err) whenever there is a emit('error'), at least in the streams classes and the option is enabled.

I think this is the first step to implement #20096, right?

@@ -1492,6 +1492,8 @@ changes:
[`stream._destroy()`][writable-_destroy] method.
* `final` {Function} Implementation for the
[`stream._final()`][stream-_final] method.
* `autoDestroy` {boolean} Whether this stream should automatically call
.destroy() on itself after ending.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.destroy() -> `.destroy()`?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide the default value explicitly here?

@@ -1741,6 +1743,8 @@ constructor and implement the `readable._read()` method.
method.
* `destroy` {Function} Implementation for the
[`stream._destroy()`][readable-_destroy] method.
* `autoDestroy` {boolean} Whether this stream should automatically call
.destroy() on itself after ending.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@@ -1075,6 +1078,10 @@ function endReadable(stream) {
}
}

function writableAutoDestroy(wState) {
return !wState || (wState.autoDestroy && wState.finished);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this just be inlined?

@targos
Copy link
Member

targos commented Sep 10, 2018

It looks like the code in _stream_readable.js should be in _stream_writable.js and vice versa.

@@ -1492,6 +1492,8 @@ changes:
[`stream._destroy()`][writable-_destroy] method.
* `final` {Function} Implementation for the
[`stream._final()`][stream-_final] method.
* `autoDestroy` {boolean} Whether this stream should automatically call
.destroy() on itself after ending.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide the default value explicitly here?

t.resume();
t.on('end', common.mustCall());
t.on('finish', common.mustCall());
t.on('close', common.mustCall());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we maybe check the order of the events/destroy callback as well?

@addaleax
Copy link
Member

Gentle ping @mafintosh :)

@targos I think it’s this way around in order to support shutting down Duplex streams?

@mafintosh
Copy link
Member Author

@addaleax i'll fix up the nits+comments

@Fishrock123
Copy link
Contributor

Seems like a good addition to me, minus others' comments.

@tniessen
Copy link
Member

semver-minor commits should contain metadata in the YAML changes section of the affected API.

@@ -251,6 +251,7 @@ function onStreamTimeout(kind) {

class Http2ServerRequest extends Readable {
constructor(stream, headers, options, rawHeaders) {
if (!options) options = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure I’m not missing anything – how does this change relate to the others?

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mafintosh
Copy link
Member Author

Fixed all the nits @addaleax @targos @vsemozhetbyt @Fishrock123

@mafintosh
Copy link
Member Author

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I would like to see a CITGM run.

@@ -1493,6 +1493,11 @@ changes:
pr-url: https://github.com/nodejs/node/pull/18438
description: >
Add `emitClose` option to specify if `'close'` is emitted on destroy
- version: TBD
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the value we put here TBD? I never remember.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REPLACEME :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right! Thanks, will fix

@mafintosh
Copy link
Member Author

@mafintosh
Copy link
Member Author

@mafintosh
Copy link
Member Author

The CITGM runs seem to report similar errors on master + this PR, landing ...

Trott pushed a commit to Trott/io.js that referenced this pull request Oct 30, 2018
PR-URL: nodejs#22795
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
@mafintosh
Copy link
Member Author

Landed in f24b070

@mafintosh mafintosh closed this Oct 30, 2018
@mafintosh mafintosh deleted the auto-destroy-stream branch October 30, 2018 14:19
targos pushed a commit that referenced this pull request Nov 2, 2018
PR-URL: #22795
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
BethGriggs pushed a commit that referenced this pull request Apr 8, 2019
PR-URL: #22795
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
@BethGriggs BethGriggs mentioned this pull request May 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http2 Issues or PRs related to the http2 subsystem. semver-minor PRs that contain new features and should be released in the next minor version. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants