Skip to content
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

Windows - process cannot catch PM2 SIGINT #3555

Closed
braytak opened this issue Mar 19, 2018 · 7 comments
Closed

Windows - process cannot catch PM2 SIGINT #3555

braytak opened this issue Mar 19, 2018 · 7 comments
Labels

Comments

@braytak
Copy link

braytak commented Mar 19, 2018

What's going wrong?

My Windows process is seemingly not getting SIGINT on stop command, since my process.on('SIGNINT'...) handler never gets triggered - so can't do gracefull exit!
The process correctly handles SIGINT when run from a terminal window by doing Ctlr-C.

Furthermore, the pm2 log head below shows that the process exited with code [1], while my signal handler exits with code [0] on SIGINT.

How could we reproduce this issue?

Run the following with pm2 start --no-autorestart sigint.js

*File: sigint.js*

process.on('SIGINT', ()=>{
    clearInterval(interval);
    console.log('GOT SIGINT');
    setTimeout(()=>{process.exit(0)}, 1000)
});

let interval = setInterval(()=>{
    console.log('Waiting for SIGINT')
}, 5000)

You never see the GOT SIGINT in the process log... only a series of Waiting for SIGINT

Supporting information

pm2 version : 2.10.1
node version: 8.9.1
Windows 10

pm2 log head:

[2018-03-19 14:29:38] PM2 log: Starting execution sequence in -fork mode- for app name:sigint id:0
[2018-03-19 14:29:38] PM2 log: App name:sigint id:0 online
[2018-03-19 14:29:46] PM2 log: Stopping app:sigint id:0
[2018-03-19 14:29:46] PM2 log: App [sigint] with id [0] and pid [14224], exited with code [1] via signal [SIGINT]
[2018-03-19 14:29:46] PM2 log: pid=14224 msg=process killed
@fanfan6ji581
Copy link

I m having same problem

@wallet77
Copy link
Contributor

Hi there,

As nodejs doc says, Windows can't handle signals as Linux does :

Note: Windows does not support sending signals, but Node.js offers some emulation with process.kill(), and ChildProcess.kill(). Sending signal 0 can be used to test for the existence of a process. Sending SIGINT, SIGTERM, and SIGKILL cause the unconditional termination of the target process.

@braytak
Copy link
Author

braytak commented Mar 26, 2018

@wallet77 I have to admit knowing that when I posted - my objective was to see if any of the bright minds out there had a workaround...

Since then I found this related piece of information in the pm2 online doc; Windows graceful stop.

I tried the recommended workaround without any success... below is a standalone code snippet I used to test the workaround.
I run it using the following pm2 commands:

  pm2 start test.js --no-autorestart --kill-timeout 5000
  pm2 stop test
  pm2 logs test		

All I see in the resulting logs is the waiting for... message!
Am I not understanding the workaround correctly?

test.js

let waitForMessage;

// recommended `pm2` workaround
process.on('message', msg => {
    console.log('process.on message:', msg);
    if (msg === 'shutdown') {
        setTimeout(()=>{
            clearInterval(waitForMessage);
            console.log('termination housekeeping done')
            process.exit(0)
        }, 2000)
    }
});

waitForMessage = setInterval(() => {
    console.log('Waiting for "shutdown" message')
}, 2000)

@xorbis
Copy link

xorbis commented May 4, 2018

@stale
Copy link

stale bot commented May 24, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 24, 2020
@stale stale bot closed this as completed Jun 7, 2020
@ntraut
Copy link

ntraut commented May 30, 2021

What's going wrong?

My Windows process is seemingly not getting SIGINT on stop command, since my process.on('SIGNINT'...) handler never gets triggered - so can't do gracefull exit!
The process correctly handles SIGINT when run from a terminal window by doing Ctlr-C.

Given that, why would it be impossible for pm2 to handle SIGINT on Windows if that works if the app is run from the terminal?
From what I tested, SIGINT is intercepted when running with nodemon but not with node, so maybe nodemon implemented a workaround on Windows...

@braytak
Copy link
Author

braytak commented May 30, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants