Unitech/pm2

Node 0.10.x - Port still in use after a "pm2 stop :id"

stumpyfr opened this issue · 49 comments

When I kill a pm2 process (http server with expressjs) with the kill command and try to restart the same process without pm2 I get:

server started on port 4242
   warn  - error raised: Error: listen EADDRINUSE

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:901:11)
    at Server._listen2 (net.js:1039:14)
    at listen (net.js:1061:10)
    at Server.listen (net.js:1127:5)

If I start the process with pm2, everything is working.

To be able to start the process without pm2, I need to kill the pm2 demon process: 'pm2: Satan Daemonizer'.

Not very practical :).

I tried with node v0.8.17 and v0.11.2, everything is okay, the port is freed when a process is stopped.

The command to stop a process managed by pm2 is pm2 stop <pm2_id|script_name|all>. pm2 kill is only to kill pm2.

Which node version do you use ?

Thanks

my mistake, It's was about pm2 stop ;) and not kill.
node version: 0.10.17

I confirm there is a bug with node v0.10.x the port is not freed when stopping the process with pm2 stop.
With v0.8.x and v0.11.x there isn't this bug.
I will try to have a deeper look at it, thanks for reporting this issue

We've seen this issue even with node 0.8 but can't consistently reproduce it. If I do, I'll report it. What would be the suggested logs/trace that you'd need so I can get them if I run into this bug again.

Having the same issue with 0.10.. I had to make my git push hook kill and start the pm2 daemon

It happens everytime for me, I'm still on node 0.8.25.

