Skip to content

Commit 65b1e4f

Browse files
sam-githubpiscisaureus
authored andcommitted
dgram: implicit binds should be exclusive
Server sockets should be shared by default, and client sockets should be exclusive by default. For net/TCP, this is how it is, for dgram/UDP, its a little less clear what a client socket is, but a socket that is auto-bound during a dgram.send() is not usefully shared among cluster workers, any more than an outgoing TCP connection would be usefully shared. Since implicit binds become exclusive, implicit/client dgram sockets can now be used with cluster on Windows. Before, neither explicit nor implicitly bound sockets could be used, causing dgram to be completely unsupported with cluster on Windows. After this change, they become half supported. PR: #325 PR: nodejs/node-v0.x-archive#8643 Reviewed-by: Ben Noordhuis <[email protected]> Reviewed-by: Bert Belder <[email protected]>
1 parent 083c421 commit 65b1e4f

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

lib/dgram.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ Socket.prototype.send = function(buffer,
278278
self._healthCheck();
279279

280280
if (self._bindState == BIND_STATE_UNBOUND)
281-
self.bind(0, null);
281+
self.bind({port: 0, exclusive: true}, null);
282282

283283
// If the socket hasn't been bound yet, push the outbound packet onto the
284284
// send queue and send after binding is complete.

test/parallel/test-cluster-dgram-2.js

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ function worker() {
5353
var socket = dgram.createSocket('udp4');
5454
var buf = new Buffer('hello world');
5555

56+
// This test is intended to exercise the cluster binding of udp sockets, but
57+
// since sockets aren't clustered when implicitly bound by at first call of
58+
// send(), explicitly bind them to an ephemeral port.
59+
socket.bind(0);
60+
5661
for (var i = 0; i < PACKETS_PER_WORKER; i++)
5762
socket.send(buf, 0, buf.length, common.PORT, '127.0.0.1');
5863

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 common = require('../common');
23+
var assert = require('assert');
24+
var cluster = require('cluster');
25+
var dgram = require('dgram');
26+
27+
// Without an explicit bind, send() causes an implicit bind, which always
28+
// generate a unique per-socket ephemeral port. An explicit bind to a port
29+
// number causes all sockets bound to that number to share a port.
30+
//
31+
// The 2 workers that call bind() will share a port, the two workers that do
32+
// not will not share a port, so master will see 3 unique source ports.
33+
34+
// Note that on Windows, clustered dgram is not supported. Since explicit
35+
// binding causes the dgram to be clustered, don't fork the workers that bind.
36+
// This is a useful test, still, because it demonstrates that by avoiding
37+
// clustering, client (ephemeral, implicitly bound) dgram sockets become
38+
// supported while using cluster, though servers still cause the master to error
39+
// with ENOTSUP.
40+
41+
var windows = process.platform === 'win32';
42+
43+
if (cluster.isMaster) {
44+
var pass;
45+
var messages = 0;
46+
var ports = {};
47+
48+
process.on('exit', function() {
49+
assert.equal(pass, true);
50+
});
51+
52+
var target = dgram.createSocket('udp4');
53+
54+
target.on('message', function(buf, rinfo) {
55+
messages++;
56+
ports[rinfo.port] = true;
57+
58+
if (windows && messages === 2) {
59+
assert.equal(Object.keys(ports).length, 2);
60+
done();
61+
}
62+
63+
if (!windows && messages === 4) {
64+
assert.equal(Object.keys(ports).length, 3);
65+
done();
66+
}
67+
68+
function done() {
69+
pass = true;
70+
cluster.disconnect();
71+
target.close();
72+
}
73+
});
74+
75+
target.on('listening', function() {
76+
cluster.fork();
77+
cluster.fork();
78+
if (!windows) {
79+
cluster.fork({BOUND: 'y'});
80+
cluster.fork({BOUND: 'y'});
81+
}
82+
});
83+
84+
target.bind({port: common.PORT, exclusive: true});
85+
86+
return;
87+
}
88+
89+
var source = dgram.createSocket('udp4');
90+
91+
if (process.env.BOUND === 'y') {
92+
source.bind(0);
93+
} else {
94+
// cluster doesn't know about exclusive sockets, so it won't close them. This
95+
// is expected, its the same situation for timers, outgoing tcp connections,
96+
// etc, which also keep workers alive after disconnect was requested.
97+
source.unref();
98+
}
99+
100+
source.send(Buffer('abc'), 0, 3, common.PORT, '127.0.0.1');

0 commit comments

Comments
 (0)