Skip to content

Commit

Permalink
cluster: fix closing dgram sockets in cluster workers throws errors
Browse files Browse the repository at this point in the history
This fixes closing dgram sockets right after binding in cluster
workers will throws `ERR_SOCKET_DGRAM_NOT_RUNNING` errors.

PR-URL: #43709
Fixes: #40671
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
oyyd authored and targos committed Jul 20, 2022
1 parent 97c1096 commit 9552058
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/internal/cluster/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ cluster._getServer = function(obj, options, cb) {
});

obj.once('listening', () => {
// short-lived sockets might have been closed
if (!indexes.has(indexesKey)) {
return;
}
cluster.worker.state = 'listening';
const address = obj.address();
message.act = 'listening';
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-dgram-cluster-close-in-listening.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
// Ensure that closing dgram sockets in 'listening' callbacks of cluster workers
// won't throw errors.

const common = require('../common');
const dgram = require('dgram');
const cluster = require('cluster');
if (common.isWindows)
common.skip('dgram clustering is currently not supported on windows.');

if (cluster.isPrimary) {
for (let i = 0; i < 3; i += 1) {
cluster.fork();
}
} else {
const socket = dgram.createSocket('udp4');

socket.on('error', common.mustNotCall());

socket.on('listening', common.mustCall(() => {
socket.close();
}));

socket.on('close', common.mustCall(() => {
cluster.worker.disconnect();
}));

socket.bind(0);
}

0 comments on commit 9552058

Please sign in to comment.