I have an NodeJS application which sets up a UNIX-socket to expose some interprocess communication channel (some kind of monitoring stuff). UNIX-socket file is placed in os.tmpdir()
folder (i.e. /tmp/app-monitor.sock
).
var net = require('net');
var server = net.createServer(...);
server.listen('/tmp/app-monitor.sock', ...);
I use a signal handling (SIGINT, SITERM, etc...) to gracefully shutdown my server and remove a socket file.
function shutdown() {
server.close(); // socket file is automatically removed here
process.exit();
}
process.on('SIGINT', shutdown);
// and so on
My application is running with forever start ...
to monitor it's lifecycle.
I have a problem with forever restartall
command. When forever doing restartall
it's using a SIGKILL
to terminate all child processes. SIGKILL
can't be handled by a process so my app dies without any shutdown procedures.
The problem is a socket file which is not removed when SIGKILL
is used. After the child process is restarted, new server can't be started cause' a listen
call will cause a EADDRINUSE
error.
I can't remove a existing socket file during an app startup cause' I don't know if it a real working socket or some traces of a previous unclean shutdown.
So, the question is... What is the better way to handle such situation (SIGKILL and UNIX-socket server)?