express server process dosn't get killed on ubuntu 16.04 so nodemon fail to restart the app
See original GitHub issuenodemon
: ^2.0.4node -v
: v14.4.0- Operating system/terminal environment: Ubuntu 16.04.6 LTS
- Using Docker? What image: no
- Command you ran: npm run dev
Expected behaviour
to close current open server process and lunch a new one when the file changes
Actual behaviour
fail to restart the server because the process is already running on the same port
root@medo:~/ECommerceApp# npm run dev
> ecomerce-app@1.0.0 dev /root/ECommerceApp
> nodemon index.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
App listening on port 3000
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at Server.listen (net.js:1447:7)
at Function.listen (/root/ECommerceApp/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/root/ECommerceApp/index.js:40:5)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1340:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
^C
root@medo:~/ECommerceApp# lsof -i:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 4887 root 19u IPv6 4033651895 0t0 TCP *:3000 (LISTEN)
root@medo:~/ECommerceApp#
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:20 (3 by maintainers)
Top Results From Across the Web
Nodemon: Error: listen EADDRINUSE: address already in use
I've to explicitly kill the server from the terminal every time, which is not the optimal solution. I tried several things, but none...
Read more >Let It Crash: Best Practices for Handling Node.js Errors on ...
Some strategies to gracefully shutdown the Node.js process and quickly restart your application after a catastrophic error terminates your ...
Read more >How to Deploy, Manage and Scale Node.js Apps with PM2
Learn the most important PM2 features and how to use them to deploy, manage, and scale your Node.js applications in production.
Read more >10 best practices to containerize Node.js web applications ...
Are you looking for best practices on how to build Node.js Docker images for your web applications? Then you've come to the right...
Read more >How To Set Up a Node.js Application for ... - DigitalOcean
This means that they will restart on reboot or failure and are safe ... a package manager for Node modules, so you don't...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Note that nodemon is not responsible of closing the express connection, your server is.
Nodemon cannot actually know which socket the http server bound to on the operating system level and thus cannot cut the connection, so if you do not gracefully shut down your server then there is a good chance that the socket remains bound until it times out which can happen after the process has already exited and nodemon restarts it.
I wrote a bit more detailed solution in this other issue: https://github.com/remy/nodemon/issues/1748#issuecomment-680420175
If anyone wants to understand what is happening here, imagine and old-school landline phone call: you call your friend and you start talking and at some point your call accidentally disconnects and you immediately dial back (e.g. nodemon restarts the server). But because your friend didn’t notice right away that you disconnected and they don’t close their line right away, their landline is still active and all you get is a busy signal on your end. Just because one side closed the connection, doesn’t mean both sides did that at the same time. The call is successfully/gracefully terminated only when you both agree to hang up and finish the call.
So don’t be a jerk and just hang up, tell your friend (http server) to close the connection too! 😉
I’m running nginx in foreground through
--exec
and whenever it restarts, it fails with “address in use”.With nodemon:
But if I manually restart the process immediately after interrupting, it works:
EDIT:
I tried calling nginx directly from nodemon instead of through the yarn script, and I saw this when making a change:
ad infinitum. So I added
--signal SIGQUIT
and now it works fine.Though it certainly does make me wonder why it’s not sending a real kill signal in the first place? hmmm