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

Closed
stumpyfr opened this Issue Aug 31, 2013 · 49 comments

Comments

Projects
None yet

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 :).

Owner

Unitech commented Sep 1, 2013

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

stumpyfr commented Sep 1, 2013

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

Owner

Unitech commented Sep 1, 2013

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

mehulved commented Sep 7, 2013

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.

purepear commented Sep 9, 2013

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

Marsup commented Sep 19, 2013

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 :(

Owner

Unitech commented Oct 7, 2013

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

Collaborator

rlidwka commented Oct 7, 2013

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/Unitech/pm2/issues/74#issuecomment-26196575
.

Owner

Unitech commented Oct 19, 2013

@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

Collaborator

rlidwka commented Oct 19, 2013

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.

Owner

Unitech commented Oct 19, 2013

@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

Unitech closed this Oct 19, 2013

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)
    })
}
Collaborator

rlidwka commented Nov 21, 2013

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 joyent/node#6554.

Collaborator

rlidwka commented Nov 21, 2013

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" :)

wayoo commented Dec 3, 2013

@rlidwka That works well, thank you !

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/Unitech/pm2/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/Unitech/pm2/issues/74#issuecomment-33043051
.

arekkas referenced this issue in reactphp/react Feb 1, 2014

Closed

Client caught up in infinite loop #261

arekkas commented Feb 1, 2014

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

arekkas referenced this issue in serlo-org/athene2-legacy Feb 1, 2014

Closed

Dnode breaks apache #121

Collaborator

rlidwka commented Feb 1, 2014

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

Contributor

dandv commented Mar 7, 2014

@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?

Collaborator

rlidwka commented Sep 5, 2014

@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..

Owner

Unitech commented Sep 5, 2014

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 ?

loginx commented Sep 8, 2014

@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?

bvedam commented Sep 12, 2014

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.

cutsin commented Sep 23, 2014

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 May 26, 2015

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

Contributor

jshkurti commented May 26, 2015

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

axelboc commented Jul 25, 2015

Same issue and ESRCH error with the following config:

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

jshkurti commented Jul 27, 2015

@EddieOne you're on Windows as well ?

axelboc commented Jul 27, 2015

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.

nesukun commented Nov 12, 2015

+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
Amazon Web Services linux OS
node v0.10.36
pm2 0.15.10

+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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment