From c15bafdaf4ea4831e31696b9e829c31f3b3e02df Mon Sep 17 00:00:00 2001 From: Jason Zhang Date: Mon, 8 May 2023 17:17:48 +0930 Subject: [PATCH] net: only defer _final call when connecting Fixes: https://github.com/nodejs/node/issues/47322 PR-URL: https://github.com/nodejs/node/pull/47385 Reviewed-By: Matteo Collina Reviewed-By: Robert Nagy --- 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 f52222ec58..71195750a3 100644 --- a/lib/net.js +++ b/lib/net.js @@ -508,7 +508,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 98cf49768a..45d0b5477e 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); +}));