node hangs after completing the task (handles keep the process running)
gravsten opened this issue · 1 comments
gravsten commented
When running from top-most level, node hangs after completing the task because handles keep the process running.
I have installed the "why-is-node-running" package to understand why, here is the output:
C:\>node main.js
result from thread pool: 2
There are 7 handle(s) keeping the process running
# TTYWRAP
(unknown stack trace)
# WORKER
C:\DATA\web\neatalerts\crons\node\node_modules\node-worker-threads-pool\dist\poolWorker.js:17 - super(...args);
C:\DATA\web\neatalerts\crons\node\node_modules\node-worker-threads-pool\dist\staticPool.js:76 - this.fill(() => new poolWorker_1.PoolWorker(script, workerOpt));
C:\DATA\web\neatalerts\crons\node\node_modules\node-worker-threads-pool\dist\pool.js:50 - const worker = getWorker();
C:\DATA\web\neatalerts\crons\node\node_modules\node-worker-threads-pool\dist\pool.js:83 - this.workers.push(this.createWorker());
C:\DATA\web\neatalerts\crons\node\node_modules\node-worker-threads-pool\dist\staticPool.js:76 - this.fill(() => new poolWorker_1.PoolWorker(script, workerOpt));
C:\DATA\web\neatalerts\crons\node\fr\main.js:2336 - const staticPool = new StaticPool({
C:\DATA\web\neatalerts\crons\node\fr\main.js:5575 - })();
And here is the code:
var { StaticPool } = require('node-worker-threads-pool');
const staticPool = new StaticPool({
size: 4,
task: (n) => n + 1
});
return staticPool.exec(1).then((result) => {
console.log('result from thread pool:', result); // result will be 2.
setTimeout(function () { log_why(); }, 200); // should be placed at the end of execution (logs out active handles that are keeping node running)
process.on('uncaughtException', function (exception) { console.log(exception); }); // show exception details in the console
});
gravsten commented
I found a solution by adding at the end:
staticPool.destroy();