tinylibs / tinypool

🧵 A minimal and tiny Node.js Worker Thread Pool implementation (38KB)

Home Page:https://npmjs.com/package/tinypool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Codebase question regarding isolate workers

rluvaton opened this issue · comments

Great repo!

I noticed that if isolateWorkers is true you reuse the same workers (recycle) but wouldn't that lead to stuff being kept in the previous run of the worker?

When isolateWorkers is true the workers are terminated after a task has been run.

tinypool/src/index.ts

Lines 1015 to 1024 in c444320

shouldRecycleWorker(taskInfo?: TaskInfo): boolean {
// Worker could be set to recycle by pool's imperative methods
if (taskInfo?.workerInfo?.shouldRecycle) {
return true
}
// When `isolateWorkers` is enabled, remove the worker after task is finished
if (this.options.isolateWorkers && taskInfo?.workerInfo) {
return true
}

tinypool/src/index.ts

Lines 917 to 918 in c444320

if (this.shouldRecycleWorker(taskInfo)) {
this._removeWorker(taskInfo.workerInfo!)

tinypool/src/index.ts

Lines 840 to 846 in c444320

_removeWorker(workerInfo: WorkerInfo): Promise<void> {
workerInfo.freeWorkerId()
this.workers.delete(workerInfo)
return workerInfo.destroy(this.options.terminateTimeout)
}

tinypool/src/index.ts

Lines 481 to 497 in c444320

async destroy(timeout?: number): Promise<void> {
let resolve: () => void
let reject: (err: Error) => void
const ret = new Promise<void>((res, rej) => {
resolve = res
reject = rej
})
const timer = timeout
? setTimeout(
() => reject(new Error('Failed to terminate worker')),
timeout
)
: null
this.worker.terminate().then(() => {

When isolation is disabled, then it indeed leads to a situation where worker may have side effects caused by previous run.

Thank you, I thought recycle meaning reuse