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

tty: implement ReadStream._final to avoid ENOTCONN on .end() #22900

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const {
} = errors.codes;
const { validateInt32, validateString } = require('internal/validators');
const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
const kShouldShutdown = Symbol('shouldShutdown');

// Lazy loaded to improve startup performance.
let cluster;
Expand Down Expand Up @@ -245,6 +246,10 @@ function Socket(options) {
options.writable = options.writable || false;
const { allowHalfOpen } = options;

// Needed to avoid to call uv_shutdown during _final
Copy link
Contributor

Choose a reason for hiding this comment

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

"to call" -> "calling"

// as otherwise it would error with an ENOTCONN.
this[kShouldShutdown] = options.writable;

// Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
options.allowHalfOpen = true;
// For backwards compat do not emit close on destroy.
Expand Down Expand Up @@ -359,7 +364,7 @@ Socket.prototype._final = function(cb) {
debug('_final: not ended, call shutdown()');

// otherwise, just shutdown, or destroy() if not possible
if (!this._handle || !this._handle.shutdown) {
if (!this[kShouldShutdown] || !this._handle || !this._handle.shutdown) {
Copy link
Member

Choose a reason for hiding this comment

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

I think the issue might be that this runs destroy() when it previously didn’t.

I can try it too (later), but maybe undoing this line change and adding if (!this[kShouldShutdown]) return cb(); below works?

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't because the writable: false parameter that is passed in to the net.Socket constructor does control other behaviors internally, and it does not mean "this is half-open".
If you want to give it a shot, feel free to push here. Otherwise we revert to the _final  solution.

cb();
return this.destroy();
}
Expand Down
1 change: 1 addition & 0 deletions lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function ReadStream(fd, options) {
}
inherits(ReadStream, net.Socket);


Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated change?

ReadStream.prototype.setRawMode = function(flag) {
flag = !!flag;
this._handle.setRawMode(flag);
Expand Down
8 changes: 8 additions & 0 deletions test/pseudo-tty/test-tty-stdin-call-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

require('../common');

// This tests verifies that process.stdin.end() does not
// crash the process with ENOTCONN

process.stdin.end();
Empty file.