2

My current setup is nginx -> reverse proxy -> nextjs. I"m using pm2 with cluster mode for nextjs. The problem is after nextjs is up for like 10 mins, it's stop responding without a trace of error but this issue is resolved when I restart nextjs app with pm2 restart all. It looks like my nextjs app only live for 10 mins and after that I have to restart. So I have to run a cron job to do this.

pm2 log output

2020-01-14T09:40:02: PM2 log: App [npm:0] starting in -cluster mode-
2020-01-14T09:40:02: PM2 log: App [npm:0] online
2020-01-14T09:40:02: PM2 log: App [npm:1] starting in -cluster mode-
2020-01-14T09:40:02: PM2 log: App [npm:1] online
2020-01-14T09:40:02: PM2 log: App [npm:2] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:2] online
2020-01-14T09:40:03: PM2 log: App [npm:3] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:3] online
2020-01-14T09:40:03: PM2 log: App [npm:4] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:4] online
2020-01-14T09:40:03: PM2 log: App [npm:5] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:5] online
2020-01-14T09:40:03: PM2 log: App [npm:6] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:6] online
2020-01-14T09:40:03: PM2 log: App [npm:7] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:7] online
> Server started at http://localhost:3000 with development ? false
events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1306:14)
    at listenInCluster (net.js:1354:12)
    at Server.listen (net.js:1442:7)
    at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1333:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 3000
}
events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1306:14)
    at listenInCluster (net.js:1354:12)
    at Server.listen (net.js:1442:7)
    at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1333:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 3000
}
events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1306:14)
    at listenInCluster (net.js:1354:12)
    at Server.listen (net.js:1442:7)
    at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1333:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 3000
}
events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1306:14)
    at listenInCluster (net.js:1354:12)
    at Server.listen (net.js:1442:7)
    at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1333:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 3000
}
events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1306:14)
    at listenInCluster (net.js:1354:12)
    at Server.listen (net.js:1442:7)
    at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1333:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 3000
}
events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1306:14)
    at listenInCluster (net.js:1354:12)
    at Server.listen (net.js:1442:7)
    at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1333:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 3000
}

uranium235
  • 81
  • 2
  • 10
  • any nginx logs? – Nico Jan 10 '20 at 08:23
  • @Nico sorry for my late reply. The error i got when i start the nextjs app is EADDRINUSE but the app works. My app run in cluster mod with pm2. – uranium235 Jan 12 '20 at 04:29
  • do you have any other app that runs on the same port? – Nico Jan 12 '20 at 17:06
  • Thank for the reply @Nico. It's the only app that run on that port. And when nextjs stops responding without error, the nginx server gets 499 from the nextjs app. – uranium235 Jan 13 '20 at 02:26
  • take a look here https://github.com/Unitech/pm2/issues/3987 – Nico Jan 13 '20 at 09:26
  • Thank @Nico. I look into the issues you described above. It looks like the issues was occurred in pm2 version 3.x.x but I'm using 4.x.x. So I think that they resolved this issues. I would like to provide more information. The is the pm2 log output when I start the my app with cluster option. I have 8 core cpu and It seems like out of the 8 processes of cluster mode. only first process can use the port and the other 7 throw the error. – uranium235 Jan 14 '20 at 03:16
  • I noticed a similar issue on my own environment. Note that you got 7 EADDRINUSE errors. I guess that only one instance is running just after you run the 'pm2 start' command and all the other are crashing (despite 'pm2 ls' telling you otherwise). Your only running instance may be crashing after 10 minutes for some other reasons. I am reproducing this issue with 'pm2 reload' : the old instance conflict with the new one. My guess is that nextjs is not playing well with node clustering and port binding across multiple instances. I got no solutions at this point but I keep you posted. – Raphaël Jun 08 '20 at 16:09

1 Answers1

3

Though you did not mention using npm, I fixed a similar issue by removing usage of npm from the ecosystem.config.json file.

Before (not working):

{
  "apps": [
    {
      ...
      "script": "/usr/bin/npm",
      "args" : "start",
      ...
    }
  ]
}

After (working):

{
  "apps": [
    {
      ...
      "script": "./node_modules/.bin/next",
      "args" : "start -p 3000",
      ...
    }
  ]
}

I did not take time to dig into the issue with npm.

I found the solution here: express server port configuration issue with pm2 cluster mode

Raphaël
  • 1,924
  • 2
  • 18
  • 22