Nodejs - process hangs on exit (Ctrl+C)
我有一个node.js项目,它执行许多操作,它产生子进程,打开一个http和socket.io服务器,等等。
当我从控制台运行它时,用
从webstorm来看,停止该过程分为两个步骤,首先单击Stop,然后再次单击该按钮,仅第二次该按钮是一个头骨图标。
现在,我知道它留下了一些悬而未决的东西,但是我无法弄清楚是什么,我试图跟踪所有启动过程的地方,并确保已正确杀死它们。
有没有一种方法可以调试它并找出导致我的进程挂起的原因? 难道是日志记录打开了写流,并且永远不会关闭? 我什至不知道什么样的事情会使一个进程挂在SIGINT上。
编辑:我已经下载了
脚本本身负责在监听
查看以下示例程序:
1 2 3 4 5 6 7 8 9 10 11 12 | process.on('SIGINT', function() { console.log('SIGINT'); }); console.log('PID: ', process.pid); var http = require('http'); // HTTP server to keep the script up long enough http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\ '); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); |
执行它,然后尝试将其杀死:它不起作用。
1 2 3 4 5 6 7 8 9 10 11 12 13 | process.on('SIGINT', function() { console.log('SIGINT'); process.exit(); }); console.log('PID: ', process.pid); var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\ '); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); |
长话短说:您的代码可能在某处监听了
1 | var listeners = process.listeners('SIGINT'); |
您甚至可以在控制台上漂亮地打印它们:
1 2 3 | for (var i = 0; i < listeners.length; i++) { console.log(listeners[i].toString()); } |
使用我在上面提供的信息,您可以轻松地编译另一个
1 2 3 4 5 6 7 8 9 | process.on('SIGINT', function() { console.log('Nice SIGINT-handler'); var listeners = process.listeners('SIGINT'); for (var i = 0; i < listeners.length; i++) { console.log(listeners[i].toString()); } process.exit(); }); |
完整的测试程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | process.on('SIGINT', function() { console.log('Naughty SIGINT-handler'); }); process.on('exit', function () { console.log('exit'); }); console.log('PID: ', process.pid); var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\ '); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); process.on('SIGINT', function() { console.log('Nice SIGINT-handler'); var listeners = process.listeners('SIGINT'); for (var i = 0; i < listeners.length; i++) { console.log(listeners[i].toString()); } process.exit(); }); |