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

tls,net: fix shutdown issue more cleanly #24290

Closed
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
6 changes: 4 additions & 2 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,12 @@ function TLSSocket(socket, opts) {

// Wrap plain JS Stream into StreamWrap
var wrap;
if ((socket instanceof net.Socket && socket._handle) || !socket)
if ((socket instanceof net.Socket && socket._handle) || !socket) {
wrap = socket;
else
} else {
wrap = new StreamWrap(socket);
wrap.once('close', () => this.destroy());
oyyd marked this conversation as resolved.
Show resolved Hide resolved
}

// Just a documented property to make secure sockets
// distinguishable from regular ones.
Expand Down
13 changes: 11 additions & 2 deletions lib/internal/wrap_js_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { ERR_STREAM_WRAP } = require('internal/errors').codes;

const kCurrentWriteRequest = Symbol('kCurrentWriteRequest');
const kCurrentShutdownRequest = Symbol('kCurrentShutdownRequest');
const kPendingShutdownRequest = Symbol('kPendingShutdownRequest');

function isClosing() { return this[owner_symbol].isClosing(); }
function onreadstart() { return this[owner_symbol].readStart(); }
Expand Down Expand Up @@ -79,6 +80,7 @@ class JSStreamWrap extends Socket {
this.stream = stream;
this[kCurrentWriteRequest] = null;
this[kCurrentShutdownRequest] = null;
this[kPendingShutdownRequest] = null;
this.readable = stream.readable;
this.writable = stream.writable;

Expand Down Expand Up @@ -115,8 +117,10 @@ class JSStreamWrap extends Socket {
// Working around that on the native side is not quite trivial (yet?),
// so for now that is supported here.

if (this[kCurrentWriteRequest] !== null)
return this.once('drain', () => this.doShutdown(req));
if (this[kCurrentWriteRequest] !== null) {
this[kPendingShutdownRequest] = req;
return 0;
}
assert.strictEqual(this[kCurrentWriteRequest], null);
assert.strictEqual(this[kCurrentShutdownRequest], null);
this[kCurrentShutdownRequest] = req;
Expand Down Expand Up @@ -189,6 +193,11 @@ class JSStreamWrap extends Socket {
this[kCurrentWriteRequest] = null;

handle.finishWrite(req, errCode);
if (this[kPendingShutdownRequest]) {
const req = this[kPendingShutdownRequest];
this[kPendingShutdownRequest] = null;
this.doShutdown(req);
}
}

doClose(cb) {
Expand Down
6 changes: 0 additions & 6 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,6 @@ Socket.prototype._final = function(cb) {
return this.once('connect', () => this._final(cb));
}

// TODO(addaleax): This should not be necessary.
if (!this.readable || this._readableState.ended) {
cb();
return this.destroy();
}

if (!this._handle)
return cb();

Expand Down