Note that there are some explanatory texts on larger screens.

plurals
  1. POnode.js process won't die on unhandled exception
    text
    copied!<p>Running the following server.js:</p> <pre><code> cluster(app) .use(cluster.logger(path_to_logs)) .use(cluster.stats()) .use(cluster.pidfiles(path_to_pids)) .use(cluster.cli()) .use(cluster.repl(8888)) .listen(3000); </code></pre> <p>it works as expected. However, let's throw in an unhandled exception like so:</p> <pre><code> setTimeout(function () { throw new Error('User generated fault.'); },5000); </code></pre> <p>Running the server with `$ node server.js, it starts and the exception is thrown after five seconds. Consequently the server is quit in what is seemingly the same as pressing ctrl+c.</p> <p>However not quite. Because now trying to restart the server using <code>$ node server.js</code> I receive the following error:</p> <pre><code>Express server listening on port 3000 node.js:134 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: EADDRINUSE, Address already in use ... </code></pre> <p>And running <code>$ ps aux | grep node</code> I can see that I still have two node processes running. Killing them allows me to start the server again. But since it was a manual kill, if I start the server again the same procedure starts over. 5 seconds pass, throw error, unable to restart.</p> <p>This is a problem because with forever, it causes an infinite death cycle upon the first unhandled exception.</p> <p>So my questions are:</p> <ol> <li>Do you have any further ideas on why this might occur?</li> <li>How can I listen for all exceptions and react by kill the process(es)</li> <li>Is the above a bad approach?</li> </ol> <hr> <p>Sorry for posting this on ServerFault aswell, but I realized this IS actually a code question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload