开发者

Why can't I write to a child process's socket during a SIGQUIT?

开发者 https://www.devze.com 2023-03-19 16:55 出处:网络
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 p

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 () {
  开发者_JAVA百科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?


From Githib, koichik answered my question: https://github.com/joyent/node/issues/1308#issuecomment-1552790

I sent a SIGQUIT to the parent process by kill -SIGQUIT (not C-), it works.

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

So I add the code to test.js, and press C-.

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

result:

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

The SIGQUIT was also sent to the child process not only the parent process. Therefore I added the code to test_child.js.

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

result:

$ node test.js
Started
This will appear
^\ child received SIGQUIT
This won't appear
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号