Skip to content

Commit

Permalink
fix: prevent new workers from writing to closed pool
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed May 22, 2024
1 parent ea05a00 commit cb7ce9f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/entry/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ process.on('message', (message: IncomingMessage) => {
ready: true,
source: 'pool',
__tinypool_worker_message__: true,
})
}) // TODO: Add no-op
})().catch(throwInNextTick)

return
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/nested-pool.mjs
Original file line number Diff line number Diff line change
@@ -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() {}
18 changes: 18 additions & 0 deletions test/termination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit cb7ce9f

Please sign in to comment.