From 5605bdb9305f9ded1d9e3019947bdb3decf31df9 Mon Sep 17 00:00:00 2001 From: Ali Sheehan-Dare Date: Wed, 17 Jan 2024 14:58:44 +0000 Subject: [PATCH] fix: use internal workerId for child_process runtime --- src/common.ts | 2 +- src/entry/process.ts | 2 +- src/runtime/process-worker.ts | 8 +++++++- test/runtime.test.ts | 6 ++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/common.ts b/src/common.ts index 80430a7..650a5dd 100644 --- a/src/common.ts +++ b/src/common.ts @@ -16,7 +16,7 @@ export interface TinypoolWorker { argv?: string[] execArgv?: string[] resourceLimits?: any - workerData?: TinypoolData + workerData: TinypoolData trackUnmanagedFds?: boolean }): void terminate(): Promise diff --git a/src/entry/process.ts b/src/entry/process.ts index 7e94f78..39f0295 100644 --- a/src/entry/process.ts +++ b/src/entry/process.ts @@ -20,7 +20,7 @@ process.__tinypool_state__ = { isChildProcess: true, isTinypoolWorker: true, workerData: null, - workerId: process.pid, + workerId: Number(process.env.TINYPOOL_WORKER_ID), } process.on('message', (message: IncomingMessage) => { diff --git a/src/runtime/process-worker.ts b/src/runtime/process-worker.ts index 5139718..fddd214 100644 --- a/src/runtime/process-worker.ts +++ b/src/runtime/process-worker.ts @@ -23,7 +23,13 @@ export default class ProcessWorker implements TinypoolWorker { this.process = fork( fileURLToPath(import.meta.url + '/../entry/process.js'), options.argv, - options + { + ...options, + env: { + ...options.env, + TINYPOOL_WORKER_ID: options.workerData[0].workerId.toString(), + }, + } ) this.threadId = this.process.pid! diff --git a/test/runtime.test.ts b/test/runtime.test.ts index 58de981..01d2a93 100644 --- a/test/runtime.test.ts +++ b/test/runtime.test.ts @@ -85,6 +85,12 @@ describe('child_process', () => { expect(result).toBe(threadId) }) + test('child process workerId should be internal tinypool workerId', async () => { + const pool = createPool({ runtime: 'child_process' }) + const workerId = await pool.run('process.__tinypool_state__.workerId') + expect(workerId).toBe(1) + }) + test('errors are serialized', async () => { const pool = createPool({ runtime: 'child_process' })