From 8de642517e41825e8930d00dc53f53eeee76521b Mon Sep 17 00:00:00 2001 From: theanarkh Date: Tue, 3 Jan 2023 14:28:08 +0800 Subject: [PATCH] dgram: sync the old handle state to new handle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/46041 Reviewed-By: Matteo Collina Reviewed-By: Anna Henningsen Reviewed-By: Paolo Insogna Reviewed-By: Colin Ihrig Reviewed-By: Darshan Sen Reviewed-By: Juan José Arboleda --- lib/dgram.js | 5 ++++- test/parallel/test-dgram-unref-in-cluster.js | 23 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-dgram-unref-in-cluster.js diff --git a/lib/dgram.js b/lib/dgram.js index f3b8d485eb92d6..a9fac97f64bb45 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -184,7 +184,10 @@ function startListening(socket) { function replaceHandle(self, newHandle) { const state = self[kStateSymbol]; const oldHandle = state.handle; - + // Sync the old handle state to new handle + if (!oldHandle.hasRef() && typeof newHandle.unref === 'function') { + newHandle.unref(); + } // Set up the handle that we got from primary. newHandle.lookup = oldHandle.lookup; newHandle.bind = oldHandle.bind; diff --git a/test/parallel/test-dgram-unref-in-cluster.js b/test/parallel/test-dgram-unref-in-cluster.js new file mode 100644 index 00000000000000..40833a129d87db --- /dev/null +++ b/test/parallel/test-dgram-unref-in-cluster.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../common'); +const dgram = require('dgram'); +const cluster = require('cluster'); +const assert = require('assert'); + +if (common.isWindows) + common.skip('dgram clustering is currently not supported on Windows.'); + +if (cluster.isPrimary) { + cluster.fork(); +} else { + const socket = dgram.createSocket('udp4'); + socket.unref(); + socket.bind(); + socket.on('listening', common.mustCall(() => { + const sockets = process.getActiveResourcesInfo().filter((item) => { + return item === 'UDPWrap'; + }); + assert.ok(sockets.length === 0); + process.disconnect(); + })); +}