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(); + })); +}