Skip to content

Commit

Permalink
fix: use internal workerId for child_process runtime (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisd23 committed Jan 17, 2024
1 parent ac2e839 commit 0eb5e0c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/common.ts
Expand Up @@ -16,7 +16,7 @@ export interface TinypoolWorker {
argv?: string[]
execArgv?: string[]
resourceLimits?: any
workerData?: TinypoolData
workerData: TinypoolData
trackUnmanagedFds?: boolean
}): void
terminate(): Promise<any>
Expand Down
2 changes: 1 addition & 1 deletion src/entry/process.ts
Expand Up @@ -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) => {
Expand Down
8 changes: 7 additions & 1 deletion src/runtime/process-worker.ts
Expand Up @@ -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!

Expand Down
6 changes: 6 additions & 0 deletions test/runtime.test.ts
Expand Up @@ -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' })

Expand Down

0 comments on commit 0eb5e0c

Please sign in to comment.