From 10a27277ad272c5a5cdfddd786251ffaa8e02295 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 48a8bc069901e2..643137b70d0641 100644 --- a/lib/net.js +++ b/lib/net.js @@ -335,14 +335,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) { @@ -352,23 +344,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')); @@ -387,7 +372,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(); }