From 636e4e02a5de3359613d83d4a98b9ecdda3ad450 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 4 Nov 2018 12:28:27 +0100 Subject: [PATCH] net: simplify Socket.prototype._final 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: https://github.com/nodejs/node/pull/24075 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/net.js | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/lib/net.js b/lib/net.js index a0597bb0a08d64..ae389940b39596 100644 --- a/lib/net.js +++ b/lib/net.js @@ -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) { @@ -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')); @@ -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(); }