From 23f086c363ebbfe791b8063086d8feafb54b1509 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Fri, 24 May 2024 04:32:25 +0800 Subject: [PATCH] lib: do not call callback if socket is closed PR-URL: https://github.com/nodejs/node/pull/52829 Reviewed-By: Matteo Collina --- lib/dgram.js | 5 ++++- ...m-bind-socket-close-before-cluster-reply.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-dgram-bind-socket-close-before-cluster-reply.js diff --git a/lib/dgram.js b/lib/dgram.js index e34e2e81f8d43d..d151cd6e5ed1df 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -221,7 +221,10 @@ function bindServerHandle(self, options, errCb) { const state = self[kStateSymbol]; cluster._getServer(self, options, (err, handle) => { if (err) { - errCb(err); + // Do not call callback if socket is closed + if (state.handle) { + errCb(err); + } return; } diff --git a/test/parallel/test-dgram-bind-socket-close-before-cluster-reply.js b/test/parallel/test-dgram-bind-socket-close-before-cluster-reply.js new file mode 100644 index 00000000000000..4a8d2517523abc --- /dev/null +++ b/test/parallel/test-dgram-bind-socket-close-before-cluster-reply.js @@ -0,0 +1,18 @@ +'use strict'; +const common = require('../common'); +const dgram = require('dgram'); +const cluster = require('cluster'); + +if (cluster.isPrimary) { + cluster.fork(); +} else { + // When the socket attempts to bind, it requests a handle from the cluster. + // Force the cluster to send back an error code. + const socket = dgram.createSocket('udp4'); + cluster._getServer = function(self, options, callback) { + socket.close(() => { cluster.worker.disconnect(); }); + callback(-1); + }; + socket.on('error', common.mustNotCall()); + socket.bind(); +}