How to crash my Node app on purpose?

后端 未结 4 583
无人及你
无人及你 2020-12-30 20:27

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

相关标签:
4条回答
  • 2020-12-30 20:58

    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.
    0 讨论(0)
  • 2020-12-30 20:58

    To add to Golo answer:

    C module to crash by segmentation fault:

    int main ()
    {
        //Create a array of 1 char
        char a [1];
        //Create a index
        int i = 0;
        //Infinite loop to go around the compiler
        while(1)
        {
            //Write on case i of a, on the second iteration, it will write in unreserved memory => crash
            a[i] = 0;
            i = i + 1;
        }
        //Should not go there
        return -1;
    }
    
    0 讨论(0)
  • 2020-12-30 21:01

    And adding to DrakaSAN's answer, an even simpler C module to crash:

    int main()
    {
        *(int*)(0) = 0;
        return -1;
    }
    

    Even shorter ones are available on this page. If you don't want it to be too hard to read, you can probably go with

    int main()
    {
        int i=1/0;
    }
    
    0 讨论(0)
  • 2020-12-30 21:06

    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);
    
    0 讨论(0)
提交回复
热议问题