worker_threads: worker handles not cleaned up in async_hooks API

  • Version: v11.10.1
  • Platform: Linux 4.20.11-arch1-1-ARCH deps: update openssl to 1.0.1j #1 SMP PREEMPT Wed Feb 20 21:11:12 UTC 2019 x86_64 GNU/Linux
  • Subsystem: worker_threads

Not sure if it's a bug or not, but I've noticed that destroy is never executed for the asyncIds pertaining to workers.

'use strict';

const threads = require('worker_threads');

if (!threads.isMainThread) {
  console.log('Thread: ' + threads.threadId);
  return;
}

const hooks = require('async_hooks');

async function spawn(file) {
  const worker = new threads.Worker(file);
  return new Promise((resolve, reject) => {
    worker.on('exit', resolve);
  });
}

async function wait(ms) {
  return new Promise(cb => setTimeout(cb, ms));
}

(async () => {
  const active = new Map();

  const hook = hooks.createHook({
    init(id, type) {
      if (type === 'WORKER')
        active.set(id, type);
    },
    destroy(id) {
      active.delete(id);
    }
  });

  hook.enable();

  for (let i = 0; i < 10; i++)
    await spawn(__filename);

  await wait(1000);

  hook.disable();

  await wait(1000);

  console.log(active);
})();

Output:

Thread: 1
Thread: 2
Thread: 3
Thread: 4
Thread: 5
Thread: 6
Thread: 7
Thread: 8
Thread: 9
Thread: 10
Map {
  3 => 'WORKER',
  23 => 'WORKER',
  39 => 'WORKER',
  55 => 'WORKER',
  71 => 'WORKER',
  87 => 'WORKER',
  103 => 'WORKER',
  119 => 'WORKER',
  135 => 'WORKER',
  151 => 'WORKER' }