Skip to content

Commit 1e02f40

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 1e02f40

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 socket = dgram.createSocket('udp4');
15+
16+
socket.on('error', common.mustNotCall());
17+
18+
socket.on('listening', common.mustCall(() => {
19+
socket.close();
20+
}));
21+
22+
socket.on('close', common.mustCall(() => {
23+
cluster.worker.disconnect();
24+
}));
25+
26+
socket.bind(0);
27+
}

0 commit comments

Comments
 (0)