Skip to content

Commit c668185

Browse files
aadityabhatiabnoordhuis
authored andcommitted
cluster: make 'listening' handler see actual port
Make the 'listening' event handler in the master process see the actual port that the worker bound to when the worker specified port 0, i.e. a random port.
1 parent 8bd4590 commit c668185

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/cluster.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ cluster._getServer = function(tcpSelf, address, port, addressType, fd, cb) {
535535
sendInternalMessage(cluster.worker, {
536536
cmd: 'listening',
537537
address: address,
538-
port: port,
538+
port: tcpSelf.address().port || port,
539539
addressType: addressType,
540540
fd: fd
541541
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var assert = require('assert');
23+
var cluster = require('cluster');
24+
var net = require('net');
25+
26+
if (cluster.isMaster) {
27+
var port = null;
28+
cluster.fork();
29+
cluster.on('listening', function(worker, address) {
30+
port = address.port;
31+
// ensure that the port is not 0 or null
32+
assert(port);
33+
// ensure that the port is numerical
34+
assert.strictEqual(typeof(port), 'number');
35+
worker.destroy();
36+
});
37+
process.on('exit', function() {
38+
// ensure that the 'listening' handler has been called
39+
assert(port);
40+
});
41+
}
42+
else {
43+
net.createServer(assert.fail).listen(0);
44+
}

0 commit comments

Comments
 (0)