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

Hook does not finish executing in worker process #11

Open
ghost opened this issue Aug 29, 2019 · 1 comment
Open

Hook does not finish executing in worker process #11

ghost opened this issue Aug 29, 2019 · 1 comment

Comments

@ghost
Copy link

ghost commented Aug 29, 2019

Let me illustrate the problem with the following code:

const cluster = require("cluster");
const exitHook = require("async-exit-hook");

function addHook(delay) {
    exitHook(cb => {
        console.log("Start " + delay);
        setTimeout(() => {
            console.log("Finish " + delay);
            cb()
        }, delay);
    });
}

if (cluster.isMaster) {
    cluster.fork();
} else {
    addHook(0);
    addHook(1);
    addHook(10);
    addHook(100);
}

If I start this script and kill it with ctrl+c, then I get:

$ node exitHook.js
^C
$ Start 0
Start 1
Start 10
Start 100
Finish 0
Finish 1

As you can see, only the very fast exit hooks are executed. This problem only happens in workers. Is this a known issue?

Versions:

  • Node: v10.16.3 and v12.8.0
  • Async exit hook: 2.0.1
  • Operating system: Ubuntu 18.04.1 LTS
@kabalin
Copy link

kabalin commented Jan 4, 2021

Just came accross this while searching for different issue and got curious 😄 I think all hooks are executed, you don't see the output presumably because main process dies after worker process gets disconnected (but is still processing hooks). If you modify the code in a way that worker kills itself, you will see all hooks are processed as expected:

const cluster = require("cluster");
const exitHook = require("async-exit-hook");

function addHook(delay) {
    exitHook(cb => {
        console.log("Start " + delay);
        setTimeout(() => {
            console.log("Finish " + delay);
            cb()
        }, delay);
    });
}

if (cluster.isMaster) {
    cluster.fork();
} else {
    addHook(0);
    addHook(1);
    addHook(10);
    addHook(100);
    // Kill worker with SIGTERM.
    process.kill(process.pid);
}

will give you the output:

$ node exitHook.js
Start 0
Start 1
Start 10
Start 100
Finish 0
Finish 1
Finish 10
Finish 100

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

No branches or pull requests

1 participant