1

I have a master process that communicates with children processes via UNIX sockets. I am unable to write to the children's sockets when the master process receives a SIGQUIT. I would like the child processes to know that the master is quitting and to gracefully exit.

Communication outside of a SIGQUIT trap works perfectly as expected.

Here's some sample code that reproduces the problem. Remember that CRTL + \ sends a SIGQUIT. (CTRL + C is SIGINT)

Master process: test.js

var net = require("net");
var spawn = require("child_process").spawn;

var socket = new net.Socket();

path_to_worker = process.cwd() + "/test_child.js"

var child = spawn("node", [path_to_worker]);
child.stdout.on('data', function (data) {process.stdout.write(data);})
setTimeout(function() {
  socket.connect("/tmp/node-sock", function () {
    socket.on('data', function(data) {
      console.log(data.toString());
    });
  });
}, 100)

process.on("SIGQUIT", function () {
  socket.write("This won't appear");
});

Child process: test_child.js

var net = require("net");

var socket = new net.Socket();
console.log("Started");
net.createServer(function (server_socket) {
  server_socket.on('data', function(data) {
    console.log(data.toString());
  });
  server_socket.write("This will appear");
}).listen("/tmp/node-sock");

The child processes are out of my control, and use a mask to block out all signals except SIGKILL and SIGTERM. If it simply isn't possible to write to their socket during a SIGQUIT trap, is there another way to communicate with them other then sockets?

4

1 回答 1

0

从 Gihib,koichik 回答了我的问题: https ://github.com/joyent/node/issues/1308#issuecomment-1552790

我通过kill -SIGQUIT(不是C-)向父进程发送了一个SIGQUIT,它可以工作。

$ node test.js
Started
This will appear
This won't appear

所以我将代码添加到 test.js,然后按 C-。

child.on('exit', function(code, signal) {
  console.log(' child died', code, signal);
});

结果:

$ node test.js
Started
This will appear
^\ child died null SIGQUIT

SIGQUIT 也被发送到子进程,而不仅仅是父进程。因此,我将代码添加到 test_child.js。

process.on("SIGQUIT", function () {
  console.log(' child received SIGQUIT');
});

结果:

$ node test.js
Started
This will appear
^\ child received SIGQUIT
This won't appear
于 2011-07-12T18:45:12.707 回答