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

net: simplify Socket.prototype._final #24075

Closed
wants to merge 2 commits into from
Closed
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
31 changes: 8 additions & 23 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,6 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
};


function shutdownSocket(self, callback) {
var req = new ShutdownWrap();
req.oncomplete = afterShutdown;
req.handle = self._handle;
req.callback = callback;
return self._handle.shutdown(req);
}

// the user has called .end(), and all the bytes have been
// sent out to the other side.
Socket.prototype._final = function(cb) {
Expand All @@ -353,23 +345,16 @@ Socket.prototype._final = function(cb) {
return this.once('connect', () => this._final(cb));
}

if (!this.readable || this._readableState.ended) {
debug('_final: ended, destroy', this._readableState);
cb();
return this.destroy();
}
if (!this._handle)
return cb();

debug('_final: not ended, call shutdown()');

// otherwise, just shutdown, or destroy() if not possible
if (!this._handle || !this._handle.shutdown) {
cb();
return this.destroy();
}

var err = defaultTriggerAsyncIdScope(
this[async_id_symbol], shutdownSocket, this, cb
);
var req = new ShutdownWrap();
req.oncomplete = afterShutdown;
req.handle = this._handle;
req.callback = cb;
var err = this._handle.shutdown(req);

if (err)
return this.destroy(errnoException(err, 'shutdown'));
Expand All @@ -388,7 +373,7 @@ function afterShutdown(status, handle) {
if (self.destroyed)
return;

if (self._readableState.ended) {
if (!self.readable || self._readableState.ended) {
debug('readableState ended, destroying');
self.destroy();
}
Expand Down