From 2fa7d33e74d069f7a3c42b355d34452e902b2132 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 29 Mar 2021 13:03:37 +0200 Subject: [PATCH] Revert "net: add support for finished after .destroy()" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 2da36112d177efc19201f1610dcf51647378131c. PR-URL: https://github.com/nodejs/node/pull/37964 Refs: https://github.com/nodejs/node/issues/37937 Reviewed-By: Robert Nagy Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Michaƫl Zasso Reviewed-By: Colin Ihrig --- lib/_http_incoming.js | 8 ++++++-- lib/internal/streams/end-of-stream.js | 10 +--------- lib/net.js | 12 ------------ test/parallel/test-net-finished.js | 25 ------------------------- 4 files changed, 7 insertions(+), 48 deletions(-) delete mode 100644 test/parallel/test-net-finished.js diff --git a/lib/_http_incoming.js b/lib/_http_incoming.js index 30432a5230fa30..d09683c9a8f1b1 100644 --- a/lib/_http_incoming.js +++ b/lib/_http_incoming.js @@ -181,8 +181,12 @@ IncomingMessage.prototype._destroy = function _destroy(err, cb) { this.emit('aborted'); } - // If aborted destroy socket. - if (this.socket && this.aborted) { + // If aborted and the underlying socket is not already destroyed, + // destroy it. + // We have to check if the socket is already destroyed because finished + // does not call the callback when this methdod is invoked from `_http_client` + // in `test/parallel/test-http-client-spurious-aborted.js` + if (this.socket && !this.socket.destroyed && this.aborted) { this.socket.destroy(err); const cleanup = finished(this.socket, (e) => { cleanup(); diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index 4425fa1931b30e..da48831a405dbe 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -22,14 +22,6 @@ const { validateObject, } = require('internal/validators'); -function isSocket(stream) { - return ( - typeof stream.connect === 'function' && - typeof stream.setNoDelay === 'function' && - typeof stream.setKeepAlive === 'function' - ); -} - function isRequest(stream) { return stream.setHeader && typeof stream.abort === 'function'; } @@ -105,7 +97,7 @@ function eos(stream, options, callback) { let willEmitClose = isServerResponse(stream) || ( state && state.autoDestroy && - (state.emitClose || isSocket(stream)) && + state.emitClose && state.closed === false && isReadable(stream) === readable && isWritable(stream) === writable diff --git a/lib/net.js b/lib/net.js index 94aae1fa9f736d..fdd565d3109754 100644 --- a/lib/net.js +++ b/lib/net.js @@ -662,12 +662,6 @@ Socket.prototype._destroy = function(exception, cb) { this._handle.close(() => { debug('emit close'); - if (this._writableState) { - this._writableState.closed = true; - } - if (this._readableState) { - this._readableState.closed = true; - } this.emit('close', isException); }); this._handle.onread = noop; @@ -1655,12 +1649,6 @@ Server.prototype._emitCloseIfDrained = function() { function emitCloseNT(self) { debug('SERVER: emit close'); - if (self._writableState) { - self._writableState.closed = true; - } - if (self._readableState) { - self._readableState.closed = true; - } self.emit('close'); } diff --git a/test/parallel/test-net-finished.js b/test/parallel/test-net-finished.js deleted file mode 100644 index 74acff0faa2384..00000000000000 --- a/test/parallel/test-net-finished.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict'; -const common = require('../common'); -const net = require('net'); -const { finished } = require('stream'); - -const server = net.createServer(function(con) { - con.on('close', common.mustCall()); -}); - -server.listen(0, common.mustCall(function() { - const con = net.connect({ - port: this.address().port - }) - .on('connect', () => { - finished(con, common.mustCall()); - con.destroy(); - finished(con, common.mustCall()); - con.on('close', common.mustCall(() => { - finished(con, common.mustCall(() => { - server.close(); - })); - })); - }) - .end(); -}));