diff --git a/lib/dgram.js b/lib/dgram.js index a9e9e2a6a9a038..9b411246afc545 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -219,7 +219,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(); +}