-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'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 (cluster.isPrimary) { | ||
for (let i = 0; i < 3; i += 1) { | ||
cluster.fork(); | ||
} | ||
} else { | ||
const buf = Buffer.alloc(1024, 42); | ||
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(); | ||
})); | ||
|
||
// Get a random port for send | ||
const portGetter = dgram.createSocket('udp4') | ||
.bind(0, 'localhost', common.mustCall(() => { | ||
// Adds a listener to 'listening' to send the data when | ||
// the socket is available | ||
socket.send(buf, 0, buf.length, | ||
portGetter.address().port, | ||
portGetter.address().address); | ||
|
||
portGetter.close(); | ||
})); | ||
} |