From cb7ce9fec1ffeec5592640cb12c16d89a313d367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Wed, 22 May 2024 18:19:20 +0300 Subject: [PATCH] fix: prevent new workers from writing to closed pool --- src/entry/process.ts | 2 +- test/fixtures/nested-pool.mjs | 17 +++++++++++++++++ test/termination.test.ts | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/nested-pool.mjs diff --git a/src/entry/process.ts b/src/entry/process.ts index ee09871..67f0f87 100644 --- a/src/entry/process.ts +++ b/src/entry/process.ts @@ -43,7 +43,7 @@ process.on('message', (message: IncomingMessage) => { ready: true, source: 'pool', __tinypool_worker_message__: true, - }) + }) // TODO: Add no-op })().catch(throwInNextTick) return diff --git a/test/fixtures/nested-pool.mjs b/test/fixtures/nested-pool.mjs new file mode 100644 index 0000000..9249cea --- /dev/null +++ b/test/fixtures/nested-pool.mjs @@ -0,0 +1,17 @@ +import { cpus } from 'os' +import { Tinypool } from 'tinypool' + +export default async function nestedPool() { + const pool = new Tinypool({ + filename: new URL(import.meta.url, import.meta.url).href, + runtime: 'child_process', + isolateWorkers: true, + minThreads: cpus().length - 1, + maxThreads: cpus().length - 1, + }) + + await Promise.resolve() + pool.recycleWorkers() +} + +export function entrypoint() {} diff --git a/test/termination.test.ts b/test/termination.test.ts index b34f922..e559830 100644 --- a/test/termination.test.ts +++ b/test/termination.test.ts @@ -56,3 +56,21 @@ test('writing to terminating worker does not crash', async () => { await destroyed }) + +test('recycling workers while closing pool does not crash', async () => { + const pool = new Tinypool({ + runtime: 'child_process', + filename: resolve(__dirname, 'fixtures/nested-pool.mjs'), + isolateWorkers: true, + minThreads: 1, + maxThreads: 1, + }) + + await Promise.all( + Array(50) + .fill(() => pool.run({})) + .map((fn) => fn()) + ) + + await pool.destroy() +})