From 12a2f412fcaec4b1edf3fa4fca6a77e80d808ff5 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 28 Feb 2025 19:24:59 -0800 Subject: [PATCH] node: fix test-net-listen-close-server.js (#17809) --- src/js/node/net.ts | 16 +++++----- ...n-close-server-callback-is-not-function.js | 11 +++++++ .../parallel/test-net-listen-close-server.js | 30 +++++++++++++++++++ 3 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 test/js/node/test/parallel/test-net-listen-close-server-callback-is-not-function.js create mode 100644 test/js/node/test/parallel/test-net-listen-close-server.js diff --git a/src/js/node/net.ts b/src/js/node/net.ts index eb4508c45d..5782ad126a 100644 --- a/src/js/node/net.ts +++ b/src/js/node/net.ts @@ -1407,6 +1407,10 @@ Server.prototype.listen = function listen(port, hostname, onListen) { throw $ERR_SERVER_ALREADY_LISTEN(); } + if (onListen != null) { + this.once("listening", onListen); + } + try { var tls = undefined; var TLSSocketClass = undefined; @@ -1501,7 +1505,7 @@ Server.prototype[kRealListen] = function realListen( // That leads to all sorts of confusion. // // process.nextTick() is not sufficient because it will run before the IO queue. - setTimeout(emitListeningNextTick, 1, this, onListen?.bind(this)); + setTimeout(emitListeningNextTick, 1, this); }; Server.prototype.getsockname = function getsockname(out) { @@ -1528,14 +1532,8 @@ class ConnResetException extends Error { } } -function emitListeningNextTick(self, onListen) { - if (typeof onListen === "function") { - try { - onListen.$call(self); - } catch (err) { - self.emit("error", err); - } - } +function emitListeningNextTick(self) { + if (!self._handle) return; self.emit("listening"); } diff --git a/test/js/node/test/parallel/test-net-listen-close-server-callback-is-not-function.js b/test/js/node/test/parallel/test-net-listen-close-server-callback-is-not-function.js new file mode 100644 index 0000000000..459ca3e538 --- /dev/null +++ b/test/js/node/test/parallel/test-net-listen-close-server-callback-is-not-function.js @@ -0,0 +1,11 @@ +'use strict'; +const common = require('../common'); +const net = require('net'); + +const server = net.createServer(common.mustNotCall()); + +server.on('close', common.mustCall()); + +server.listen(0, common.mustNotCall()); + +server.close('bad argument'); diff --git a/test/js/node/test/parallel/test-net-listen-close-server.js b/test/js/node/test/parallel/test-net-listen-close-server.js new file mode 100644 index 0000000000..99d7111eba --- /dev/null +++ b/test/js/node/test/parallel/test-net-listen-close-server.js @@ -0,0 +1,30 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; +const common = require('../common'); +const net = require('net'); + +const server = net.createServer(function(socket) { +}); +server.listen(0, common.mustNotCall()); +server.on('error', common.mustNotCall()); +server.close();