Nodemon: Nodemon is not able to gracefully restart applications in Windows environment

Created on 14 May 2020  路  20Comments  路  Source: remy/nodemon

Possibly related to https://github.com/remy/nodemon/issues/1567

  • nodemon -v: 6.13.4
  • node -v: v12.16.1
  • Operating system/terminal environment:

    • Windows Command Prompt, Windows Git Bash, Windows Terminal WSL

         Edition: Windows 10 Pro 
         Version: 1909
    Installed on: 5/22/2019
        OS Build: 18363.836 

  • Command ran: nodemon --signal SIGHUP index.js


Expected behaviour

On Windows, Nodemon should wait for the process to complete if it handles a shut down signal.

Notice the express server closed message

WSL:
wsl

Actual behaviour

The application is killed and restarted right away. This behavior does not exist in Linux, Mac and WSL.

CMD:
windows

Steps to reproduce

I have created a sample Express app here: https://github.com/chriswoodle/nodemon-1567 that shuts down the express server once the application is signaled to stop ['SIGTERM', 'SIGINT', 'SIGHUP']

git clone https://github.com/chriswoodle/nodemon-1567.git
cd nodemon-1567
npm install
npm start
touch index.js

Let me know if I can provide any more information or test changes, thanks.


help wanted stale windows

Most helpful comment

If I get time, I'll look into this, but I wanted to stop and say:

Thank you for filing an issue with a good level of detail and a good example of pared down code to replicate the issue. I'll be pointing people to this issue as an example of what makes the world of difference in debugging. 馃檹

All 20 comments

If I get time, I'll look into this, but I wanted to stop and say:

Thank you for filing an issue with a good level of detail and a good example of pared down code to replicate the issue. I'll be pointing people to this issue as an example of what makes the world of difference in debugging. 馃檹

I came here with the same exact problem and I'm glad I found this issue. On *nix this works perfectly, on Windows it's almost like the signals are ignored and a SIGKILL is used instead? I'm seeing the same behavior regardless during restarts for file changes (or typing 'rs') - the application is IMMEDIATELY restarted and my shutdown handlers are never called.

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

Still happening..

Hi Remy.

Just a suggestion

I hadn't seen this problem on Windows via WSL until I enabled a subscription in my GraphQL server.
GraphQL Subscriptions use websockets, which may be relevant.

This seems to be consistent, activating a subscription will cause nodemon to not terminate the server before attempting to restart. It usually leaves more than one instance running.

I can't offer any solutions, but I'd be happy to help you debug it.

Hi there,

I dug a little bit deeper into this rabbit hole and like to share my findings:

Problem: taskkill does not send signals

If nodemon is running in a Windows context it is using the taskkill command to terminate the node process:

https://github.com/remy/nodemon/blob/a74f5dc3efef9a76e5c810ff3b70c4e3f1584183/lib/monitor/run.js#L347

Sadly taskkill does not send any signals prior to reaping the process. Therefore no process event listeners within the Node.js application will trigger.

Solution: use windows-kill

There is actually a way to programmatically send the following signals to a process in Windows:

  • SIGINT ( CTRL + C )
  • SIGBREAK ( CTRL + Break )

There is a project called windows-kill which implements this:

.\windows-kill.exe -SIGINT 16312 
The node process (16312) received a SIGINT POSIX signal, starting graceful shutdown...

I used the windows-kill_x64_1.1.4_lib_release.zip binary.

Next steps

@remy I think the fix would be a drop in replacement of taskkill with windows-kill. But I have also seen the big comments above the taskkill invocation and do not know all edge-cases you have already tested for. And I am not sure, if it is possible to simply add the windows-kill.exe binary to this project (yet another dependency, license issues etc.). What do you think?

Facing issue nodemon stuck at restarting the server on windows 10 platform . Any trick around ?

Still isn't resolved

i have some issue with nodemon when i use command nodemon app.js -e js,hbs after making some changes in hbs files nodemon is not restarting only for the first time its working later i have to shutdown the app.js and rerun the command

Finally getting around to this - given @chriswoodle's code example, my primary question is: how does this work without nodemon?

@countzero I'm not sure this is the right solution - my reading is that this problem is a failing of the code running in a system that doesn't have POSSIX signals, and therefore the source code shouldn't be listening for these. Now, I could be entirely wrong, but that fact it works in WSL suggests that the windows-kill project is actually monkey patching _in_ signals. Nodemon definitely does not want to do this because it'll change the running environment without nodemon (and one of the core principles that I first built nodemon with is: not to change the behaviour of runtime - i.e. no code changes required, etc).

I don't often have a windows machine to hand test this, but there's a good number of you on this thread that could try @chriswoodle's example git repo that replicates the problem.

The tasks is: run without nodemon, does the application behave as expected?

Finally getting around to this - given @chriswoodle's code example, my primary question is: how does this work without nodemon?

@countzero I'm not sure this is the right solution - my reading is that this problem is a failing of the code running in a system that doesn't have POSSIX signals, and therefore the source code shouldn't be listening for these. Now, I could be entirely wrong, but that fact it works in WSL suggests that the windows-kill project is actually monkey patching _in_ signals. Nodemon definitely does not want to do this because it'll change the running environment without nodemon (and one of the core principles that I first built nodemon with is: not to change the behaviour of runtime - i.e. no code changes required, etc).

I don't often have a windows machine to hand test this, but there's a good number of you on this thread that could try @chriswoodle's example git repo that replicates the problem.

The tasks is: run without nodemon, does the application behave as expected?

Using windows cmd, running node index and pressing CTRL + C sends SIGINT and waits for the process to exit.

image

Even when adding a timeout period.

image

@chriswoodle I was pretty sure that the command prompt checked with confirmation whether you wanted to exit (the "are you sure y/n") on ctrl-c.

Why wouldn't that happen in this case? Or is this a particular flavour of Windows/the prompt/something else?

Hmm... unless it's node intercepting the interrupt and doing something bespoke...

@chriswoodle I was pretty sure that the command prompt checked with confirmation whether you wanted to exit (the "are you sure y/n") on ctrl-c.

Why wouldn't that happen in this case? Or is this a particular flavour of Windows/the prompt/something else?

I believe this was the case in the past, "Terminate batch job (Y/N)?" is promoted when exiting batch/cmd scripts. I think node/npm switched over to powershell ps1 scripts, which do not have this prompt. Not %100 certain though, but I think it's something like this.

Then I suspect this is what nodemon will need. I _really_ don't want to add a bespoke binary 3rd party to do the windows-kill thing - it doesn't feel right. But I really know nothing about powershell so am looking to Windows users to help advise/drive.

Just to note,
Looking at my windows node install:
image

Runnng node from cmd

image

Directly executes node.exe, so no powershell involved.

I wonder what the nodemon executable looks like on Windows? Isn't it one of those .cmd files?

nodemon is a cmd file, which calls node.exe with arguments:
image

so you have a node.exe for nodemon, monitoring a node.exe for the app:
image

From tasklist:
image

@remy how can I help with this. I've got a Windows machine.

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

piton13 picture piton13  路  3Comments

olalonde picture olalonde  路  3Comments

Autre31415 picture Autre31415  路  4Comments

ehmicky picture ehmicky  路  4Comments

giacomorebonato picture giacomorebonato  路  5Comments