Skip to content

Commit 092c224

Browse files
sam-githubpiscisaureus
authored andcommitted
cluster: cluster.disconnect() should check status
Workers that are already disconnected but not yet exited should not be disconnected, trying to do so raises exceptions. PR-URL: nodejs/node-v0.x-archive#8642 Reviewed-by: Bert Belder <bertbelder@gmail.com>
1 parent 14310e6 commit 092c224

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

lib/cluster.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ function masterInit() {
409409
} else {
410410
for (var key in workers) {
411411
key = workers[key];
412-
cluster.workers[key].disconnect();
412+
if (cluster.workers[key].isConnected())
413+
cluster.workers[key].disconnect();
413414
}
414415
}
415416
if (cb) intercom.once('disconnect', cb);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 cluster = require('cluster');
23+
24+
if (cluster.isMaster) {
25+
var worker = cluster.fork().on('online', disconnect);
26+
27+
function disconnect() {
28+
worker.disconnect();
29+
// The worker remains in cluster.workers until both disconnect AND exit.
30+
// Disconnect is supposed to disconnect all workers, but not workers that
31+
// are already disconnected, since calling disconnect() on an already
32+
// disconnected worker would error.
33+
worker.on('disconnect', cluster.disconnect);
34+
}
35+
}

0 commit comments

Comments
 (0)