mrbar42/node-graceful

on() does not wait for promise to resolve on SIGINT

Closed this issue · 5 comments

From docs...

The listener function can return a promise that will delay the process exit until it's fulfilment.

import Graceful from "node-graceful"

Graceful.captureExceptions = true
Graceful.captureRejections = true
Graceful.timeout = 0

console.log("starting...")

Graceful.on("exit", (signal, details) => {
  return new Promise((resolve) =>
    setTimeout(() => {
      console.log(signal, details)
      resolve()
    }, 2500)
  )
})

process.openStdin()

Hitting Ctrl+C causes the program to terminate immediately. Should it wait for for 2.5s before terminating?

Hey,

I just tested your snippet and after clicking Ctrl+c it waits for 2500ms and then closes.

What OS are you using?

also, just making sure you are not clicking Ctrl+c twice which will immediately exit by design.
this behaviour can be disabled by setting Graceful.exitOnDouble = false
exitOnDouble docs

~/code/graceful-test
10:08 $ node --version
v10.16.3
~/code/graceful-test
10:09 $ uname -an
Darwin suave.local 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64 i386 MacPro6,1 Darwin

I figured it out...it's due to npm.

node -r @babel/register index.js

works, but running as npm start does not.

nice catch, i'm able to reproduce.

looks like Ctrl+C is sending SIGINT to all process group and then npm sends it once more which triggers the exitOnDouble logic.
apparently this is a known problem.

Workarounds:

  1. run the command directly like your examples node index.js and not through npm
  2. disable the exitOnDouble logic like so: Graceful.exitOnDouble = false. i tested it and it restores the proper waiting behaviour.

closing this for now, feel free to reopen if further discussion is needed.