-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nodemon is not able to gracefully restart applications in Windows environment #1720
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. |
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. 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 signalsIf nodemon is running in a Windows context it is using the taskkill command to terminate the node process: Line 347 in a74f5dc
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-killThere is actually a way to programmatically send the following signals to a process in Windows:
There is a project called windows-kill which implements this: .\windows-kill.exe -SIGINT 16312
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 |
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? |
Using windows cmd, running Even when adding a timeout period. |
@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... |
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 |
I wonder what the nodemon executable looks like on Windows? Isn't it one of those |
@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. |
Just bumping this because of the message from the bot and this does not appear to be resolved as of yet. Ideally nodemon should send a signal and wait for the process to exit before restarting and it does not currently do this. To clarify on the "Terminate batch job (Y/N)?" question, it's true that this only happens with a batch/cmd file which is executed directly from terminal. For example, upon Ctrl-C:
Additionally, I do not believe you need a third party binary to do this if you are happy to use powershell, the following line of powershell will give you a list of all of the processes running, their pid, and their parent pid - so from here it would be possible to terminate a process tree recursively with no additional binaries:
|
Happy to take a PR that detects powershell and uses that method. I'm not in Windows at all, so I'm no help here (and unless someone actually contributes a PR to address this, nothing is really going to change - though I'm sure I've said this before ) |
This commit silences all errors from the wmic.exe process. Which is save, because the error case is handled by falling back to the child.pid. It also optimizes the windows-kill invocation by minimizing (hiding) the terminal window and keeping it open until the process has finished.
Progress: I found a solution to hide the command prompt window (see above commit for details). Now there is only one bug left. The "restart" event is triggered twice on the bus:
@remy: I already detected the |
Short status report: I made a minimal proof of concept implementation with all the
https://github.com/countzero/nodemon_windows_kill @caesay & @chriswoodle: Do you have some time to test this? |
Hi, I optimized the https://github.com/countzero/nodemon_windows_kill proof of concept code further: Now it is cross-platform compatible, so that it can run side by side in a PowerShell context and a WSL Debian context: My findings from this are, that nodemon sometimes triggers POSIX signals multiple times in BOTH the Windows AND the UNIX context! I am not sure, if that is intended behaviour. @remy Is this actually intended behaviour, or is this a bug?
|
fyi, we wrote a very small package to do nothing except send a Don't know if it would be a handy thing to use here, but it's worked well for us, and is extremely lightweight. |
Here is a way to send ctrl-c to the node process. Command
File: ctrlc.ps1
Edit: Made it so I didn't need a separate file. This approach doesn't work though. It sends the Ctrl-C to too many things.
|
@countzero I tried your solution. The link for windows-kill was dead, but it worked for me after I changed it. Are you going to bundle the exe, and make a pull request? |
…1720) This commit replaces the provisioning script, which downloaded a specific windows-kill.exe binary, with the binary itself. We are using the x64 version of the windows-kill.exe from: https://github.com/ElyDotDev/windows-kill/releases/tag/1.1.4 We are now embedding the binary with the project directly to decrease the complexity and avoid breaking the build by external changes. Actually the maintainer of windows-kill changed his GitHub username recently from "alirdn" to "ElyDotDev" which broke the provisioning script!
@remy I removed the provisioning script and added the You were right there to be sceptic. This way nodemon is way more resilient to external changes: Actually the maintainer of windows-kill changed his GitHub username recently from "alirdn" to "ElyDotDev" which broke the provisioning! I updated https://github.com/countzero/nodemon_windows_kill to match my latest commit and added a PR for you: #1853 |
@countzero I think the windows-kill binary is better. I couldn't get it to work correctly with just powershell, it is probably possible, but it might be a lot of work. |
Folks, there's a debug build available on npm from @countzero's great work. Those with Windows, could you test the install using If you can comment here or on PR #1853 that would be perfect to help sign these changes off. |
2.0.8-alpha.a is correctly shutting down, and restarting for me. It might help if 'SIGKILL' mapped to the previous method, to give people another option if the program they are working on ignores SIGINT, or just takes a long time, and they don't care about it safely exiting. |
@wrexbe: I like that idea! So the use case is: As a developer on Windows I like nodemon to terminate the node process by force, to quickly restart my node process that does not require a graceful shutdown. Under Windows nodemon would then have two behaviours:
@remy: If you are OK with this behaviour, I can extend the PR. Just need some time... |
This commit adds back the hard shutdown behaviour under Windows as an opt-in: If the --signal option is "SIGKILL" nodemon will terminate the process group by force without waiting for the process to clean-up. This matches a SIGKILL on a UNIX system.
@wrexbe Done. Please take a look at countzero@f7c6e0d and @remy this commit is also included in the PR #1853! |
@wrexbe @chriswoodle @cowboyd @nicholas-ochoa @JulianNicholls @usm076 @caesay @oviecodes @GIDustin @sridharjatla Do you have some time to test this solution to our shared problem? You can simply check out https://github.com/countzero/nodemon_windows_kill to rapidly test this fix. The npm run watch-graceful-shutdown And the "old" npm run watch-hard-shutdown See https://github.com/countzero/nodemon_windows_kill/blob/main/package.json for technical details. @remy I am not quite sure what else can be done here. What are the next steps? |
@remy That alpha works for me also, thanks! :) Just ran into this, but glad to find this thread. |
Your test project worked fine for me. I even tested without a SIGINT handler to ensure that apps without one would still function as they did in the past and that worked as well. |
@countzero Seems to work for me in CMD, PowerShell, Git Bash: However, I'm not sure if this is related, but your example repo doesen't detect file changes when running in WSL (I tested in WSL 2 Debian). Saving index.js does not trigger a restart I tested your repo on the example repo I linked (https://github.com/chriswoodle/nodemon-1567) and I get the same behavior. |
@chriswoodle I am currently using WSL 1 because I am working frequently with VirtualBox. This works with Debian: Please note, that I did NOT modify the process restart behaviour of nodemon in a UNIX context. If you can reproduce this behaviour in a Debian under Microsofts WSL 2 you found a new bug which is unrelated to the changes discussed in this issue! I am curious if you can reproduce this with the latest nodemon version... |
@countzero Yea, looks like this is unrelated bug, that issue happens on latest nodemon 2.0.7 |
Yep, I've opened a new issue: #1866 @countzero 's repo seems to work just fine. |
Merged and live. |
Possibly related to #1567
nodemon -v
: 6.13.4node -v
: v12.16.1Operating system/terminal environment:
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.
WSL:
![wsl](https://github.com/chriswoodle/nodemon-1567/raw/master/wsl.png)
Actual behaviour
The application is killed and restarted right away. This behavior does not exist in Linux, Mac and WSL.
CMD:
![windows](https://raw.githubusercontent.com/chriswoodle/nodemon-1567/master/windows.png)
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']
Let me know if I can provide any more information or test changes, thanks.
The text was updated successfully, but these errors were encountered: