-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cluster,net: fix random port keying #7043
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -473,14 +473,21 @@ function masterInit() { | |
// Stop processing if worker already disconnecting | ||
if (worker.exitedAfterDisconnect) | ||
return; | ||
var args = [message.address, | ||
message.port, | ||
message.addressType, | ||
message.fd, | ||
message.index]; | ||
var key = args.join(':'); | ||
|
||
const port = +message.port; | ||
var key = [message.address, | ||
message.port, | ||
message.addressType, | ||
message.fd, | ||
message.index].join(':'); | ||
var handle = handles[key]; | ||
if (handle === undefined) { | ||
|
||
// Keys containing port zero are special cases in that they represent | ||
// temporary placeholders in the list of bound handles until the OS actually | ||
// assigns the real port number. In these cases we must always create a new | ||
// handle since there is no way the handle could be shared until the real | ||
// port number is received. | ||
if (handle === undefined || port === 0) { | ||
var constructor = RoundRobinHandle; | ||
// UDP is exempt from round-robin connection balancing for what should | ||
// be obvious reasons: it's connectionless. There is nothing to send to | ||
|
@@ -501,15 +508,45 @@ function masterInit() { | |
if (!handle.data) handle.data = message.data; | ||
|
||
// Set custom server data | ||
handle.add(worker, function(errno, reply, handle) { | ||
handle.add(worker, function(errno, reply, handle_) { | ||
var data; | ||
if (port === 0) { | ||
delete handles[key]; | ||
var port_; | ||
if (reply && reply.sockname && reply.sockname.port) { | ||
port_ = reply.sockname.port; | ||
} else if (handle_ && handle_.getsockname) { | ||
const out = {}; | ||
handle_.getsockname(out); | ||
port_ = out.port; | ||
} else { | ||
port_ = message.port; | ||
} | ||
key = [message.address, | ||
port_, | ||
message.addressType, | ||
message.fd, | ||
message.index].join(':'); | ||
if (!errno) | ||
handles[key] = handle; | ||
data = handle.data; | ||
handle.key = key; | ||
} else { | ||
data = handles[key].data; | ||
} | ||
|
||
reply = util._extend({ | ||
errno: errno, | ||
key: key, | ||
ack: message.seq, | ||
data: handles[key].data | ||
data: data | ||
}, reply); | ||
if (errno) delete handles[key]; // Gives other workers a chance to retry. | ||
send(worker, reply, handle); | ||
|
||
// Gives other workers a chance to retry. | ||
if (errno && port !== 0) | ||
delete handles[key]; | ||
|
||
send(worker, reply, handle_); | ||
}); | ||
} | ||
|
||
|
@@ -571,18 +608,23 @@ function workerInit() { | |
|
||
// obj is a net#Server or a dgram#Socket object. | ||
cluster._getServer = function(obj, options, cb) { | ||
const indexesKey = [ options.address, | ||
var index; | ||
if (+options.port !== 0) { | ||
var indexesKey = [ options.address, | ||
options.port, | ||
options.addressType, | ||
options.fd ].join(':'); | ||
if (indexes[indexesKey] === undefined) | ||
indexes[indexesKey] = 0; | ||
else | ||
indexes[indexesKey]++; | ||
if (indexes[indexesKey] === undefined) | ||
index = indexes[indexesKey] = 0; | ||
else | ||
index = ++indexes[indexesKey]; | ||
} else { | ||
index = 0; | ||
} | ||
|
||
const message = util._extend({ | ||
act: 'queryServer', | ||
index: indexes[indexesKey], | ||
index: index, | ||
data: null | ||
}, options); | ||
|
||
|
@@ -591,6 +633,16 @@ function workerInit() { | |
send(message, function(reply, handle) { | ||
if (obj._setServerData) obj._setServerData(reply.data); | ||
|
||
if (+options.port === 0 && reply.sockname) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: As the |
||
indexesKey = [ options.address, | ||
reply.sockname.port, | ||
options.addressType, | ||
options.fd ].join(':'); | ||
if (indexes[indexesKey] === undefined) | ||
index = indexes[indexesKey] = 0; | ||
else | ||
index = ++indexes[indexesKey]; | ||
} | ||
if (handle) | ||
shared(reply, handle, indexesKey, cb); // Shared listen socket. | ||
else | ||
|
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
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,54 @@ | ||
'use strict'; | ||
require('../common'); | ||
var assert = require('assert'); | ||
var cluster = require('cluster'); | ||
var net = require('net'); | ||
|
||
function noop() {} | ||
|
||
if (cluster.isMaster) { | ||
var worker1 = cluster.fork(); | ||
var worker2 = cluster.fork(); | ||
|
||
worker1.on('message', onMessage); | ||
worker2.on('message', onMessage); | ||
function onMessage(obj) { | ||
assert.strictEqual(typeof obj, 'object'); | ||
assert.strictEqual(obj.msg, 'success'); | ||
assert.strictEqual(typeof obj.port, 'number'); | ||
assert.ok(obj.port !== 0, 'Expected non-zero port number from worker'); | ||
this.listens = (this.listens || 0) + 1; | ||
if (worker1.listens === 2 && worker2.listens === 2) { | ||
worker1.kill(); | ||
worker2.kill(); | ||
} | ||
} | ||
} else { | ||
net.createServer(noop).on('error', function(err) { | ||
// no errors expected | ||
process.send('server1:' + err.code); | ||
}).listen({ | ||
host: 'localhost', | ||
port: 0, | ||
exclusive: false | ||
}, function() { | ||
process.send({ | ||
msg: 'success', | ||
port: this.address().port, | ||
}); | ||
}) | ||
|
||
net.createServer(noop).on('error', function(err) { | ||
// no errors expected | ||
process.send('server2:' + err.code); | ||
}).listen({ | ||
host: 'localhost', | ||
port: 0, | ||
exclusive: false | ||
}, function() { | ||
process.send({ | ||
msg: 'success', | ||
port: this.address().port, | ||
}); | ||
}); | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be within the
!errno
check?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't really matter. I just did it there so that it's overwritten no matter if there should happen to be another reference to the handle now or in the future.