Repository navigation
Ctrl + C Doesn't Kill Gracefully | Node v6.11.3 - Git Bash #16103
Description
- Version: v6.11.3
- Platform: 64-bit Windows 10
- CLI: Git 2.14.1
- Express Version: 4.16.1
Referencing #4182 which seems similar (but at the end, it was stated that a new issue should be opened).
Ctrl + C does not kill the server. The resolution to the issue was using:
process.on('SIGINT', function() {
process.exit();
});however, when I use it, my server exits immediately. It completely skips app.listen() and exits completely, even when I previously kill the process on the specific port. Without the process.exit() function, it starts up and I can Ctrl + C to exit, but the process still runs in the background. I opened an issue on StackOverflow which hasn't gone anywhere really. This happens across multiple projects, so it's not just one project. I'm pretty sure it started happening when I updated node.
I am able to use netstat -aon to list it, then tskill <pid> to kill it afterwards, but this isn't really a solution.
Here's my code with my express server:
const express = require('express');
const app = express();
var config = require('./config')(process.env.NODE_ENV);
var appPort = process.env.PORT || config.port;
app.listen(appPort, function () {
console.log('UAction Test Server Running on Port ' + appPort + '!');
});
process.on('SIGINT', function() {
console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
// some other closing procedures go here
process.exit(1);
});UPDATE
So the server is able to start now with:
process.on('SIGINT', function() {
console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
// some other closing procedures go here
process.exit(1);
});I added app.use('/', express.static('./src'));, but removing it didn't change anything.
When I do Ctrl + C, the console.log("\nGracefully shutting down from SIGINT (Ctrl-C)" ); doesn't appear, and trying to restart causes the same issue. Just for reference, here's the log:
$ npm start
> express-test@1.0.0 start E:\github\uaction\express-test
> node index.js
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::1337
at Object.exports._errnoException (util.js:1020:11)
at exports._exceptionWithHostPort (util.js:1043:20)
at Server._listen2 (net.js:1258:14)
at listen (net.js:1294:10)
at Server.listen (net.js:1390:5)
at EventEmitter.listen (E:\github\uaction\express-test\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (E:\github\uaction\express-test\index.js:9:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
npm ERR! Windows_NT 10.0.15063
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.11.3
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! express-test@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the express-test@1.0.0 start script 'node index.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the express-test package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node index.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs express-test
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls express-test
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! E:\github\uaction\express-test\npm-debug.log
Activity
refack commentedon Oct 9, 2017
Hi @dark-swordsman,
I can't repro. Is there a specific way that you start the process?
dark-swordsman commentedon Oct 9, 2017
I start the process like any other. The
package.jsonlooks at"start": "node index.js". I usenpm startto start the process. The process then starts with the following console messages:I usually do
Ctrl + Cto exit the process, which usually gracefully kills it and it doesn't run in the background. What ends up happening is the command line returns to it's previous state:Doing
Ctrl + Cwithout any process running results in a^Cbeing printed, which is normal behavior.Then from there, doing
npm startagain (which I usually do and have done forever) results in a EADDRINUSE error (detailed above in OP).netstat -aonshows the localhost process running with a PID (which is random every time):then
tskill <pid>is used to kill it.I did find this in the documentation, which may be relevant, but I am not sure:
"
SIGTERMandSIGINThave default handlers on non-Windows platforms that resets the terminal mode before exiting with code128 + signal number. If one of these signals has a listener installed, its default behavior will be removed (Node.js will no longer exit)."According to that, I am not sure what I may have installed... unless it may be firebase tools? I will uninstall them temporarily and see if that fixes it, since I also changed that recently.
UPDATE
Uninstalling
firebase,firebase-tools,phonegap, andcordovadid not fix the issue.refack commentedon Oct 9, 2017
By my guess it wouldn't be a general CLI tool but rather something that traps keystrokes like ComEnu or clink.
There are several players in you scenario:
I'll try to reproduce so I can investigate, but the simplest answer I can give you, don't launch node from
mintty(Ref: #16104)dark-swordsman commentedon Oct 9, 2017
I just tried using Node.js command prompt (A CMD with Node: "
Your environment has been set up for using Node.js 6.11.3 (x64) and npm.") and it was able to successfully kill and restart the process. It also confirms if I want toTerminate batch job:So I can confirm that this is an issue with (at least) MinTTY, hopefully not with Git Bash completely.
I will continue to find anything that would interfere with Bash (I may start with just a clean re-install; perhaps different settings) since I would prefer not to work in CMD... Lol
EDIT: I want to mention I had Git version
2.14.1and am going to install version2.14.2.2which is the latest from Git.UPDATE: Updating to Git version
2.14.2.2did not work. I used the following settings:I will come back and reinstall changing from MinTTY to CMD.
dark-swordsman commentedon Oct 9, 2017
The issue was MinTTY. I don't know why, but changing to the Default Console Window fixed the issue.
EDIT: I referenced this issue in git-for-windows/git/issues/1219 which seems to be the most relevant issue. @dscho said something about MSYS2 being modified since v2.13.1 (which is what MinTTY uses) that causes this.
Here are the settings:
Here's it gracefully closing and restarting:
Everyone tells me Mac is better, and I'm starting to believe that more and more everyday, but I really don't want to fork over thousands when I already have a great working computer.
Might try ElementaryOS though. 🤔
Sorry for cluttering Node with another issue, but hopefully this will help being here since many people may think it's Node's fault like I did.
refack commentedon Oct 9, 2017
There are a few of us who develop node on Windows, so it's not a completely uncharted territory. Personally I use
ComEmuto run most of my shells; "Git bash", cmd, and "WSL bash", and I don't have zombie process problems.I believe the problem you were experiencing was the shell was disconnecting from the process, but the process didn't receive the signal (similar in a sense to
nohup). You could try runningwinpty npm start, I'm not sure but it might make sure the process dies if thettystreams are closed.dark-swordsman commentedon Oct 9, 2017
@refack doing that, I get an error:
I have never used or heard of
winptybefore, and searching for a solution to that error didn't return anything.danikane commentedon Oct 18, 2017
Hi all, so I experienced the same issue lately and as indicated here zombie processes were present when launching node in the git bash terminal MinTTY - launched from the default Git Bash shortcut and from the context menu on a folder - located in C:\Program Files\Git
So I decided to use bash.exe located in C:\Program Files\Git\bin and voilà- no more zombie processes!
Then I modified the shortcut and the windows shell context menu and no more nuisances.
See the screenshot below:

Hope this helps.
KiritoCheng commentedon Nov 6, 2017
I also met the same problem, this is the latest git bash bug, if back to the previous version can kill the process node
martinmckenna commentedon Nov 13, 2017
chiming in here to say I'm having the same issue.
Switching away from MinTTY fixed the issue
hottredpen commentedon Nov 14, 2017
i has same problem ,but my colleague use old git-bash version not the problem , so I use the git-bash version 2.8.1 now :)
dark-swordsman commentedon Nov 14, 2017
@KiritoCheng @martinmckenna @hottredpen I suggest you guys post that in the actual issue with a little more information. git-for-windows/git#1219
I'm sure it will help dscho pinpoint the issue a little more.
19 remaining items
38leinaD commentedon May 2, 2018
@dscho For the gradle-scenario described above it does not work. the process does not terminate on ctrl-c.
Merge branch 'ctrl-c-with-CtrlRoutine'
steranka commentedon Nov 24, 2018
For the record, this happens with cygwin as well.
I searched and found this command helps me know what process to kill:
Found from https://superuser.com/questions/1003921/how-to-show-full-command-line-of-all-processes-in-windows
dscho commentedon Nov 26, 2018
That looks more like MSYS than Cygwin. (And a pretty old one at that.)
ozyman42 commentedon Jan 24, 2019
Does anyone know of a workaround for the Mintty shell that ships with MSYS2? I don't want to give up MSYS2 or use git bash, because I love tmux and pacman too much.
Merge branch 'ctrl-c-with-CtrlRoutine'
Merge branch 'ctrl-c-with-CtrlRoutine'
Merge branch 'ctrl-c-with-CtrlRoutine'
Merge branch 'ctrl-c-with-CtrlRoutine'
Merge branch 'ctrl-c-with-CtrlRoutine'
Merge branch 'ctrl-c-with-CtrlRoutine'
Merge branch 'ctrl-c-with-CtrlRoutine'
Merge branch 'ctrl-c-with-CtrlRoutine'
neddongo commentedon Feb 21, 2021
So what about those of us who want to use mintty? We're just SOL?
This literally doesn't happen with any other program but Node, so why do you blame the terminal?
Merge branch 'ctrl-c-with-CtrlRoutine'