2

I'm hosting a website on a web host using a nodejs application (expressjs, request, etc.). The web host has an apache terminal from where I can call node app.js & or forever start app.js and the app runs fine. However, once the terminal is closed, the number of processes being used balloons to about 30, meaning that the next time I open the terminal, instead of being greeted with [hostname@server ~] $ I instead get:

bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: Resource temporarily unavailable
bash-4.2$

Somehow I believe a fork bomb is being created, and I can't seem to figure out where or why? I'm not using fork() anywhere, and if I use forever to start my app, the logs show no signs of errors or new processes being created.

When this happens, I can't use any terminal commands half the time, until I manage to pkill/pgrep.

I tried calling bash-4.2$ top, and got this:

top - 13:41:13 up 71 days, 20:57, 0 users, load average: 1.82, 1.81, 1.72
Tasks: 14 total, 1 running, 2 sleeping, 11 stopped, 0 zombie
%Cpu(s): 11.7 us, 2.7 sy, 0.1 ni, 85.5 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 41034544 total, 2903992 free, 6525792 used, 31604760 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 28583704 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1001511 xxxxxxxx 20 0 11880 3692 1384 S 0.0 0.0 0:00.02 bash
1001578 xxxxxxxx 20 0 11880 2840 524 T 0.0 0.0 0:00.00 bash
1001598 xxxxxxxx 20 0 11880 2672 348 T 0.0 0.0 0:00.00 bash
1001599 xxxxxxxx 20 0 11880 2896 524 T 0.0 0.0 0:00.00 bash
1001600 xxxxxxxx 20 0 11880 2720 396 T 0.0 0.0 0:00.00 bash
1001607 xxxxxxxx 20 0 11880 2928 532 T 0.0 0.0 0:00.00 bash
1001613 xxxxxxxx 20 0 11880 2964 532 T 0.0 0.0 0:00.00 bash
1001618 xxxxxxxx 20 0 11880 2780 348 T 0.0 0.0 0:00.00 bash
1001619 xxxxxxxx 20 0 12012 3024 544 T 0.0 0.0 0:00.00 bash
1001620 xxxxxxxx 20 0 11880 2804 372 T 0.0 0.0 0:00.00 bash
1001651 xxxxxxxx 20 0 12012 2836 352 T 0.0 0.0 0:00.00 bash
1001653 xxxxxxxx 20 0 12016 3392 896 T 0.0 0.0 0:00.00 bash
1004463 xxxxxxxx 20 0 9904 1840 1444 S 0.0 0.0 0:00.00 bash
1005200 xxxxxxxx 20 0 56364 1928 1412 R 0.0 0.0 0:00.00 top

Which looks like somehow bash processes are being repeatedly made?

My actual nodejs app.js is a typical expressjs web app, with code such as (shortened example code):

var app = express();

app.use(express.static('..//'))
   .use(cors())
   .use(cookieParser());

...

app.post('/profile', function(req,res){
    "use strict";
    var id = req.query.id;
    con.query("SELECT * FROM xxxxxxx WHERE xxxxx=" + id, function(err,result){
        res.send(result);               
    }); 
});

...

app.get('*', function(req, res) {
    "use strict";
    res.redirect('/404.html');
});

console.log('Listening on 8080');
app.listen(8080);

Any ideas on why this issue is occurring? I'm relatively new to node so any help is appreciated, and apologies if any noob mistakes have been made.

  • Does this happen when you don't use forever -- as in when you use node app.js & and exit, does it still happen? – abelito May 26 '19 at 14:20
  • using `node app.js &` still causes the error, however it eventually stops the app (domain posts a 503 error) while `forever` keeps the app running – sleepdotexe May 26 '19 at 14:23
  • You need to determine if the NodeJS process is actually restarting or if the NodeJS app is forking. To do this I recommend writing something simple to a file at the start of your NodeJS app load. If there are 30 entries in that file, the the OS is restarting NodeJS over and over. As a side note: NodeJS **is single threaded** and cannot spawn/fork a process on its own. Most people use NodeCluster for this purpose these days. Previously you would see the use of `child_process`. – Randy Casburn May 26 '19 at 14:25
  • 1
    Check out the EDIT of this answer for why forever might be restarting your app https://stackoverflow.com/a/32029341/370321 – abelito May 26 '19 at 14:27
  • @RandyCasburn Thanks for the idea, I implemented a script which wrote to a debug text file on the app load, but only 1 line was appended. It seems however from the `top` command that the processes being created are `bash` processes? – sleepdotexe May 26 '19 at 14:56
  • @abelito I had a look in the logs (~/.forever/) and it doesn't seem as though the app is actually being restarted? At no point in the log is `Forever detected script exited` which I've had in the past – sleepdotexe May 26 '19 at 14:58
  • Next test - remove all `console.log()` statements or any other statements that would use the console. – Randy Casburn May 26 '19 at 15:08

2 Answers2

1

So it seems that the problem is actually stemming from my ~/.bashrc file which contains these lines:

# User specific aliases and functions

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

If these lines are commented out, then the problem ceases to occur. However I obviously can't use node commands because nvm isn't started.

Somehow that code is looping creating multiple bash instances whenever the terminal is accessed.

-2

Do you use Visual Code Extension "Visual Studio IntelliCode" ? If yes try removing it. No more "Fork Bombs". Do not ask me why. I only know that after 3 hours hitting my head, i finally found a way to stop node forking processes.

CONTEXT: Visual Code on Windows, accessing a VPS with Ubuntu Server via Remote SSH Plugin at VS Code. NVM installed. If i remove NVM and/or remove the export line at ~/.bashrc nothing happens. Thousands of Zumbi processes are created. I remove Visual Studio IntelliCode Extension and Voilá! All OK.