From e71dcc09efb7dff6af965f3f01f507a4d24c8833 Mon Sep 17 00:00:00 2001 From: jazelly Date: Mon, 3 Apr 2023 22:29:27 +0930 Subject: [PATCH] net: only defer _final call when connecting Fixes: https://github.com/nodejs/node/issues/47322 --- lib/net.js | 2 +- test/parallel/test-net-end-without-connect.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/net.js b/lib/net.js index e29b62c8127648..946b1550fa5267 100644 --- a/lib/net.js +++ b/lib/net.js @@ -507,7 +507,7 @@ Socket.prototype._unrefTimer = function _unrefTimer() { // sent out to the other side. Socket.prototype._final = function(cb) { // If still connecting - defer handling `_final` until 'connect' will happen - if (this.pending) { + if (this.connecting) { debug('_final: not yet connected'); return this.once('connect', () => this._final(cb)); } diff --git a/test/parallel/test-net-end-without-connect.js b/test/parallel/test-net-end-without-connect.js index 98cf49768a2a26..45d0b5477ed227 100644 --- a/test/parallel/test-net-end-without-connect.js +++ b/test/parallel/test-net-end-without-connect.js @@ -20,8 +20,11 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const net = require('net'); +const assert = require('assert'); const sock = new net.Socket(); -sock.end(); // Should not throw. +sock.end(common.mustCall(() => { + assert.strictEqual(sock.writable, false); +}));