diff --git a/src/entry/worker.ts b/src/entry/worker.ts index 40eb3ae..78a3527 100644 --- a/src/entry/worker.ts +++ b/src/entry/worker.ts @@ -49,6 +49,8 @@ parentPort!.on('message', (message: StartupMessage) => { const readyMessage: ReadyMessage = { ready: true } parentPort!.postMessage(readyMessage) + port.start() + port.on('message', onMessage.bind(null, port, sharedBuffer)) atomicsWaitLoop(port, sharedBuffer) })().catch(throwInNextTick) diff --git a/src/index.ts b/src/index.ts index 01c71d6..eef0ecc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -791,6 +791,8 @@ class ThreadPool { worker.on('message', (message: ReadyMessage) => { if (message.ready === true) { + port1.start() + if (workerInfo.currentUsage() === 0) { workerInfo.unref() } diff --git a/src/runtime/process-worker.ts b/src/runtime/process-worker.ts index ca96aa4..50a2316 100644 --- a/src/runtime/process-worker.ts +++ b/src/runtime/process-worker.ts @@ -92,6 +92,7 @@ export default class ProcessWorker implements TinypoolWorker { transferListItem?.forEach((item) => { if (item instanceof MessagePort) { this.port = item + this.port.start() } }) @@ -149,7 +150,7 @@ export default class ProcessWorker implements TinypoolWorker { // The forked child_process adds event listener on `process.on('message)`. // This requires manual unreffing of its channel. - this.process.channel?.unref() + this.process.channel?.unref?.() if (hasUnref(this.process.stdout)) { this.process.stdout.unref()