@Unitech http://nodejs.org states: Current Version: v0.10.19
great if we could get read of this major bug on 0.10.x
pm2 kill on github webhooks makes no fun :(

i haven't found any solution to fix that. I tried 4 differents ways and nothing work with 0.10.x...

The only solution will be in the next pm2 version, who include "fork mode", it will not wrap the code anymore and the cluster feature will be disabled :(

Hope everyone will switch to 0.11.x or that the 1.x node version will have the same engine for cluster than 0.11.x

i haven't found any solution to fix that.

At least one solution would be to clone cluster.js from v0.11.x source code and use it in v0.10.x (the same way as people use streams2 in v0.10.x with readable-stream fallback for v0.8.x).

It would be a pain to maintain it though, so it's easier for people to just switch to v0.11.x, it's only a few months from being stable anyway.

does pm2 play nicely with nvm?

can i use node v0.11.x to run pm2 but make it still use node v0.10.x for running scripts i want to manage with it?

You can get this to happen when you shut down node improperly. Its a node
error not pm2, although i feel that if pm2 shuts node down differently it
may solve the issue you have

Matej Kramny

On 12 Oct 2013, at 01:28 pm, "☮ elf Pavlik ☮" notifications@github.com
wrote:

does pm2 play nicely with nvm?

can i use node v0.11.x to run pm2 but make it still use node v0.10.x for
running scripts i want to manage with it?


Reply to this email directly or view it on
GitHubhttps://github.com//issues/74#issuecomment-26196575
.

@matejkramny I tried shut down process in different ways (disconnecting, suicide, kill...) nothing works, the port is still not freed.

@rlidwka you think it's possible ? I'm scarred that all this mechanism comes from C++ modules and not the cluster.js, I will have a look at it

@elf-pavlik as the "cluster mode" require your code, you can't use different node version

I'm scarred that all this mechanism comes from C++ modules and not the cluster.js, I will have a look at it

cluster.js doesn't even require any native modules, but base around process.send(message, fd) function which I believe is the same in 0.12, 0.10 and even 0.8. Can't be sure though.

@rlidwka I tried to use the Node v0.11x cluster module for 0.10.x, without success

For Node v0.10.x the workaround is now to launch your script in "fork mode" by adding the -x parameter.
To beneficiate of the fork mode you have to install at least the 0.6.3 version of pm2

$ npm install -g pm2@latest

I've added a section "known bugs" in the README.md : https://github.com/Unitech/pm2#known-bugs

Thanks for reporting, closing

Did you ever report this to joyent/node?

Do you have a simple reproduction? Below doesn't cause any listen socket leak.

var cluster = require('cluster')
var net = require('net');

if(cluster.isMaster) {
  cluster.fork()

  cluster.on('exit', function() {
    setTimeout(function() {
      cluster.fork();
    }, 1000)
  })
} else {
  var port = Math.floor(Math.random() * 14000 + 2000)
  var server = net.createServer()
    .listen(port, function() {
      console.log('pid %d listen on:', process.pid, port);
      process.nextTick(function() {
        process.disconnect()
      })
    })
    .on('error', function(er) {
      console.log('listen error:', er)
    })
}

Did you ever report this to joyent/node?

There's no reason to, they'll probably not fix this on stable. Why waste time, 0.10.x is almost in maintenance only mode.

Do you have a simple reproduction? Below doesn't cause any listen socket leak.

Instead of cluster.fork in setTimeout run another process (not fork) that will try to bind the same port. If all workers exit, it should bind successfully, but on 0.10 it won't.

v0.8 is still pretty used, v0.10 is going to be around a bit. Whether it is fixed or not probably depends on how hard it is to fix, and how much it hurts users.

Anyhow, @Unitech, depending on how pm2 stop is implemented, consider calling cluster.disconnect() when you know you have no more workers (like, after they have all been stopped), it will close the server handles. See gist linked to by nodejs/node-v0.x-archive#6554.

Anyhow, @Unitech, depending on how pm2 stop is implemented, consider calling cluster.disconnect() when you know you have no more workers

You can call pm2 kill instead to stop it completely.

I'm doing it quite often when I forget the difference between "stop" and "kill" :)

What is the resolution / workaround for this? It's still occurring for me on v0.10.24.

Is it the same issue when you kill a node app with ctrl-z?

Matej Kramny

On 22 Jan 2014, at 04:48 pm, Eric Martindale notifications@github.com
wrote:

What is the resolution / workaround for this? It's still occurring for me
on v0.10.24.


Reply to this email directly or view it on
GitHubhttps://github.com//issues/74#issuecomment-33041863
.

^Z should pause an app, not kill -- the port should remain in use in this case (you can resume with bg or fg for backgrounding or foregrounding the process, respectively).

Oh. Hmm.
Well i was referring to a problem which i encountered during development of
an app, while using nodemon.
My app would crash, and port remained open after restart.

I could solve this by visiting the url (which strangely still sent some of
the content). Visiting again would return 404, after which the port was
usable again.

Matej Kramny

On 22 Jan 2014, at 04:59 pm, Eric Martindale notifications@github.com
wrote:

^Z should pause an app, not kill -- the port should remain in use in this
case (you can resume with bg or fg for backgrounding or foregrounding the
process, respectively).


Reply to this email directly or view it on
GitHubhttps://github.com//issues/74#issuecomment-33043051
.

So how do I solve this? Upgrade node to 0.11.2?

Upgrade node to latest version in 0.11.x branch. It's 0.11.11 now.

@rlidwka: I just saw an update from 0.10.25 to 0.10.26 last week, so it's still maintained. Might be worth bugging Joyent.

are you sure it's still the same process ? can you have lock at the pid and confirm ?

it still happen..how to solve it?

@goldalworming , update to node 0.11.x?

in nodejs.org Current Version: v0.10.31
I use pm2 in production..I haven't test the application in 0.11..

gracefulStop (not available) that sends a shutdown message to the application so you can app.close() and so free the port ?

Btw currently the solutions are:

  • use Node.js v0.11.13
    or
  • pm2 kill when you need to free the port in order to run your app without pm2
    or
  • run your app in fork mode

I experience the same issue and I am running in fork mode on node 0.10.31. Can you elaborate on that, @Unitech ?

@Unitech So my understanding is that for a project that isn't using a web server, cluster mode on node 0.10 is still ok, am I correct?

I use pm2 to manage a pool of workers, and I get the warning about fork mode, but everything is working fine. Maybe that warning should only be presented when pm2 manages a listening socket for the managed processes?

I use node version 0.10.26. Installed pm2 latest version 0.10.7. Port was not getting freed up. uninstalled pm2 and went back to 0.9.4. Works fine now.

I'm sure that it's pm2@0.10.2's problem. The pm2@0.9.5 works fine.

On pm2@0.10.2 later, "exec_mode": "cluster_mode" was fallback to fork_mode

dlinx commented

Same problem with pm2 0.12.10
Tried with pm2 stop, pm2 restart, pm2 kill.

pm2 is not killing process if the process is running for few days and when I restarted that application using pm2 restart. I needed to kill node process manually from task manager.

platform: Windows server x64
pm2 version: 0.12.10

Give it a try with PM2 v0.12.15 and Node.js >= 0.11.x

Suffering the same thing as the original post.

PM2 0.14.2
Node v0.12.4
Windows 8

Because of it. automatic reloading also fails and thus is unusable.
Only way to start my server again is to kill the node process from task manager and start again.

Any logs or files that I can help with to try and find a solution?

Same here.

Windows 7
pm2 0.14.3
node 0.12.2
npm 2.7.4

It fails to kill the process, only way to get the port free again is to manually kill the node.exe from process manager.

This is what the pm2 logs shows when trying to do pm2 restart app

PM2 Stopping app:app id:0
PM2 7620 pid can not be killed { [Error: kill ESRCH] code: 'ESRCH', errno: 'ESRCH', syscall: 'kill' }
PM2 Starting execution sequence in -fork mode- for app name:app id:0
PM2 App name:app id:0 online

Same issue and ESRCH error with the following config:

  • Windows 8.1
  • pm2 0.14.5
  • node 0.12.4
  • npm 2.11.3

@EddieOne you're on Windows as well ?

Not sure if it matters at all, but my app does real-time things with Primus. I tried listening for the shutdown signal and gracefully closing the Express and Primus servers, but that didn't fix the issue.

+1
node v0.12.7
pm2 0.15.10
osx 10.11.1

I wonder, does this always happen on machines that also have forever globally installed?

+1
RHEL7
node v0.10.36
pm2 1.0.0
I literally need to reboot the server every time a process goes rogue since trying to do a kill -9 <PID> based upon the output of netstat -nltp | grep 3000, says the process doesn't exist, yet somehow blocking my port 3000! Also pm2 list or pm2 monit says nothing is running.

FYI, found a possible source of problem on cloud servers.
pm2 was launched by default as a sudo process in my automatic script, so when I was trying to relaunch one as a normal user I should be careful to use sudo.

Note : pm2 isn't the same using sudo & no-sudo users

+1

cmal commented

+1
$ uname -a
Linux xxx 2.6.32-573.8.1.el6.x86_64 #1 SMP Tue Nov 10 18:01:38 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

$ node -v
v8.9.3

$ pm2 -v
2.9.1

i'm sad to report this bug still exists