Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crash my Node app on purpose?

Tags:

node.js

I've been working on a deployment work flow with Dokku and Docker and now I want to take care of continuity of my app (along the lines of Forever). To test it, I need a way to deliberately crash my app.

I created a new route '/crashme' with a function that is supposed to wreck my app.

Haven't found a way that worked locally with node/nodemon so far, I've tried:

  • Division by zero
  • Throw a new user exception
  • Referencing a variable that doesn't exist

None of those things crash the app to a point where it needs to be restarted.

Just how can I bring it down?

like image 522
Alexander Rechsteiner Avatar asked Dec 30 '13 13:12

Alexander Rechsteiner


People also ask

Can I close node EXE?

When you find the node.exe file is causing problems or taking too many resources, you can consider ending the process. You just need to open Task Manager, right-click node.exe, and select End task.

What are exit codes in node JS?

Node normally exits with code 0 when no more async operations are pending. process. exit(1) should be used to exit with a failure code.


2 Answers

Three things come to my mind:

  • You could just call process.exit. This for sure brings your application to a state where it needs to be restarted.
  • The other option might be to run an endless loop, something such as while (true) {}. This should make Node.js use 100% of your CPU, and hence the application should be restarted as well (although this, of course, means that you / someone has to watch your application).
  • Create a module in C that crashes by e.g. trying to access a random place in memory. I have no such module at hand, but I'm pretty sure that it should be quite easy for someone with C skills to write such a module.
like image 77
Golo Roden Avatar answered Nov 02 '22 09:11

Golo Roden


I was attempting a similar thing with a /crash route in express, but just throwing an error from within the route handler was not enough to crash it.

process.exit would stop my app but forever would not restart it. (The forever logs just said something like process self terminated.)

What did work for me was inserting this into my /crash route:

setTimeout(function () {       throw new Error('We crashed!!!!!'); }, 10); 
like image 29
Lindsay-Needs-Sleep Avatar answered Nov 02 '22 08:11

Lindsay-Needs-Sleep