0

I had an interesting issue one night when we had to deploy. We had a setup where we had two different servers set up on the same docker container, so to start both servers we'd use something like node server1.js & node server2.js.

I know that using pm2 and other ways of managing the services are way better but at the time we weren't launching a full production build, we just quickly wanted to get it up and running.

But then we had a really strange issue. When you node server1.js everything works perfect, when you do node server2.js everything works perfect, but when you do node server1.js & node server2.js it broke with an error saying that "port 4000 is already in use".

Server 1 was using port 4000 and server 2 was using port 4001, other than that there were no node processes and nothing else was using any ports (or at least certainly not 4000). I spent a good 2 hours searching everything I can for that port, but there was nothing.

I ended up realizing that "something" is happening when you background process the first server that makes it somehow try to restart itself, then it breaks on its own port. It's like it starts it, then dies or something, then immediately tries to restart itself again on the same port.

Till today I don't really have an explanation for why exactly that happened.

I ended up using nohup which solved the problem and made both servers start up just fine, no more port issues, but I'd really like to understand why.

  • Why exactly does using node server1.js & node server2.js cause a port conflict (or what's happening there)?
  • Why exactly does using nohup fix it?

EDIT: Based on the comments I guess what's happening is that when you run it in a docker container the shell will automatically exit once you've executed your commands (even if you're in a different shell), so once you background process the first server & won't pick up on the SIGHUP signal (or won't send it at all), which makes the first process run without stopping. Using nohup fixes that because it will pick up on the SIGHUP signal (or add if it it doesn't exist), so it can properly pick up on the first process being killed and restart it properly, thus avoiding the port conflict. So I guess the only thing left that I don't understand is why the server is started twice in the first place, or why it's restarted on using &, instead of just starting it once and background processing it.

  • [What's the difference between nohup and ampersand](https://stackoverflow.com/questions/15595374/whats-the-difference-between-nohup-and-ampersand)? – jfriend00 Aug 15 '21 at 15:15
  • Thank you for that, that's pretty informative. But it leads to more questions, like I didn't actually terminate any shell, it was still open on my screen, so why is it sending a ````SIGHUP```` at all? Does ````&```` automatically send a ````SIGHUP```` even if you didn't actually exit the shell? Also even if it does send a kill signal, why would that cause a port conflict, I'd assume then that it properly kills the process (so there shouldn't be one running any more), even more why does it automatically try to restart itself? I'm missing some puzzle pieces. –  Aug 15 '21 at 15:24
  • The port conflict has me baffled. You will have to do some debugging to gather more info. For starters, you can add debug messages for a SIGHUP to see if your process is getting one of those and add debugging to see if your process is getting started more than once. It's really up to you at this stage to gather more debugging info. – jfriend00 Aug 15 '21 at 15:29
  • Unfortunately I don't have access to any of those servers any more. But so far I understand that nohup will catch the ````SIGHUP```` and therefore will be able to properly kill the process before restarting it, avoiding the port conflict. However the key question is still why background processing a service restarts it (or why it runs it twice). "if your process is getting started more than once" -> I already know this, it is getting started more than once, I just don't know why. –  Aug 15 '21 at 15:39
  • Well, it's less likely we can help any further (beyond wild guesses) if this is purely a theoretical exercise with no access to the code or the ability to run tests. Theoretical exercises don't work that well here on stackoverflow. This places works a lot better when there's code we can see, run, understand and offer fixes for or you can carry out tests and report info back. – jfriend00 Aug 15 '21 at 15:47
  • That's just not correct, you have the fact that using ````&```` makes a server restart itself when it's background processed, I just want to understand why (that's not theory, that's a technical fact). No debugs or tests or anything about my vanilla server is going to add more to that. –  Aug 15 '21 at 15:50
  • Then, if you're claiming this has nothing to do with whatever the server code itself is doing and this is purely an OS-type of question, then you're probably asking in the wrong forum. Regardless, I have no idea how to help further if it's going down that path. Good luck. – jfriend00 Aug 15 '21 at 15:52

0 Answers0