nodemon: Restart with --inspect flag fails with port in use error

  • nodemon -v: 1.17.4
  • node -v: v10.1.0
  • Operating system/terminal environment: Linux version 4.9.87-linuxkit-aufs (root@70f48253c8b4) (gcc version 6.4.0 (Alpine 6.4.0) )
  • Command you ran: nodemon --expose-gc --inspect=0.0.0.0:9228 --legacy-watch --delay 250ms --watch webpack.config.js node_modules/.bin/webpack-dev-server --mode development --host 0.0.0.0 --port 80

Expected behaviour

When a file has changed, it should restart the node application, with the following steps “stop the current application; start the a new instance of the application”

Actual behaviour

It’s starting the application before the previous instance finish to stop, what makes it to fail and display the following error:

[nodemon] starting `node --expose-gc --inspect=0.0.0.0:9228 node_modules/.bin/webpack-dev-server --mode development --host 0.0.0.0 --port 80 src/index.js`
Starting inspector on 0.0.0.0:9228 failed: address already in use
[nodemon] app crashed - waiting for file changes before starting...

If any the watched file get’s changed, it restart again and it work since it will be enough time to stop the application instance.

Steps to reproduce

Start a simple project with webpack-dev-server, babel and react, then add a dummy src/index.js that initialize react to make the process be a bit slower for webpack-dev-server. Start the nodemon using the command provided. execute: touch webpack.config.js it might fail if your computer isn’t fast enough to stop the instance before start the next.

Suggestion

Listen for the PID to be dead before start a new instance using a timeout or interval when --inspector flag is added to nodemon, to be sure that all resources are available before starting a new instance.


If applicable, please append the --dump flag on your command and include the output here ensuring to remove any sensitive/personal details or tokens.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 22 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Adding "signal": "SIGINT" to the nodemon.json fixed this for me

I’m experiencing the same issue From #1050 I tried the workaround of that comment

nodemon --delay 80ms --exec 'fuser -k 56745/tcp; node --inspect=0.0.0.0:56745 ./app/http.js'

replaced kill-port with fuser

Here is a repo that replicates this issue: https://github.com/darkobits/nodemon-restart-issue.

It seems that Nodemon isn’t waiting for the old process to completely exit before starting a new one, but only when in a Docker container and only when debug mode is active.

To be clear: The presence of --inspect does cause the new process to immediately crash because its trying to open a server on the same port that is still in use by the previous process, but this merely a side-effect of this issue. You will notice that even in the Docker container, if the --inspect flag is removed, Nodemon correctly waits for the old process to exit, but when --inspect is used inside the container, Nodemon does not wait for the process to exit.

I hope this helps.


P.S. – 👏 to Remy and the other contributors to this fantastic project. 😸

No idea but this is the full config I’m using, maybe it helps:

nodemon.json

{
  "restartable": "rs",
  "ignore": [
    ".git",
    "node_modules/**/node_modules"
  ],
  "verbose": false,
  "execMap": {
    "ts": "node --require ts-node/register"
  },
  "watch": [
    "server/",
    "config/"
  ],
  "ext": "json,ts",
  "signal": "SIGINT"
}

Start with (as NPM script): nodemon --inspect server/index.ts

Bizarrely changing the signal to SIGINT also solves the problem for me. I do not have Docker in the mix.

This happens to me outside of docker: https://github.com/nomcopter/nodemon-no-wait-repro

Can we mark this as worked around (I suspect the fact it’s nodemon plus node plus debugger protocol plus docker plus host system - might make the inception like debugging silly hard - certainly no one has offered any PRs, so I’m guessing I’m right on that one).

I’m happy to add this to the faq too.

I’m also running into this inside of a Docker container (Docker for Windows)

So the trick with fuser kinda worked for me, but still at times it would not manage to kill the process before starting a new one. So I resolved it by using a while loop to make sure we don’t move on until the old process is definitely killed.

here is my solution:

nodemon --delay 100ms --exec 'while [ -n "$(fuser -k 9229/tcp)" ]; do sleep 0.1; done; node --inspect=0.0.0.0:9229 ./src/app.js'

Hope this helps someone in the future