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..dd0ceec1bb1398 --- /dev/null +++ b/test/parallel/test-dgram-unref-in-cluster.js @@ -0,0 +1,18 @@ +'use strict'; +const common = require('../common'); +const dgram = require('dgram'); +const cluster = require('cluster'); + +// Test if the worker process exits. + +if (cluster.isPrimary) { + cluster.fork(); +} else { + const socket = dgram.createSocket('udp4'); + socket.unref(); + socket.bind(); + socket.on('listening', common.mustCall(() => { + // Do not keep loop alive + process.channel.unref(); + })); +}