Skip to content

Commit 2c12374

Browse files
committed
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.
1 parent b993789 commit 2c12374

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/internal/cluster/child.js

Lines changed: 4 additions & 0 deletions
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';
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
9+
if (cluster.isPrimary) {
10+
for (let i = 0; i < 3; i += 1) {
11+
cluster.fork();
12+
}
13+
} else {
14+
const buf = Buffer.alloc(1024, 42);
15+
const socket = dgram.createSocket('udp4');
16+
17+
socket.on('error', common.mustNotCall());
18+
19+
socket.on('listening', common.mustCall(() => {
20+
socket.close();
21+
}));
22+
23+
socket.on('close', common.mustCall(() => {
24+
cluster.worker.disconnect();
25+
}));
26+
27+
// Get a random port for send
28+
const portGetter = dgram.createSocket('udp4')
29+
.bind(0, 'localhost', common.mustCall(() => {
30+
// Adds a listener to 'listening' to send the data when
31+
// the socket is available
32+
socket.send(buf, 0, buf.length,
33+
portGetter.address().port,
34+
portGetter.address().address);
35+
36+
portGetter.close();
37+
}));
38+
}

0 commit comments

Comments
 (0)