async-exit-hook
async-exit-hook copied to clipboard
Hook does not finish executing in worker process
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
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