Skip to content

Commit ce3a22a

Browse files
oyydtargos
authored andcommitted
cluster: fix closing dgram sockets in cluster workers throws errors
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent f2f7d7b commit ce3a22a

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/internal/cluster/child.js

+4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ cluster._getServer = function(obj, options, cb) {
111111
});
112112

113113
obj.once('listening', () => {
114+
// short-lived sockets might have been closed
115+
if (!indexes.has(indexesKey)) {
116+
return;
117+
}
114118
cluster.worker.state = 'listening';
115119
const address = obj.address();
116120
message.act = 'listening';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
// Ensure that closing dgram sockets in 'listening' callbacks of cluster workers
3+
// won't throw errors.
4+
5+
const common = require('../common');
6+
const dgram = require('dgram');
7+
const cluster = require('cluster');
8+
if (common.isWindows)
9+
common.skip('dgram clustering is currently not supported on windows.');
10+
11+
if (cluster.isPrimary) {
12+
for (let i = 0; i < 3; i += 1) {
13+
cluster.fork();
14+
}
15+
} else {
16+
const socket = dgram.createSocket('udp4');
17+
18+
socket.on('error', common.mustNotCall());
19+
20+
socket.on('listening', common.mustCall(() => {
21+
socket.close();
22+
}));
23+
24+
socket.on('close', common.mustCall(() => {
25+
cluster.worker.disconnect();
26+
}));
27+
28+
socket.bind(0);
29+
}

0 commit comments

Comments
 (0)