Skip to content

Commit

Permalink
net: simplify Socket.prototype._final
Browse files Browse the repository at this point in the history
Remove conditions that should be irrelevant since we started
using `_final`, as well as an extra `defaultTriggerAsyncIdScope()`
call which is unnecessary because there is an equivalent
scope already present on the native side.

PR-URL: nodejs#24075
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
addaleax committed Nov 10, 2018
1 parent 9c6b7f7 commit b7e6ccd
Showing 1 changed file with 8 additions and 23 deletions.
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

0 comments on commit b7e6ccd

Please sign in to comment.